将叠加应用于缩放位图 - 缩放叠加问题

时间:2012-04-22 20:28:40

标签: android matrix overlay scale

我正在从SD卡加载带有jpg的imageview,并对其进行缩放以使其最大程度地放大而不会剪切。 然后我想用圆圈覆盖位图,以触摸事件的坐标为中心。

我从imageview保存当前位图,创建新的叠加位图,添加画布,绘制原始位图,绘制圆,重新应用缩放代码到新的叠加位图,然后使用新的叠加位图重新加载imageview。我第一次触摸图像绘制圆圈时,圆圈被精确绘制,但图像缩放不正确。在第二次触摸时,图像缩放被校正,但是圆圈被绘制在“错误”的位置 - 它被绘制在我触摸屏幕的位置,但现在图像被正确缩放,因此目标已经移动。在第三次以及随后的所有接触中,事情就像我希望他们从一开始就能发挥作用。

这是我的代码:

private static Matrix curMatrix = new Matrix();

public boolean onTouch(View v, MotionEvent event) {
    int eventType = event.getAction() & MotionEvent.ACTION_MASK;
    switch (eventType) {
        case MotionEvent.ACTION_UP:
            ImageView i = (ImageView) v;        

            //Save the current bitmap
            i.buildDrawingCache();
            Bitmap bm = i.getDrawingCache();

            //Create new overlay bitmap
            Bitmap bmOverlay = Bitmap.createBitmap(i.getWidth(), i.getHeight(), Bitmap.Config.ARGB_8888);

            //Create new drawing canvas and paint
            Canvas c = new Canvas(bmOverlay);
            Paint p = new Paint();
            p.setColor(Color.RED);
            p.setAlpha(50);

            //Draw the saved bitmap onto the canvas
            c.drawBitmap(bm, new Matrix(), null);

            //Draw a circle on the current canvas, centered on event coordinates
            c.drawCircle(event.getX(), event.getY(), 100F, p);

            //Autosize canvas to previous imageview settings
            RectF drawableRect = new RectF(0, 0, (float) c.getWidth(), (float) c.getHeight());
            RectF viewRect = new RectF(0, 0, (float) i.getWidth(), (float) i.getHeight());
            curMatrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);

            //Apply the autosize transformation
            i.setImageMatrix(curMatrix);        

            //Reload the imageview with the new bitmap
            i.setImageBitmap(bmOverlay);
            }
            return true;
    }

这里有一些图片可以更好地解释发生的事情:

开始 - 我想点击鱼:
Start

首先点击 - 点击是准确的,缩放比例丢失:
First Click

第二次点击 - 缩放再次出现,点击应用于原始缩放,因此它关闭:
Second Click

第三次以及随后的所有点击 - 从我希望从一开始就起作用:
Third Click

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

像往常一样,我过度复杂化了。对于那些感兴趣的人,以下更简单的代码可行。只需将其设置为ImageView的TouchListener,当您触摸图像时,它将以触摸点为中心绘制一个100(像素?)半径的半透明红色圆圈:

public class GetCoordinatesTouchListener implements OnTouchListener {
    public boolean onTouch(View v, MotionEvent event) {
        int eventType = event.getAction() & MotionEvent.ACTION_MASK;
        switch (eventType) {
            case MotionEvent.ACTION_UP:
                ImageView i = (ImageView) v;
                //Save the current bitmap from the imageview
                i.buildDrawingCache();
                Bitmap bm = i.getDrawingCache();
                //Create new overlay bitmap
                Bitmap bmOverlay = Bitmap.createBitmap(i.getWidth(), i.getHeight(), Bitmap.Config.ARGB_8888);
                //Create new drawing canvas and paint
                Canvas c = new Canvas(bmOverlay);
                Paint p = new Paint();
                p.setColor(Color.RED);
                p.setAlpha(50);
                //Draw the saved bitmap onto the canvas
                c.drawBitmap(bm, new Matrix(), null);
                //Draw a circle on the current canvas, centered on event coordinates
                c.drawCircle(event.getX(), event.getY(), 100F, p);
                //Reload the imageview with the new bitmap with FIT_XY scaling
                i.setScaleType(ImageView.ScaleType.FIT_XY);
                i.setImageBitmap(bmOverlay);
            }
        return true;
    }
}