如何在Android中用手指擦除图像

如何在Android中用手指擦除图像,第1张

概述我正在开发一个需要对象功能的应用程序.我有一个覆盖图像B的图像A.用手指我需要擦除图像A以显示图像B.擦除必须遵循手指流动的图像A.我正在尝试一些代码但仍然无法擦除图像A.这是我用于在图像上绘制一条线的代码(_imageToErase是图像A):Canvas canvas; Paint paint; float downx = 0, downy = 0, u

我正在开发一个需要对象功能的应用程序.

我有一个覆盖图像B的图像A.用手指我需要擦除图像A以显示图像B.
擦除必须遵循手指流动的图像A.

我正在尝试一些代码但仍然无法擦除图像A.这是我用于在图像上绘制一条线的代码(_imagetoErase是图像A):

Canvas canvas;Paint paint;float downx = 0,downy = 0,upx = 0,upy = 0;ImageVIEw _imagetoErase;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.g_layout);    _imagetoErase = (ImageVIEw) findVIEwByID(R.ID.image_to_erase);    _imagetoErase.setontouchListener(this);}@OverrIDepublic voID onWindowFocusChanged(boolean hasFocus){    int wIDth = _imagetoErase.getWIDth();    int height = _imagetoErase.getHeight();    Bitmap bitmap = Bitmap.createBitmap(wIDth,height,Bitmap.Config.ARGB_8888);    canvas = new Canvas(bitmap);    paint = new Paint();    paint.setcolor(color.WHITE);    paint.setstrokeWIDth(25);    paint.setAntiAlias(true);    _imagetoErase.setimageBitmap(bitmap);}public boolean ontouch(VIEw v,MotionEvent event) {    int action = event.getAction();    switch (action){        case MotionEvent.ACTION_DOWN:            downx = event.getX();            downy = event.getY();            break;        case MotionEvent.ACTION_MOVE:            upx = event.getX();            upy = event.getY();            canvas.drawline(downx,downy,upx,upy,paint);            _imagetoErase.invalIDate();            downx = upx;            downy = upy;            break;        case MotionEvent.ACTION_UP:            break;        case MotionEvent.ACTION_CANCEL:            break;        default:            break;    }    return true;}

此代码仅产生跟随手指但不擦除图像的线.

如何修改此代码以删除图像?谢谢

编辑

评论中建议的链接没有解决我的问题.只需添加以下行:

 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

不适合我.最佳答案PaintVIEw.java

public class PaintVIEw extends VIEw implements VIEw.OntouchListener {    private static final String TAG = "PaintVIEw";    Bitmap Bitmap1,Bitmap2;    Bitmap transparent;    int X = -100;    int Y = -100;    Canvas c2;    private boolean istouched = false;    Paint paint = new Paint();    Path drawPath = new Path();public PaintVIEw(Context context) {    super(context);    setFocusable(true);    setFocusableIntouchMode(true);    this.setontouchListener(this);    displayMetrics metrics = context.getResources().getdisplayMetrics();    int wIDth = metrics.wIDthPixels;    int height = metrics.heightPixels;    transparent = Bitmap.createBitmap(wIDth,Bitmap.Config.ARGB_8888);    Bitmap1 = BitmapFactory.decodeResource(getResources(),R.drawable.cake1);    Bitmap2 = BitmapFactory.decodeResource(getResources(),R.drawable.cake2);    c2 = new Canvas();    c2.setBitmap(transparent);    c2.drawBitmap(Bitmap2,paint);    paint.setAlpha(0);    paint.setStyle(Paint.Style.stroke);    paint.setstrokeJoin(Paint.Join.ROUND);    paint.setstrokeCap(Paint.Cap.ROUND);    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));    paint.setAntiAlias(true);}private static Point getdisplaySize(final display display) {    final Point point = new Point();    point.x = display.getWIDth();    point.y = display.getHeight();    return point;}@OverrIDeprotected voID onDraw(Canvas canvas) {    System.out.println("onDraw");    if(istouched)    {        canvas.drawBitmap(Bitmap1,null);    }    canvas.drawBitmap(transparent,null);}@OverrIDepublic boolean ontouch(VIEw v,MotionEvent event) {    istouched = true;    X = (int) event.getX();    Y = (int) event.getY();    paint.setstrokeWIDth(60);    switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:            drawPath.moveto(X,Y);            c2.drawPath(drawPath,paint);            break;        case MotionEvent.ACTION_MOVE:            drawPath.lineto(X,paint);            break;        case MotionEvent.ACTION_UP:            drawPath.lineto(X,paint);            drawPath.reset();            count=0;            break;        default:            return false;    }    invalIDate();    return true;}}class Point {float x,y;@OverrIDepublic String toString() {    return x + "," + y;}}

MainActivity.java

public class MainActivity extends Activity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(new PaintVIEw(this));}}
总结

以上是内存溢出为你收集整理的如何在Android中用手指擦除图像全部内容,希望文章能够帮你解决如何在Android中用手指擦除图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1140715.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存