Rounded corner ImageView - Android - android

I try to create imageview with rounded corners.i searched and found xml code
<?xml version="1.0" encoding="utf-8"?>
<solid android:color="#00ffffff" />
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp" />
<corners android:radius="12dp" />
<stroke
android:width="6dp"
android:color="#ffffffff" />
and this is a my imageview xml code
<ImageView
android:id="#+id/slidemenuuserimage"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="14dp"
android:background="#drawable/frame" />
i have one problem.when i added background image programmatically i receive divirent result.
slidemenuuserimage=(ImageView)findViewById(R.id.slidemenuuserimage);
slidemenuuserimage.setBackgroundResource(R.drawable.myuserimg);
this is a my result
how i can add padding left or right programmatically.in my option this is a my problem
if anyone knows solution please help me

You can also do this programatically
public Bitmap roundCornerImage(Bitmap raw, float round) {
int width = raw.getWidth();
int height = raw.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawARGB(0, 0, 0, 0);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.parseColor("#000000"));
final Rect rect = new Rect(0, 0, width, height);
final RectF rectF = new RectF(rect);
canvas.drawRoundRect(rectF, round, round, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.raw_IN));
canvas.drawBitmap(raw, rect, rect, paint);
return result;
}
Use it like
slidemenuuserimage.setImageBitmap(roundCornerImage(BitmapFactory.decodeResource(getResources(), R.drawable.yourImage),50)

Try out this shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#00ffffff"/>
<stroke android:width="3dp"
android:color="#ffffffff"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners android:radius="30px"/>
</shape>
Code is here

you need to set src image, not background. So use setImageResource() not setBackgroundResource()

Related

Rounded corners not working when button has background image

I have a button in my activity and it has a background image also.I have added selector attributes,and it is working for set_pressed and set_focused.But rounded corners are not coming at default state of button, for that i have inserted a background image also.pls help me ...
activity_sam.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape >
<solid android:color="#f27d0f"/>
<corners android:radius="7dp"/>
</shape>
</item>
<item android:state_focused="true">
<shape >
<solid android:color="#f27d0f"/>
<corners android:radius="7dp"/>
</shape>
</item>
<item android:state_focused="false"
android:state_enabled="true"
android:drawable="#drawable/sam_logo" >
<shape >
<corners android:radius="7dp"/>
</shape>
</item>
</selector>
add Layout same size of button
provide background image and set rounder corner xml as background to Button ..if it work accept answer.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/top_right_left_coner"
android:text="#string/hello_world" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#android:color/black" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:bottomLeftRadius="10dip"
android:bottomRightRadius="10dip"
android:topLeftRadius="10dip"
android:topRightRadius="10dip" />
</shape>
Add this below last item.
<item>
<shape>
<corners android:radius="7dp"/>
</shape>
</item>
It will work as default style for button.
Add another item to show the default state as like this :
<shape>
<corners android:radius="7dp"/>
</shape>
If you want to show the rounded corner programmatically do the following :
public Drawable getRoundedBitmap(Bitmap bitmap, float Rnd_px) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff151515;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = Rnd_px;
paint.setAlpha(50);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
Drawable image = new BitmapDrawable(output);
return image;
}
The above code will crop the edges of the image. If you want to show a rounded layer over the image view. Please do the following.
<?xml version="1.0" encoding="UTF-8"?>
<solid android:color="#FFFFFF" />
<stroke
android:width="3dp"
android:color="#0000CC" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp" />
I would rather try something like this in code(not checked yet):
public class MyDrawable extends PaintDrawable {
BitmapShader mShader;
Rect mRect = new Rect();
public MyDrawable(BitmapDrawable bitmapDrawable) {
super();
final BitmapShader mShader = new BitmapShader(bitmapDrawable.getBitmap(), bitmapDrawable.getTileModeX() == null ? Shader.TileMode.CLAMP : bitmapDrawable.getTileModeX(), bitmapDrawable.getTileModeY() == null ? Shader.TileMode.CLAMP : bitmapDrawable.getTileModeY());
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setFilterBitmap(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setShader(mShader);
}
#Override
public void draw(Canvas canvas) {
int saveCount = canvas.getSaveCount();
canvas.save();
getPadding(mRect);
canvas.translate(mRect.left, mRect.top);
getShape().draw(canvas, mPaint);
canvas.translate(-mRect.left, -mRect.top);
canvas.restoreToCount(saveCount);
}
}
thanks to this You can use MyDrawable.setCornerRadii(float[] radiuses), and thanks to this draw image in rect with 4 diffrent radiuses(top-left,top-right,bottom-left,bottom-roght)

Image not cropping

I am setting a background image to LinearLayout like this:
1.back_xml:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/back" >
</item>
<item>
<shape>
<solid/>
<stroke android:width="1dip" android:color="#225786" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
2. tile.xml
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/pie_chart_background"
android:tileMode="repeat">
</bitmap>
now I am setting the back.xml as a background to LinearLayout it works fine.
I need to have a image with rounded corners and also a border to it.But i only have the border with rounded corners and not the image what is the problem in my code am i missing something?
this is how my image looks:
After spending hours of time behind your issue, finally I achieved, hope now it will give you same result as you want, please go through below code and let me know whether its works or not?
Pass appropriate parameter to below function to get rounded corner with your desire color border.
public static Bitmap getRoundedCornerImage(Bitmap bitmap, int cornerDips, int borderDips, Context context) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
context.getResources().getDisplayMetrics());
final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,
context.getResources().getDisplayMetrics());
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setColor(0xFFFFFFFF);
paint.setStyle(Paint.Style.FILL);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
paint.setColor((Color.RED)); // you can change color of your border here, to other color
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) borderSizePx);
canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);
return output;
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
OnCreate
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView rl=(ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.testing); // your desire drawable image.
rl.setImageBitmap(getRoundedCornerImage(bitmap, 10, 10, this));
}
Original image
Output
Below links help me to achieve my goal:
Border over a bitmap with rounded corners in Android
Creating ImageView with round corners
How to make an ImageView to have rounded corners
How to set paint.setColor(int color)
try your back.xml some thing like like this instead of giving a rectangle background image.
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#ff00ff >
</item>
<item>
<shape android:shape="rectangle" >
<solid/>
<stroke android:width="1dip" android:color="#225786" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
Another alternative to what Raj has posted is to use a background image with transparency at the corner.
The BEST solution, as your background is simple, is to use a 9-patch, which is the Android-native solution to the majority of background images: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch, which is similar in style to how some website backgrounds are drawn.
Android also has it's own 9-patch creator bundled with the sdk: http://developer.android.com/tools/help/draw9patch.html, so it's easy enough to edit your own png files, then apply the 9-patch to the layout. The advantage of 9-patches is that they also handle the resizing the UI for you, meaning you won't encounter as many problems when using your app on different screens
Use your shape in combination with an image with rounded corners.
You can use AndroidQuery to load the image with rounded corners.
See http://code.google.com/p/android-query/wiki/ImageLoading#Rounded_Corner
String url ="http://www.vikispot.com/z/images/vikispot/android-w.png";
ImageOptions options = new ImageOptions();
options.round = 15;
aq.id(R.id.image).image(url, options);
I think you should have done it like this.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/back" />
<stroke android:width="1dip" android:color="#225786" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
If the background is not a color but it is a bitmap, you should follow this beautiful guide
http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
Solution 1
create a 9-patch image for the border. the middle of the image should be transparent.
use a RelativeLayout as a parent and put your image LinearLayout at the bottom. set the border 9-patch as the background to another linear layout and place it on top of the image LinearLayout. Then it will cover the corners and you will get the expected result.
Solution 2
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}

