2

I am trying to build a Magnify tool in my Android app. For this, I have an ImageView, which I converted to Bitmap (with some zoom/scale factor).

imageView.setDrawingCacheEnabled(true);
Bitmap drawingCache = imageView.getDrawingCache(true);
Matrix matrix = new Matrix();
matrix.postScale(5, 5);        

Bitmap viewCapture = Bitmap.createBitmap(drawingCache, 0, 0, 
                                         drawingCache.getWidth(),
                                         drawingCache.getHeight(), 
                                         matrix, true);
imageView.setDrawingCacheEnabled(false);

Now, I am drawing this Bitmap image "viewCapture" to my canvas. Here, I want only portion of the image to be rendered on the canvas.

I tried using approaches: "setRectToRect() on Matrix", "canvas.drawBitmap(bitmap, src, dst, paint)". But, didn't work out appropriately.

Would using SurfaceViews be helpful? Has anyone come across this situation? Please post your thoughts/ideas.

user1094717
  • 31
  • 1
  • 4

2 Answers2

3

Why not just use the following?

 Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

src corresponds to which region of the bitmap you want to draw and dst says where you want the bitmap drawn. You can read more about it here: http://developer.android.com/reference/android/graphics/Canvas.html

slayton
  • 20,123
  • 10
  • 60
  • 89
  • When I do that, I get a smaller image of the bitmap, instead of cropped image. – user1094717 Dec 16 '11 at 16:52
  • Actually, I am scaling the image before creating the Bitmap. And, when I do use this method, I lose the associated density. http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap%28android.graphics.Bitmap,%20android.graphics.Rect,%20android.graphics.RectF,%20android.graphics.Paint%29 – user1094717 Dec 16 '11 at 22:31
0

When I do that, I get a smaller image of the bitmap, instead of cropped image. Following is the snippet I have used.

            Canvas c = holder.lockCanvas();
            c.drawRGB(10, 10, 10);              
            Rect src = new Rect((int)x - _image.getWidth(), (int) y - _image.getHeight(), (int)x + _image.getWidth(), (int) y + _image.getHeight());
            RectF dst = new RectF((int)x - 50, (int) y - 50, (int)x + 50, (int) y + 50);
            c.drawBitmap(_image, src, dst, null);
            holder.unlockCanvasAndPost(c);
user1094717
  • 31
  • 1
  • 4