I have a drawable that I have resized to the appropriate desired height (25dp), and the width is scaled proportionally. However, I want an image that is square, with a width of 25dp as well. However, I do not want to distort the image, so I am looking to to crop equal portions of the right and left of the drawable to give me the center 25dpx25dp. Is this possible? How? I have been trying for the past two hours and I'm about ready to throw in the towel...
Asked
Active
Viewed 4,241 times
1 Answers
3
Bitmap target= Bitmap.createBitmap(width, height,Config.ARGB_8888);
Canvas canvas = new Canvas(target)
float hScale = width/(float)source.getWidth();
float vScale = height/(float)source.getHeight();
float scale = Math.min(hScale, vScale);
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
matrix.postTranslate(width/2 - source.getWidth()/2 * scale, height/2 - source.getHeight()/2 * scale);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(source, matrix, new Paint());

Sudar Nimalan
- 3,962
- 2
- 20
- 28
-
i'm very very new to drawing on canvases.. how to i add this to my layout once its drawn? – user1118042 Feb 13 '12 at 03:02
-
if you are using ImageView then imageView.setImageBitmap(target);http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap) – Sudar Nimalan Feb 13 '12 at 03:34
-
can i add it to a textview as a compound drawable somehow? and thanks so much for the help you've already given me! – user1118042 Feb 13 '12 at 04:01
-
If you want to use for setBackgroundDrawable(), you can use setBackgroundDrawable(new BitmapDrawable(target)); – Sudar Nimalan Feb 13 '12 at 04:24
-
i also try your code but i can`t draw the canvas what is the problem – Zala Janaksinh Aug 07 '12 at 06:17
-
i also do this. but i can`t work.i post my question here http://stackoverflow.com/questions/11840811/how-to-get-select-area-which-cover-by-canvas-in-android but i can`t do this.please give me some idea to how to slove this – Zala Janaksinh Aug 07 '12 at 09:05