Bitmap image with rounded corners with stroke

I have a image with sharp edges.
the tile_mode.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat">
</bitmap>
the back.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tile_mode" />
<item>
<shape>
<solid/>
<stroke android:width="1dip" android:color="#225786" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
layout.xml
<LinearLayout
android:id="#+id/frame1"
android:background="#drawable/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
I am setting the image as a background to this layout and drawing a border to it but the problem is the image is square with sharp edges and the border which I am drawing in the xml is rounded corners. So how to make the image also with rounded corners?
This is one of solution in which you have to make round to your main layout background and inside keep your imageview with your desire image:
something like below:
back.xml
This will make your image to round corner
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#dd7b7a"/>
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<solid android:color="#dd7b7a"/>
</shape>
tile_mode.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" />
layout.xml
<?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"
android:gravity="center"
>
<LinearLayout
android:padding="4dip"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tile_mode"
/>
</LinearLayout>
</LinearLayout>
Updated
After digging lots, I came across on of solution which already posted on stackoverflow
Changing Image as Rounded Corner
How to make an ImageView to have rounded corners
step1#
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Step2#
Make one function which make rounded to your bitmap using canvas.
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
step3#
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image=(ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.testing);
image.setImageBitmap(getRoundedCornerBitmap(bitmap, 20));
Pass original bitmap to the following function and you will get a rounded bitmap as result :) . Hope this helps someone.
public Bitmap getRoundedBitmap(Bitmap bitmap) {
Bitmap resultBitmap;
int originalWidth = bitmap.getWidth();
int originalHeight = bitmap.getHeight();
float r;
if (originalWidth > originalHeight) {
resultBitmap = Bitmap.createBitmap(originalHeight, originalHeight,
Bitmap.Config.ARGB_8888);
r = originalHeight / 2;
} else {
resultBitmap = Bitmap.createBitmap(originalWidth, originalWidth,
Bitmap.Config.ARGB_8888);
r = originalWidth / 2;
}
Canvas canvas = new Canvas(resultBitmap);
final Paint paint = new Paint();
final Rect rect = new Rect(ConstsCore.ZERO_INT_VALUE,
ConstsCore.ZERO_INT_VALUE, originalWidth, originalHeight);
paint.setAntiAlias(true);
canvas.drawARGB(ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE,
ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE);
canvas.drawCircle(r, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return resultBitmap;
}
ok let's try in below way
<?xml version="1.0" encoding="utf-8"?>
<shape
android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp"/>
<stroke android:width="2dp" android:color="#000000"/>
</shape>
<LinearLayout
android:id="#+id/frame1"
android:background="#drawable/corner_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/background"
/>
</LinearLayout>
RobinHood's answer worked for me with one change due to an error I was receiving about variable assignment.
I had to change the line:
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
To this line:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
Making my complete code this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView textViewTitle = (TextView) findViewById(R.id.MYTEXTIDHERE);
textViewTitle.setText("Some Text");
ImageButton imageButtonSetter = (ImageButton) findViewById(R.id.MYIMAGEIDHERE);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.MYIMAGENAMEHERE);
imageButtonSetter.setImageBitmap(getRoundedCornerBitmap(myBitmap, 40));
}
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Actually, there is a library that I made which meet your needs exactly, and you don't have to bother with those xml files.
https://github.com/pungrue26/SelectableRoundedImageView
With this open-source project, You can set different radii on each corners, and set borders(width and colors), and etc. like below. I hope it may help you.
try your back.xml something like this.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffffff"/>
<stroke android:width="3dp"
android:color="#ff000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
I had a similar problem today only my image was a Google map. you actually have to hide the corners and the way to do that is to create a 9Patch as the following one:
and apply it as a background over all your layout or another layout covering your layout.
Take a look at the following link for more information:
Is there a way to implement rounded corners to a Mapfragment?
I have actully went to this site: http://www.sumopaint.com/app/
and painted it all by hand you can take my example and change the colors to your liking.
you would probably need the next one:
you can get more information on how to create 9Patchs in the following links:
http://radleymarx.com/blog/simple-guide-to-9-patch/
http://android9patch.blogspot.co.il/
<?xml version="1.0" encoding="utf-8"?>
<item>
<bitmap
android:src="#drawable/bg_striped_img"
android:tileMode="repeat" />
</item>
<item android:left="-20dp" android:top="-20dp" android:right="-20dp" android:bottom="-20dp">
<shape android:shape="oval" >
<stroke
android:width="20dp"
android:color="#ffffffff" />
<solid android:color="#00000000" />
<size
android:height="120dp"
android:width="120dp" />
</shape>
</item>
<item >
<shape android:shape="oval" >
<stroke
android:width="1dp"
android:color="#ff999999" />
<solid android:color="#00000000" />
<size
android:height="120dp"
android:width="120dp" />
</shape>
</item>

