Android 画布擦除位图代码

Android 画布擦除位图代码,android,canvas,bitmap,Android,Canvas,Bitmap,我正在使用下面的代码在触摸事件中从画布上擦除位图 它在android 2.3中运行良好,但在4.0中,它显示的是黑点,而不是删除位图 任何人都可以帮忙 公共类Demouch扩展活动{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new TouchView(this)); } class TouchVi

我正在使用下面的代码在触摸事件中从画布上擦除位图

它在android 2.3中运行良好,但在4.0中,它显示的是黑点,而不是删除位图

任何人都可以帮忙

公共类Demouch扩展活动{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new TouchView(this));

}

class TouchView extends View {
    Bitmap bgr;
    Bitmap overlayDefault;
    Bitmap overlay;
    Paint pTouch;
    int X = -100;
    int Y = -100;
    Canvas c2;

    public TouchView(Context context) {
        super(context);

        bgr = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
        overlayDefault = BitmapFactory.decodeResource(getResources(),
                R.drawable.a2);
        overlay = BitmapFactory.decodeResource(getResources(),
                R.drawable.a2).copy(Config.ARGB_4444, true);
        c2 = new Canvas(overlay);

        pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
        pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        pTouch.setColor(Color.TRANSPARENT);
        pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
        pTouch.setAntiAlias(true);

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

        case MotionEvent.ACTION_DOWN: {

            X = (int) ev.getX();
            Y = (int) ev.getY();
            invalidate();

            break;
        }

        case MotionEvent.ACTION_MOVE: {

            X = (int) ev.getX();
            Y = (int) ev.getY();
            invalidate();
            break;

        }

        case MotionEvent.ACTION_UP:

            break;

        }
        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // draw background
        canvas.drawBitmap(bgr, 0, 0, null);
        // copy the default overlay into temporary overlay and punch a hole
        // in it
        // c2.drawBitmap(overlayDefault, 0, 0, null); // exclude this line
        // to
        // show all as you draw
        c2.drawCircle(X, Y, 20, pTouch);
        // draw the overlay over the background
        canvas.drawBitmap(overlay, 0, 0, null);

    }

}

}看看我的这种方法。视图
EditImageView
是一个自定义的
ImageView
,我也对其应用了擦除功能。我也有同样的问题,直到我在if语句中应用了下面的代码。这解决了3.0及更高版本的问题

/**
 * Sets the image to the view in the content view
 * @param view
 */
private void setImageView(EditImageView view) {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(params);

    // #### THIS IS THE IMPORTANT PART ######
    if (Build.VERSION.SDK_INT >= 11) {
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    LinearLayout main = (LinearLayout) findViewById(R.id.galleryImage);
    main.removeAllViews();
    main.addView(view);
}
编辑

您可能可以在代码中执行类似的操作

public TouchView(Context context) {
    super(context);

    // This should fix it from 3.0 and up
    if (Build.VERSION.SDK_INT >= 11) {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    bgr = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
    overlayDefault = BitmapFactory.decodeResource(getResources(),
            R.drawable.a2);
    overlay = BitmapFactory.decodeResource(getResources(),
            R.drawable.a2).copy(Config.ARGB_4444, true);
    c2 = new Canvas(overlay);

    pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
    pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    pTouch.setColor(Color.TRANSPARENT);
    pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
    pTouch.setAntiAlias(true);

}

希望这也能帮助你

/############if(Build.VERSION.SDK_INT>=11){view.setLayerType(view.LAYER(TYPE)SOFTWARE,null)}添加这行后,它会显示一个白点。你的视图后面是什么?如果它后面没有任何东西,它就不会显示任何东西。后面有另一张图片。谢谢!工作起来很有魅力。只要看看下面的代码,下面的代码就会对你有用