android


Drawing background shape with one corner and two cutting edges - Android


I want to draw a shape to set it as background. the shape has one corner and two cutting edges.
Here is the rough diagram of the shape I want with one round corner and two corners joined with straight line. I am using and to draw it. Could you help on this ?
A 9-patch bitmap (as per UDI's answer) is probably the easiest, but if you want to do it in code, create a custom Shape:
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.shapes.Shape;
import android.graphics.RectF;
public class WeirdShape extends Shape {
private static final int COLOUR = Color.RED;
private static final float STROKE_WIDTH = 1.0f;
private static final float CORNER = 10.0f;
private final Paint border = new Paint();
private final Path path;
public WeirdShape() {
path = new Path();
border.setColor (COLOUR);
border.setStyle (Paint.Style.STROKE);
border.setStrokeWidth(STROKE_WIDTH);
border.setAntiAlias (true);
border.setDither (true);
border.setStrokeJoin (Paint.Join.ROUND);
border.setStrokeCap (Paint.Cap.ROUND);
}
#Override
protected void onResize(float width, float height) {
super.onResize(width, height);
float dx = STROKE_WIDTH/2.0f;
float dy = STROKE_WIDTH/2.0f;
float x = dx;
float y = dy;
float w = width - dx;
float h = height - dy;
RectF arc = new RectF(x,h-2*CORNER,x+2*CORNER,h);
path.reset();
path.moveTo(x + CORNER,y);
path.lineTo(w - CORNER,y);
path.lineTo(w,y + CORNER);
path.lineTo(w, h);
path.lineTo(x + CORNER,h);
path.arcTo (arc,90.0f,90.0f);
path.lineTo(dx,h - CORNER);
path.lineTo(dx,y + CORNER);
path.close();
}
#Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawPath(path,border);
}
}
and then use the custom Shape in a ShapeDrawable as the background Drawable:
view.setBackground(new ShapeDrawable(new WeirdShape()));
Which looks something like:
There is no facility in ShapeDrawables for cutting the corner of a square like you have proposed. There is a 'radius' component.
You could try creating multiple images and stacking them on top of each other (using a LayeredList Drawable), but this is likely complicated, and will for sure cause overdraw (ie. bad drawing performance).
Your other alternative is to use the Paint API to create whatever image you want, which can then be cached, and used however.
put this in drawable like rounded_edittext.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="10dp"
android:topRightRadius="5dp"/>
</shape>
call drawable as edittext background
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:background="#drawable/rounded_edittext" />
</LinearLayout>
I would say use photoshop to get the aspects right and use it as png drawable

Related Links

Got null response body on Post retrofit2
how to enable select/copy texts in android?
Android SDK location should not contain whitespace, as this cause problems with NDK tools
JSON url Redirection fails
ng click not calling the function --very new to angular and ionic
Failed to build Android project with Google Play services
Error implementing tutorial code
applidium/HeaderListView not working correct
Facebook Login Button crashing app without error message
When I come back from other screens to ViewPager, then ViewPager is totally Blank, What could be the cause?
API to extract text from a image for Android Mobile
How to Go to New Screen/Page in Android Studio?
Can I pass Surface between Activities?
Fail to Move a View to a TableRow
Save data to server data base in Android give unexpected error and close
webview fragment press back to return to

Categories

HOME
beautifulsoup
phantom-dsl
ggplot2
alexa-skills-kit
pyqt
pascal
fftw
mstest
sh
composite-primary-key
error-handling
pdfbox
angularfire2
opengl-3
pkcs#11
teechart
fosrestbundle
spring-social
google-form
intercom
symmetricds
do-while
git-extensions
foreign-keys
qliksense
aggregation
tosca
slidetoggle
appirater
comparable
anki
mockjax
login-script
android-maps-extensions
instantiation
sendbird
processwire
protege4
magento-1.9.2.4
ideascript
nohup
projects-and-solutions
ods
verifiable-c
metawidget
chef-solo
portforwarding
vaadin-elements
httrack
ovf
hammer.js
dtsearch
edit
excel-dna
reshape
mouseleave
rackspace-cloud
offline.js
django-1.10
swingworker
structuremap4
filenet
chromium-os
redbeard-ios
rspec2
ng-grid
synonym
avspeechsynthesizer
xdoclet
deep
ikiwiki
cortex-m
thread-sleep
relativelayout
concurrentmodification
custom-errors
fuelcms
rubaxa-sortable
entity-framework-4.3
jquery-forms-plugin
android-broadcast
openbabel
packet-sniffers
crowd
linkedin-jsapi
tweenlite
create.js
malformedurlexception
renderer
twitter-bootstrap-rails
koala
sphinxql
mvc-editor-templates
adobe-media-server
correlated-subquery
update-statement
inline-formset
diagrams
zend-rest
filesystemobject
sifr3
business-model
information-management
cardspace

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App