使用 PorterDuff 模式擦除位图部分

发布于 2024-09-14 05:13:22 字数 1817 浏览 1 评论 0原文

我尝试使用 Porter-Duff Xfermodes 擦除 Android 应用程序中的部分位图。

我有一个绿色背景,上面覆盖着蓝色位图。当我触摸屏幕时,应该在重叠位图中创建一个“洞”,使绿色背景可见。我当前的代码产生的不是一个洞,而是一个黑点。

下面是我的代码。有什么想法吗,我在这里做错了什么?

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(new DrawView(this));
}

public class DrawView extends View implements OnTouchListener {

    private int x = 0;
    private int y = 0;

    Bitmap bitmap;
    Canvas bitmapCanvas;

    private final Paint paint = new Paint();
    private final Paint eraserPaint = new Paint();

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        // Set background
        this.setBackgroundColor(Color.GREEN);

        // Set bitmap
        bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.RGB_565);
        bitmapCanvas = new Canvas();
        bitmapCanvas.setBitmap(bitmap);
        bitmapCanvas.drawColor(Color.BLUE);

        // Set eraser paint properties
        eraserPaint.setAlpha(0);
        eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        eraserPaint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        bitmapCanvas.drawColor(Color.BLUE);
        bitmapCanvas.drawCircle(x, y, 10, eraserPaint);

        canvas.drawBitmap(bitmap, 0, 0, paint);
    }

    public boolean onTouch(View view, MotionEvent event) {
        x = (int) event.getX();
        y = (int) event.getY();

        invalidate();
        return true;
    }

}

I try to erase parts of a bitmap in my Android application by using Porter-Duff Xfermodes.

I have a green background which is overlayed by a blue bitmap. When I touch the screen a "hole" in the overlaying bitmap is supposed to be created making the green background visible. Instead of a hole my current code produces a black dot.

Below is my code. Any ideas, what I am doing wrong here?

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(new DrawView(this));
}

public class DrawView extends View implements OnTouchListener {

    private int x = 0;
    private int y = 0;

    Bitmap bitmap;
    Canvas bitmapCanvas;

    private final Paint paint = new Paint();
    private final Paint eraserPaint = new Paint();

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        // Set background
        this.setBackgroundColor(Color.GREEN);

        // Set bitmap
        bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.RGB_565);
        bitmapCanvas = new Canvas();
        bitmapCanvas.setBitmap(bitmap);
        bitmapCanvas.drawColor(Color.BLUE);

        // Set eraser paint properties
        eraserPaint.setAlpha(0);
        eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        eraserPaint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        bitmapCanvas.drawColor(Color.BLUE);
        bitmapCanvas.drawCircle(x, y, 10, eraserPaint);

        canvas.drawBitmap(bitmap, 0, 0, paint);
    }

    public boolean onTouch(View view, MotionEvent event) {
        x = (int) event.getX();
        y = (int) event.getY();

        invalidate();
        return true;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-09-21 05:13:22

这是工作代码...可能对某人有帮助

public class ImageDemo extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new Panel(this));   
    }

    class Panel extends View {

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mPaint;

        Bitmap bitmap;
        Canvas pcanvas;

        int x = 0;
        int y =0;
        int r =0;

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

            Log.v("Panel", ">>>>>>");

            setFocusable(true);
            setBackgroundColor(Color.GREEN);

            // setting paint 
            mPaint = new Paint();
            mPaint.setAlpha(0);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
            mPaint.setAntiAlias(true);

            // getting image from resources
            Resources r = this.getContext().getResources();

            Bitmap bm = BitmapFactory.decodeResource(getResources(),  R.drawable.mickey);

            // converting image bitmap into mutable bitmap  
            bitmap =  bm.createBitmap(295, 260, Config.ARGB_8888);
            pcanvas = new Canvas();
            pcanvas.setBitmap(bitmap);    // drawXY will result on that Bitmap
            pcanvas.drawBitmap(bm, 0, 0, null);           
        }

        @Override
        protected void onDraw(Canvas canvas) {  
            // draw a circle that is  erasing bitmap            
            pcanvas.drawCircle(x, y, r, mPaint);

            canvas.drawBitmap(bitmap, 0, 0,null);

            super.onDraw(canvas);
        }       

        @Override
        public boolean onTouchEvent(MotionEvent event) {        
             // set parameter to draw circle on touch event
             x = (int) event.getX();
             y = (int) event.getY();

             r =20;
             // At last invalidate canvas
             invalidate();
             return true;
        }
    }
}

