在图像视图中触摸时滞后钻取位图



我只是在创建一个需要钻取位图的应用程序,我也成功了,但是当我触摸图像视图时,位图钻取滞后。对此的任何帮助都非常感谢...谢谢。。

这是主文件

public class MainActivity extends Activity{
int x, y;
Bitmap background,foreground;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.img);
    background = BitmapFactory.decodeResource(getResources(), R.mipmap.mug_shot);
    foreground = BitmapFactory.decodeResource(getResources(), R.mipmap.poster);
    iv.setImageBitmap(combineTwoBitmaps(background, foreground));
    iv.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View view, MotionEvent event)
        {
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    x = (int) (event.getRawX()+100);
                    y = (int) (event.getY()+100);
                    System.out.println("X : "+ x +" Y : "+ y);
                    foreground = punchAHoleInABitmap(foreground);
                    iv.setImageBitmap(combineTwoBitmaps(background,foreground));
                    System.out.println("Hello this is Test2");
                    break;
                case MotionEvent.ACTION_CANCEL:
                    break;
                default:
                    break;
            }
            return true;
        }
    });
}
private Bitmap punchAHoleInABitmap(Bitmap foreground)
{
    System.out.println("Hello this is Tesyt");
    Bitmap bitmap = Bitmap.createBitmap(foreground.getWidth(), foreground.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(foreground, 0, 0, paint);
    paint.setAntiAlias(true);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    float radius = (float)(getScreenSize().x *.35);
    float x1 = (float) ((x*.5) + (radius * .5));
    float y1 = (float)  ((y*.5) + (radius * .5));
    System.out.println("X1 : "+ x1 +" Y1 : "+ y1);
    canvas.drawCircle(x1, y1, 50, paint);
    return bitmap;
}
private Bitmap combineTwoBitmaps(Bitmap background, Bitmap foreground) {
    Bitmap combinedBitmap = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
    Canvas canvas = new Canvas(combinedBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(background, 0, 0, paint);
    canvas.drawBitmap(foreground, 0, 0, paint);
    return combinedBitmap;
}
private Point getScreenSize() {
    WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display display = window.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size;
}

}

当用户

滑动屏幕时,ACTION_MOVE被多次调用。这意味着您将在短时间内创建大量位图。

使用GestureDetector您可以检测到精确的一张幻灯片/滑动/抛掷。

最新更新