Android XML drawable rounded corners with bitmap tag

I have next XML drawable blue_button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item><bitmap android:src="#drawable/button_blue_bg" />
</item>
<item >
<shape>
<corners android:radius="50dip" />
<stroke android:width="1dip" android:color="#ccffffff" />
<solid android:color="#00000000" />
<padding android:bottom="3dip"
android:left="3dip"
android:right="3dip"
android:top="3dip" />
</shape>
</item>
</layer-list>
</item>
</selector>
And i have image button_blue_bg with gradient with 1px width.
When i set button background i get next image
As you can see i have background not clipped with rounded border.
How i need modificate xml for background gradient image be not outside border?
I understand why it happended, because i use layers - so this like sandwich- but i also programming on objective c, and there it's also use layers. But in Apple it's wotks fine.
i use this.... First a Layer for the basic definition, set this layer as backgound of your layout:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="0.2dp" android:color="#color/anycolor" />
<corners android:radius="10dp" > </corners>
</shape>
</item>
<item>
<bitmap android:src="#drawable/imgback" />
</item>
</layer-list>
i use this function for making round corners:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output ;
}
and finaly, the code in the activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
// i use a Relative Layout
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlmenu);
// Obtain then backgroud of RelativeLayout
LayerDrawable layer = (LayerDrawable) rl.getBackground();
// obtain the image set in the Layer
BitmapDrawable bg = (BitmapDrawable) layer.getDrawable(1);
// create a new BitmapDrawable from the function
Drawable d =new BitmapDrawable(getRoundedCornerBitmap(bg.getBitmap()));
// set the new roundcorner image in the layer
layer.setDrawableByLayerId(1, d);
rl.setBackgroundDrawable(d);
}
A 9-patch would be perfect for your situation

