【问题标题】:Applying overlay to scaled bitmap - issues with scaling overlay将叠加应用于缩放位图 - 缩放叠加问题
【发布时间】:2012-04-22 20:28:39
【问题描述】:

我正在从 SD 卡加载一个带有 jpg 的图像视图,并对其进行缩放,以便在不剪裁的情况下最大程度地放大它。 然后我想用一个圆圈覆盖位图,以触摸事件的坐标为中心。

我从 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;
    }

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

开始-我要点击鱼:

第一次点击——点击准确,缩放丢失:

第二次点击 - 缩放重新出现,但点击应用于原始缩放,所以它关闭了:

第三次和所有后续点击 - 从一开始就按我希望的那样工作:

感谢您的帮助!

【问题讨论】:

    标签: android matrix overlay scale


    【解决方案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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2015-06-16
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      相关资源
      最近更新 更多