Here is working code... may help somebody

public class ImageDemo extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new Panel(this));   
    }

    class Panel extends View {

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mPaint;

        Bitmap bitmap;
        Canvas pcanvas;

        int x = 0;
        int y =0;
        int r =0;

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

            Log.v("Panel", ">>>>>>");

            setFocusable(true);
            setBackgroundColor(Color.GREEN);

            // setting paint 
            mPaint = new Paint();
            mPaint.setAlpha(0);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
            mPaint.setAntiAlias(true);

            // getting image from resources
            Resources r = this.getContext().getResources();

            Bitmap bm = BitmapFactory.decodeResource(getResources(),  R.drawable.mickey);

            // converting image bitmap into mutable bitmap  
            bitmap =  bm.createBitmap(295, 260, Config.ARGB_8888);
            pcanvas = new Canvas();
            pcanvas.setBitmap(bitmap);    // drawXY will result on that Bitmap
            pcanvas.drawBitmap(bm, 0, 0, null);           
        }

        @Override
        protected void onDraw(Canvas canvas) {  
            // draw a circle that is  erasing bitmap            
            pcanvas.drawCircle(x, y, r, mPaint);

            canvas.drawBitmap(bitmap, 0, 0,null);

            super.onDraw(canvas);
        }       

        @Override
        public boolean onTouchEvent(MotionEvent event) {        
             // set parameter to draw circle on touch event
             x = (int) event.getX();
             y = (int) event.getY();

             r =20;
             // At last invalidate canvas
             invalidate();
             return true;
        }
    }
}
兮子 2024-09-21 05:13:22

首先想到的是,我不确定在擦除绘画对象上将 alpha 设置为 0 是否是一个好主意。这可能会让整个事情变得无效。

另外,如果您正在处理 Alpha,则应始终使用 Bitmap.Config.ARGB_8888。

不过,如果您在使用 PorterDuff 方面遇到麻烦,我建议简化您的方法,仅(暂时)这样做。这将帮助您缩小不起作用的部分的范围。注释掉与触摸和视图更新有关的所有内容。

然后你就可以找出绘图中不正确的部分。像这样设置你的构造函数:

DrawView()
{
    /* Create the background green bitmap */
    ...

    /* Create foreground transparent bitmap */
    ...

    /* Draw a blue circle on the foreground bitmap */
    ...

    /* Apply the foreground to the background bitmap
       using a PorterDuff method */
    ...
}

onDraw()
{
    /* Simply draw the background bitmap */
    ...
}

如果你像这样设置,你应该能够知道你的 PD 方法如何影响绿色位图,并相应地更改内容。

First thought, I'm not sure if setting alpha to 0 on your erase paint object is a good idea. That might make the whole thing ineffective.

Also, you should always use Bitmap.Config.ARGB_8888 if you're dealing with alphas.

If you're having trouble with the PorterDuff stuff, though, I would suggest simplifying your approach to ONLY do that (temporarily). That will help you narrow down the part which isn't working. Comment out everything to do with touch and view updates.

Then you can single out what part of the drawing isn't working right. Set up your constructor like this:

DrawView()
{
    /* Create the background green bitmap */
    ...

    /* Create foreground transparent bitmap */
    ...

    /* Draw a blue circle on the foreground bitmap */
    ...

    /* Apply the foreground to the background bitmap
       using a PorterDuff method */
    ...
}

onDraw()
{
    /* Simply draw the background bitmap */
    ...
}

If you set things up like that, you should be able to tell how your PD method is affecting the green bitmap, and change things accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文