How to draw a circle in Drawable.draw - android

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:

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.

Cut region out of Bitmap with Path

I'm trying to get cut a jigsaw puzzle piece out of an image, creating a new Bitmap image. I'm using a Path object to do this. This is the current result.
And how I achived this
Path path = new Path();
// Multiple path methods to create shape of puzzle piece...
path.close();
Bitmap source = BitmapFactory.decodeResource(getResources(), R.drawable.flowers);
Bitmap workingCopy = source.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(workingCopy);
path.setFillType(Path.FillType.INVERSE_WINDING);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawPath(path, paint);
((ImageView) findViewById(R.id.myImage)).setImageBitmap(workingCopy);
I wish I could have it transparent instead of black and cut out everything outside the bounds of path.
I've tried it using a PNG-file with transparancy, and the background is transparent in stead of black.

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

Distorted circle in android using canvas

Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.DKGRAY);
int y=getWindowManager().getDefaultDisplay().getWidth();
Config conf = Bitmap.Config.RGB_565;
Bitmap bmp =Bitmap.createBitmap(y,y,conf);
Canvas c = new Canvas(bmp);
c.drawCircle(y/2 ,y/2, y/3, p);
iv.setBackgroundDrawable(new BitmapDrawable(bmp));
By this code i do get circle ,which is looking as:-
Now the problem is that it doesnt look like a true circle ,and it looks like an oval shape..
So what should i do ??
Thanks in advance,....
iv.setBackgroundDrawable(new BitmapDrawable(bmp));
use setImageBitmap(), instead of setBackgroundDrawable(), and setScaleType() to FIT_CENTER

Android Circular Gradient Alpha Mask

Is there a way to draw a circular gradient mask on a bitmap in Android? Trying to produce something similar to a foggy window. Click the window and a transparent circle shows up revealing whats behind the window. Prefferably using a gradient so the center of the circle is completely transparent and the further out from the center the less transparent. Is this possible?
I'm new to Android so any code samples would be appreciated.
Thanks.
private void drawFoggyWindowWithTransparentCircle(Canvas canvas,
float circleX, float circleY, float radius) {
// Get the "foggy window" bitmap
BitmapDrawable foggyWindow =
(BitmapDrawable) getResources().getDrawable(R.drawable.foggy_window);
Bitmap foggyWindowBmp = foggyWindow.getBitmap();
// Create a temporary bitmap
Bitmap tempBitmap = Bitmap.createBitmap(
foggyWindowBmp.getWidth(),
foggyWindowBmp.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(tempBitmap);
// Copy foggyWindowBmp into tempBitmap
tempCanvas.drawBitmap(foggyWindowBmp, 0, 0, null);
// Create a radial gradient
RadialGradient gradient = new android.graphics.RadialGradient(
circleX, circleY,
radius, 0xFF000000, 0x00000000,
android.graphics.Shader.TileMode.CLAMP);
// Draw transparent circle into tempBitmap
Paint p = new Paint();
p.setShader(gradient);
p.setColor(0xFF000000);
p.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
tempCanvas.drawCircle(circleX, circleY, radius, p);
// Draw tempBitmap onto the screen (over what's already there)
canvas.drawBitmap(tempBitmap, 0, 0, null);
}

Categories

Resources