PorterDuffXfermode之Mode.SRC_IN

Posted loaderman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PorterDuffXfermode之Mode.SRC_IN相关的知识,希望对你有一定的参考价值。

package com.loaderman.customviewdemo.view;

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.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.loaderman.customviewdemo.R;


public class RoundImageView_SRCIN extends View {
    private Paint mBitPaint;
    private Bitmap BmpDST,BmpSRC;

    public RoundImageView_SRCIN(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mBitPaint = new Paint();
        BmpDST = BitmapFactory.decodeResource(getResources(), R.drawable.dog_shade, null);
        BmpSRC = BitmapFactory.decodeResource(getResources(),R.drawable.dog,null);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth()/2;
        int height = width * BmpDST.getHeight()/BmpDST.getWidth();

        canvas.save();

        canvas.drawBitmap(BmpDST,null,new RectF(0,0,width,height),mBitPaint);
        mBitPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(BmpSRC,null,new RectF(0,0,width,height),mBitPaint);

        mBitPaint.setXfermode(null);
        canvas.restore();
    }
}
   <com.loaderman.customviewdemo.view.RoundImageView_SRCIN
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

效果:

技术分享图片

以上是关于PorterDuffXfermode之Mode.SRC_IN的主要内容,如果未能解决你的问题,请参考以下文章

PorterDuffXfermode之Mode.SRC_IN

PorterDuffXfermode之PorterDuff.Mode.DST_IN

Android Paint之 setXfermode PorterDuffXfermode 讲解

关于PorterDuffXferMode的用法

利用PorterDuffXfermode绘制图片文字

Android绘图机制与处理技巧——Android图像处理之画笔特效处理