使用PorterDuff.Mode.SRC_ATOP将Drawable放在Imageview上

时间:2018-03-22 15:04:49

标签: java android

我有一个ImageView:

ImageView imageView = findViewById(R.id.imageView);

我想实现这样的目标( Atop 图) enter image description here

drawable1是原始的ImageView图像,drawable2是我要放置的图像:

Drawable drawable1 =  AppCompatResources.getDrawable(context, drawable1);
Drawable drawable2 = AppCompatResources.getDrawable(context, drawable2);

LD layerDrawable = new LD(new Drawable[]{
       drawable1,
       drawable2,
});
imageView.setImageDrawable(layerDrawable);

LD课程:

public class LD extends LayerDrawable
{
    private Paint p = new Paint();

    public LD(Drawable[] layers) {
        super(layers);
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        int count = canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);

        for (int i = 0, numberOfLayers = getNumberOfLayers(); i < numberOfLayers; ++i) {
            this.getDrawable(i).draw(canvas);
            canvas.saveLayer(null, p, Canvas.ALL_SAVE_FLAG);
        }
        canvas.restoreToCount(count);

    }
}

然而这并不起作用,因为我放置的图像(drawable2)放在整个imageView上,而不仅仅放在drawable1图像的非透明内容上。

如何实现Atop数字(仅将drawable2放在&lt; fillable1&gt;的drawable1上)?

编辑:

p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(bitmap1, 0, 0, null);
canvas.drawBitmap(bitmap2, 0, 0, p);

0 个答案:

没有答案