How do I round a button programmatically in Android? - android

I have a Button which populates a GridView using a custom adapter's call to getView(). As such, there is no .xml file for the Button.
Is there a way to programmatically round the Button?

Use a nine patch, here is an example one:
save this in your /res/drawable-mdpi/ directory with the filename btn_round.9.png
then in your java code do this:
mBtn.setBackgroundResource(R.drawable.btn_round);
because it is 9 patch wit will stretch to fit whatever content you are putting inside the button. Search for "android draw9patch" to learn more about how to create the 9 patch files.

Below given method returns rounded bitmap of image.Apply it to button image and check if this can help
public 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;
}

Use an XML drawable background with corners and use setBackground. See How to make the corners of a button round?
You can use this even though the button itself isn't defined in xml, you're just declaring the background as an xml file.

Related

set image inside circle shaped layout and hide overlapping part

I'm using frame layout and I've added imageview into it. frame layout is circled shape. the imageview inside frame layout moves outside of that circle when it's size gets bigger than layout size. I want to hide that extra imageview. any help would be much appreciated.
Thanks :)
Try this function...
You need to provide your square bitmap file and output size of the circle image you want.
public static Bitmap getCroppedBitmap(Bitmap bitmap, int size) {
bitmap = Bitmap.createScaledBitmap(bitmap, size, size, false);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Basically you should use canvas for this purpose.
Draw a circular canvas and add you image bitmap in it.
For your refs https://github.com/lopspower/CircularImageView
You can use this lib in your project
just add this in xml
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="#drawable/image"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true" />

How to provide stroke and rounded inner corners in Android

I am having custom ImageView which will be having stroke and rounded inner corners.
I have successfully been able to do this. But I also want to perform zoom & translate on this image. So if I perform zooming, then rounded corners is not seen.
Below is the code which I have used:
Bitmap output = Bitmap.createBitmap(bg_image_bitmap.getWidth(),bg_image_bitmap.getHeight(),bg_image_bitmap.getConfig());
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bg_image_bitmap
.getWidth(), bg_image_bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(getResources().getColor(
R.color.theme_red));
canvas.drawRoundRect(rectF, roundCornerRadius,
roundCornerRadius, paint);
paint.setXfermode(new PorterDuffXfermode(
Mode.SRC_IN));
canvas.drawBitmap(bg_image_bitmap, rect, rect,
paint);
paint.setXfermode(null);
view_BG.setImageBitmap(output);
I know I have given image height and width to canvas, so issue will arise. But I am not getting other option to proceed.
Please help me to fix this problem.
Thanks.
I think while zooming, you have to recalculate the width and height of image and again recall above code.

imageview in android resolution [duplicate]

This question already has answers here:
How to make an ImageView with rounded corners?
(58 answers)
How to create a circular ImageView in Android? [duplicate]
(1 answer)
Closed 9 years ago.
i would like to know how to view the image in a circle view ?! like the imageview in instagram or like the other applications that can be put an imageview . In fact i would like to control by display the image in a circle or square .
Just check the below code
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;
}
please refer to this answer here
and please five me some feedback
Hope that Helps .

Animation for converting Circular image to Rounded corner in android

I have an image with circular shape,I want to apply an animation for Circular image,so that it should slowly convert into little bigger size rounded corner image.
I tried for converting circular image to rounded corner but could not able to write an animation like effect.
This makes me to convert image into rounded rectangle,but I wants the Animation for this
kind of performance.My animation should slowly convert my circular image to rectangle with rounded corners and also increase in size
public Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels)
{
result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
color = 0xff424242;
paint = new Paint();
rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
rectF = new RectF(rect);
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 result;
}

Low FPS in the ListView with ImageView

I have a ListView with the custom ArrayAdapter. It have one ImageView and couple TextViews.
I use Cache for downloading and saving Images.
I created round corners for ImageView, used this code:
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 = 15;
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);
And I use this code in Cache class in the ArrayAdapter.
When I moved list up\down I have low FPS(frame per second)
Condition: All images must be saved in real size and without any changes.
On the screen at the same time present 7 - 8 items.
Testing device: Samsung Galaxy Tab p1000.

Categories

Resources