使用PorterDuff模式擦除位图件 [英] Erase bitmap parts using PorterDuff mode

查看:145
本文介绍了使用PorterDuff模式擦除位图件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用波特 - 达夫Xfermodes擦除位图的部分在我的Andr​​oid应用程序。

我有一个绿色的背景是由一个蓝色的位图叠加。当我触摸屏幕的位图覆盖漏洞应该是使创建绿色背景可见。而不是一个洞我目前的code产生一个​​黑点。

下面是我的code。任何想法,我做什么错在这里?

  / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    的setContentView(新drawView函数(本));
}

公共类drawView函数扩展视图实现OnTouchListener {

    私人INT X = 0;
    私人诠释Y = 0;

    点阵位图;
    帆布bitmapCanvas;

    私人最终涂料粉刷=新的油漆();
    私人最终涂料eraserPaint =新的油漆();

    公共drawView函数(上下文的背景下){
        超(上下文);
        setFocusable(真正的);
        setFocusableInTouchMode(真正的);

        this.setOnTouchListener(本);

        //设置背景
        this.setBackgroundColor(Color.GREEN);

        //设置位图
        位= Bitmap.createBitmap(320,480,Bitmap.Config.RGB_565);
        bitmapCanvas =新的Canvas();
        bitmapCanvas.setBitmap(位);
        bitmapCanvas.drawColor(Color.BLUE);

        //设置橡皮擦的涂料性能
        eraserPaint.setAlpha(0);
        eraserPaint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        eraserPaint.setAntiAlias​​(真正的);
    }

    @覆盖
    公共无效的OnDraw(帆布油画){
        bitmapCanvas.drawColor(Color.BLUE);
        bitmapCanvas.drawCircle(X,Y,10,eraserPaint);

        canvas.drawBitmap(位图,0,0,油漆);
    }

    公共布尔onTouch(查看视图,MotionEvent事件){
        X =(int)的event.getX();
        Y =(INT)event.getY();

        无效();
        返回true;
    }

}
 

解决方案

首先想到的,我不知道是否设置阿尔法0您擦漆的对象是一个好主意。这可能使整个事情是无效的。

此外,你应该总是使用Bitmap.Config.ARGB_8888,如果你处理的阿尔法。

如果您在使用PorterDuff东西麻烦,不过,我会建议简化你的方法,只做到这一点(临时)。这将帮助你缩小其无法正常工作的一部分。注释掉一切都与触摸和查看更新。

然后你可以挑出什么样的绘图部分没有工作的权利。设置你的构造是这样的:

  drawView函数()
{
    / *创建绿色背景位图* /
    ...

    / *创建前景透明位图* /
    ...

    / *绘制一个蓝色圆圈上的前景位图* /
    ...

    / *应用前景到背景位图
       使用PorterDuff方法* /
    ...
}

的OnDraw()
{
    / *简单地画背景位图* /
    ...
}
 

如果您设置的东西了这样,你应该能够告诉你如何PD方法影响绿色的位图,并相应地改变的东西。

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

}

解决方案

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.

这篇关于使用PorterDuff模式擦除位图件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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