Distorted circle in android using canvas - android

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

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.

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:

Converting Canvas to Drawable

int x = 10;
int y = 10;
int r = 4;
Paint mPaint = new Paint();
mPaint.setColor(0xFF0000);
Canvas mCanvas = new Canvas();
mCanvas.drawCircle(x,y,r,mPaint);
Is there any way to convert mCanvas to a Drawable? My goal is to generate drawables with a certain shape and color.
Thanks
For simple shapes like your circle, I'd think a Shape Drawable would be easier. For more complicated things, just create a Bitmap for your Canvas to use, then create the Canvas and draw into it, then create a Drawable from your Bitmap. Something like:
int x = 10;
int y = 10;
int r = 4;
Paint mPaint = new Paint();
mPaint.setColor(0xFF0000);
Bitmap bitmap = Bitmap.createBitmap(/* read the docs*/);
Canvas mCanvas = new Canvas(bitmap);
mCanvas.drawCircle(x,y,r,mPaint);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
To be perhaps somewhat pedantic (and hopefully increase your understanding), a Canvas just hosts the "draw" calls and draws into a Bitmap that you specify. This means:
Your example code doesn't do much, since you didn't construct your Canvas with a Bitmap or call setBitmap() on it.
You're not converting your Canvas to a Drawable, you're constructing a Drawable from the Bitmap your Canvas calls draw into.
Taken from another post, here is the psuedo code to do this.
Image on canvas to JPEG file
ByteArrayOutputStream baos = new ByteArrayOutputStream()
Bitmap bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// This converts the bitmap to a drawable
BitmapDrawable mDrawable = new BitmapDrawable(getResources(),bitmap);
Alternately, you could use getDrawingCache() as outlined in another answer of that thread.

Canvas.drawBitmap() ignoring the tile mode of the paint

I want to draw subparts of a bitmap, but at a different size. If the size is bigger than the source rectangle in the bitmap, then I want that section of the bitmap to tile to fill the destination area. However, instead of getting tiled they are getting stretched.
I set up all the variables as follows:
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Rect srcRect = ...
Rect dstRect = ...
Paint p = new Paint();
p.setShader(new BitmapShader(b, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
And then in the draw() method I draw as follows:
canvas.drawBitmap(b, srcRect, dstRect, p);
What am I doing wrong? How should I draw srcRect to dstRect such that my subpart of the bitmap gets tiled?
I discovered the problem, put succinctly: shaders on bitmaps don't work that way.
To draw a rectangle tiled with a specific Bitmap you have to use Canvas.drawRect(), with a Paint that has a BitmapShader. However, Android dev can't ever be as simple as that.
First you have to cut out the srcRect to a separate Bitmap (caching this somewhere since I don't think this is a cheap operation), like so:
Bitmap t = Bitmap.createBitmap(b, srcRect.left, srcRect.top, srcRect.right-srcRect.left, srcRect.bottom-srcRect.top);
Then you have to create the Paint and the BitmapShader:
BitmapShader bs = new BitmapShader(t, TileMode.REPEAT, TileMode.REPEAT);
Paint p = new Paint();
p.setShader(bs);
Then you can finally draw to the destination rectangle, but first you have to set up a translation matrix for the shader or else it won't start from the correct place and might bugger up completely if your tile mode is CLAMP:
Matrix m = new Matrix();
m.postTranslate(dstRect.left, dstRect.right);
p.getShader().setMatrix(m);
canvas.drawRect(dstRect, p);

Categories

Resources