帆布擦除位code机器人 [英] canvas erase bitmap code android

查看:161
本文介绍了帆布擦除位code机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用低于code到擦除触摸事件从帆布位。

I am using below code to erase bitmap from canvas on touch event.

它的正常工作中的Andr​​oid 2.3,但4.0它告诉我,而不是删除位图黑点。

it's working fine in android 2.3 but in 4.0 it show me black spot instead of erasing bitmap.

任何人都可以提供帮助。

anyone can help.

公共类DemoTouch延伸活动{

public class DemoTouch extends Activity {

@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 ,我还申请了擦除功能。直到我申请低于code中的if语句里面我有同样的问题。能解决问题3.0及以上

Have a look at this method of mine. The view EditImageView is a custom ImageView that I also applied that erase function to. I had the same problem until I applied the code below inside the if statement. That fixes the problem for 3.0 and up

/**
 * 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);
}

修改

您也许可以做这样的事情在你的code。

You can probably do something like this in your code.

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);

}

希望这可以帮助你。

Hope this also helps you

这篇关于帆布擦除位code机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