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

How to recycle a bitmap in pageAdapter with Fragment? in android
android broadcastreceiver auto start on boot up
Permission - android.permission.WRITE_EXTERNAL_STORAGE PlayStore
Select dialog option in Espresso with ArrayAdapter and Material v7 Support library
Static variable set in Activity not found in Fragment when app is resumed after being idle(when killed by android system) leading to app crash
Mqtt sample app is not keeping connection on android 5.1.1
I have a listview that displays data from Sqlite Database but it does not work when click is fired on it?
Target host must not be null, or set in parameters. scheme=null, host=null, path=eocribin.netne.net/FetchUserData.php
Animate RecyclerView onLoad
NoSuchMethodError for RecyclerView in Espresso test
Where do I type in the password in the Realm Browser mac app?
Android Retrofit xml with Callback
how to convert rgb code to hex and set as background color for textview or button in android programatically
Ignore app/build in Android Studio 1.3+ in .gitignore
Cocos Application did go into background callback not working
Android set Normal status bar programmatically

Categories

HOME
phantom-dsl
facebook-oauth
design-patterns
dns
apache-jena
rdbms
pda
mysql-workbench
sitecore
graphics
javacc
newrelic
gembox-spreadsheet
extjs4.2
pattern-matching
google-chrome-app
vimdiff
php-5.6
stellar.js
enterprise-architect
production
sigma.js
roslyn
expressionengine3
data.stackexchange.com
itunes
bioinformatics
gitlab-api
send
jmockit
dsx-desktop
hreflang
github3.py
installshield-le
protege4
parse-tree
tex
linq-to-objects
opensmpp
p4merge
dcmtk
apollostack
jide
getchar
lampp
geotiff
arq
d3-force-directed
stripe-connect
nss
upx
grass
relativelayout
character-replacement
java-money
rewrite
pypiserver
poppler
zen-cart
pax-exam
atoi
mmwormhole
cross-join
pseudo-class
addressing-mode
sip-server
contrast
rmysql
angular-carousel
jquery-slider
jammer
cocoalibspotify-2.0
poker
robotlegs
boost-signals
node-redis
pylucene
eclipse-marketplace
hbm2java
message-passing
zen
cldc
business-model
wcf-proxy
image-capture
great-circle

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