Android PorterDuff没有按预期工作

时间:2017-07-13 11:35:05

标签: android porter-duff

我正在尝试使用Android的PorterDuff绘制阴影,只会出现在它下面有东西的地方(不是背景图像透明的地方),我很难让它工作。我编写了以下代码来测试各种PorterDuff.Modes,它们似乎没有像这里描述的那样工作:

https://developer.android.com/reference/android/graphics/PorterDuff.Mode.html

从那个页面,我会说我正在寻找的效果是SRC_ATOP。

这是我正在使用的代码,下面是结果的图像:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.v4.content.ContextCompat;
import android.view.View;

public class PorterDuffTest extends View {
    private int mTexWidth;
    private int mTexHeight;
    private Bitmap dstBmp;

    PorterDuffTest(Context c) {
        super(c);

        dstBmp = BitmapFactory.decodeResource(c.getResources(), R.drawable.src);
        mTexWidth = dstBmp.getWidth();
        mTexHeight = dstBmp.getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawRGB(255, 255, 255);

        canvas.save();
        canvas.translate(40.0f, 40.0f);

            //COLUMN 1
        drawBlendedBitmap(canvas, PorterDuff.Mode.DARKEN);
        drawBlendedBitmap(canvas, PorterDuff.Mode.LIGHTEN);
        drawBlendedBitmap(canvas, PorterDuff.Mode.OVERLAY);
        drawBlendedBitmap(canvas, PorterDuff.Mode.ADD);

        canvas.restore();

        canvas.save();
        canvas.translate(40.0f + mTexWidth + 40.0f, 40.0f);

            //COLUMN 2
        drawBlendedBitmap(canvas, PorterDuff.Mode.CLEAR);
        drawBlendedBitmap(canvas, PorterDuff.Mode.MULTIPLY);
        drawBlendedBitmap(canvas, PorterDuff.Mode.SCREEN);
        drawBlendedBitmap(canvas, PorterDuff.Mode.XOR);
        drawBlendedBitmap(canvas, PorterDuff.Mode.OVERLAY);

        canvas.restore();

        canvas.save();
        canvas.translate(40.0f + mTexWidth + 40.0f + mTexWidth + 40.0f, 40.0f);

            //COLUMN 3
        drawBlendedBitmap(canvas, PorterDuff.Mode.SRC);
        drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_OUT);
        drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_IN);
        drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_ATOP);
        drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_OVER);
        canvas.restore();

        canvas.save();
        canvas.translate(40.0f + mTexWidth + 40.0f + mTexWidth + 40.0f + mTexWidth + 40.0f, 40.0f);

            //COLUMN 4
        drawBlendedBitmap(canvas, PorterDuff.Mode.DST);
        drawBlendedBitmap(canvas, PorterDuff.Mode.DST_IN);
        drawBlendedBitmap(canvas, PorterDuff.Mode.DST_OUT);
        drawBlendedBitmap(canvas, PorterDuff.Mode.DST_ATOP);
        drawBlendedBitmap(canvas, PorterDuff.Mode.DST_OVER);

        canvas.restore();
    }

    private void drawBlendedBitmap(Canvas canvas, PorterDuff.Mode mode) {
        Paint paint = new Paint();
        canvas.drawBitmap(dstBmp, 0.0f, 0.0f, paint);

        paint.setXfermode(new PorterDuffXfermode(mode));
        paint.setColor(ContextCompat.getColor(getContext(), R.color.tileBlue));
        canvas.drawRect(15, 15, 135, 135, paint);

        canvas.translate(0.0f, 40.0f + mTexHeight);
    }
}

PorterDuff result

我现在已经玩了好几个小时了,我真的很难取得任何进展,所以任何帮助都会非常感激。提前谢谢!

0 个答案:

没有答案