Android ImageView with Rounded Corners not working [duplicate]

This question already has answers here:
How to make an ImageView with rounded corners?
(58 answers)
Closed 9 years ago.
I have definied the file thumb_rounded.xml and placed it inside the res/drawable folder in my android application. With this file i want to get rounded corners in my ImageViewthat contains the news thumb,
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="13.0dip" />
</shape>
and i am setting this as background of my ImageView, however, i dont get the rounded corners, the image remains a rectangle. Does anyone have any idea on how to get that? I want to achieve the same effect as this screen:
Many thanks
T
use it,
#Override
public void onCreate(Bundle mBundle) {
super.onCreate(mBundle);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.img);
Bitmap bitImg = BitmapFactory.decodeResource(getResources(),
R.drawable.default_profile_image);
image.setImageBitmap(getRoundedCornerImage(bitImg));
}
public static Bitmap getRoundedCornerImage(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 100;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Here , round corder is depend on roundPx.
If you place a view on top of the ImageView that functions as a mask, you can achieve exactly that rounded image effect.
take a look at this example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffff"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
android:padding="10dp"
android:src="#android:color/holo_red_dark"
/>
<View
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignLeft="#+id/image1"
android:layout_alignTop="#+id/image1"
android:layout_margin="00dp"
android:background="#drawable/mask" />
</RelativeLayout>
and a mask.xml drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp" />
<solid android:color="#0000"/>
<stroke android:color="#ffff" android:width="10dp"/>
</shape>
Two important things:
the stroke width of the mask drawable should match the padding of the ImageView
the stroke color of the mask drawable should match the background color.
the radius of the mask corners must be adjusted to correspond with the stroke width (so you don't see the image behind the mask).
The sample produces the following image:
HTH
You're lacking a couple of tags. Here's a new sample:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff"/>
<stroke android:width="3dp"
android:color="#ff000000"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners android:radius="30px"/>
</shape>
Seen here
Also, are you aware of RoundRectShape?

Categories

Resources