How to set bitmap in circular imageview? - android

I have 1 circular imageview and I want to place bitmap inside the imageview.
but when I set the compressed bitmap image it is always rectangle.
Please help me to set bitmap in circular imageview.
Thanks

I am curious about how you created a circular ImageView. Can you share that secret ??
As far as creating a circular Bitmap is concerned, create a BitmapShader from the bitmap you want to show. Then create a ShapeDrawable (Oval) and assign the bitmap shader to it. Draw the drawable. Bam! circular image!
Bitmap bitmap = getthebitmapyouwanttoshowinacirclefromsomewhere;
Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader (bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);
myImageView.setImageBitmap(circleBitmap);

Androidx released native support for this.
add to your build.gradle:
dependencies {
...
implementation "androidx.core:core-ktx:1.7.0"
}
usage (kotlin is shown here, java is also supported):
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
val bitmap: Bitmap = ... // get from somewhere
val roundedBitmapWrapper: RoundedBitmapDrawable =
RoundedBitmapDrawableFactory.create(Resources.getSystem(), bitmap)
roundedBitmapWrapper.setCircular(true) // important! if you don't call this, the wrapper will just show the original rectangle bitmap
// and then you can display the roundedBitmap on an ImageView like this:
val imageView: ImageView = ... // get from somewhere
imageView.setImageDrawable(roundedBitmapWrapper)
P.S., the class RoundedBitmapDrawable has a lot of other cool stuff, for example if you don't want the bitmap to be completely circle, but instead just give some rounded-corners effect for the original rectangle bitmap, this can also be supported. check out the official doc at https://developer.android.com/reference/androidx/core/graphics/drawable/RoundedBitmapDrawable

Related

How to set round corner image from bitmap (get image string from server)

i have tried this code image rounder perfectly but rounded image can not focus my face!!! any body help me?
//Bitmap mbitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.cat)).getBitmap();
Bitmap imageRounded = Bitmap.createBitmap(result.getWidth(), result.getHeight(), result.getConfig());
Canvas canvas = new Canvas(imageRounded);
Paint mpaint = new Paint();
mpaint.setAntiAlias(true);
mpaint.setShader(new BitmapShader(result, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect((new RectF(0, 0, 258, 250)), 120, 120, mpaint);// Round Image Corner 100 100 100 10
// ivPhoto.setImageBitmap(result);
ivPhoto.setImageBitmap(imageRounded);
If only a rounded imageview is what you're looking for then a CircularImageView might help you, it also supports the .setimageBitmap() method
Check this library. Its pretty easy to use.
Use this library, have many customization's.

Android Image Masking with Rotate & Scale

How to mask image such as we can scale/zoom/rotate the background image but not the mask?
I should be able to zoom the image in this mask but it is scaling whole image.
Suggest me a way to achieve this, I'm creating a photo collage app.
Blue color is background of Layout.
The white color is for mask
I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it.
private void getMaskedBitmap() {
Bitmap bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
ImageView bg = (ImageView) findViewById(R.id.bg);
bg.setImageBitmap(bgBitmap);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
Canvas canvasBmp = new Canvas(bgBitmap);
ImageView mImageView = (ImageView) findViewById(R.id.troll_face);
Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable);
Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable);
original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
//mImageView.setScaleType(ScaleType.FIT_XY);
mImageView.setBackgroundResource(R.drawable.background_drawable);
bg.setOnTouchListener(new MultiTouchListener());
mImageView.invalidate();
}
Exception on line
Canvas canvasBmp = new Canvas(bgBitmap);
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
I cannot provide you code but here is what you should do:
Get background image as bitmap and draw it on view's canvas.
Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask.
Canvas canvasBmp = new Canvas(bmp);
Now draw bmp on view's canvas. So, that you can get mask effect on image.
Zoom (re-size etc whatever you want) background bitmap image.
Invalidate your view, and follow all steps again

How to draw a circle in Drawable.draw

Q1. The R.drawable.wallpaper can show but the point cannot show.
Q2. Moreover, how to put an other image in
Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
I mean 2 pictures mix.
img = (ImageView) findViewById(R.id.img);
Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
bitmap.setBounds(0, 0, bitmap.getIntrinsicWidth(), bitmap.getIntrinsicHeight());
Bitmap point = Bitmap.createBitmap(
bitmap.getIntrinsicWidth(),
bitmap.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888
);
Paint radius = new Paint(); radius.setColor(Color.RED);
radius.setStyle(Paint.Style.FILL); radius.setStrokeWidth(1);
radius.setAntiAlias(true);
Canvas canvas = new Canvas(point);
canvas.drawCircle((float)(5), (float)(5), 5, radius);
bitmap.draw(canvas);
img.setImageDrawable(bitmap);
Look in the Android SDK samples. There are several examples in the ApiDemos project:
/ApiDemos/res/drawable/
black_box.xml
shape_5.xml
etc
It will look something like this for a circle with a gradient fill:

Android Drawing text onto a bitmap

I'm trying to write some text across a bitmap in Android. I've followed several guides and attempted several different code variations but I either get something obscure, a blank screen or the unaltered bitmap. This is the basic idea of what I've been playing around with.
public Bitmap writeOnDrawable(Context gContext, String path, String text) {
Bitmap bitmap = BitmapFactory.decodeFile(path);
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawPoint(30, 50, paint);
canvas.drawText("Text", 33, 53, paint);
return bmOverlay;
}
I pass this function the variables and then use the returned bitmap by turning it into a BitmapDrawable and making it a background of a layout. This particular version of the function returns only my initial, unaltered bitmap with no "Text" text across it.
I've attempted variations such as copying the original image:
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Drawing the original image onto the canvas:
canvas.drawBitmap(drawableBitmap, screenHeight, screenWidth, paint);
I solved the problem using android-maps-util library.
add compile 'com.google.maps.android:android-maps-utils:0.4' dependency to your gradle file.
Use IconGenerator to create bitmap with text and use it in your BitmapDescriptor to create marker on your map.

Android clip Picture to Bitmap. Is Possible?

I have a Picture object, loaded from an SVG file, and I have set hardwareAccelerated=false to make it works on all devices.
Since there is a bug on android 4.0.4, I have to convert the Picture to Bitmap and I do that, in this way:
...
...
public void draw(Canvas canvas) {
...
...
//myPicture size is 9000x5000 but I want to display only this portion
clipRect.set(50, 50, 370, 530);
Bitmap bmp = getBitmapFromPicture(myPicture, clipRect);
canvas.drawBitmap(bmp, 0, 0, null);
bmp.recycle();
...
...
}
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect) {
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()), Math.round(clipRect.height()), Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture);
}
Now I want to clip the Picture because I want to display only the visible screen part of it.
But the canvas.drawPicture does not accept srcRect parameter.
How is it possible to achieve this?
EDIT:
By translate the canvas: canvas.translate(-50, -50) it seems that translate the bitmap, too.
You need to set a transform on the Canvas.
To move the portion of the picture at 50,50 down so it is on the bitmap (ie. at 0,0), just do:
canvas.translate(-50, -50);
So your method becomes:
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect)
{
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()),
Math.round(clipRect.height()),
Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-clipRect.left, -clipRect.top);
canvas.drawPicture(picture);
}

Categories

Resources