Paint之setXfermode(图形混合模式)

PorterDuff.Mode表示混合模式,枚举值有18个,表示各种图形混合模式,有:


//DST相关模式
Mode.DST  
Mode.DST_OVER  
Mode.DST_IN  
Mode.DST_OUT  
Mode.DST_ATOP  

//SRC相关模式
Mode.SRC  
Mode.SRC_OVER  
Mode.SRC_IN  
Mode.SRC_OUT  
Mode.SRC_ATOP  

//颜色叠加相关模式
Mode.DARKEN  (变暗)
Mode.LIGHTEN  (变亮)
Mode.MULTIPLY  (正片叠底)
Mode.SCREEN  (滤色)
Mode.OVERLAY  (叠加)
Mode.ADD (饱和度相加)

//其它模式
Mode.CLEAR  
Mode.XOR  

上面每一个模式都对应着一个算法:

效果:

背景红色:

这里写图片描述

背景白色:

这里写图片描述

测试:

MyActivityI

public class MyActivityI extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        //黄色圆形:dst    蓝色矩形:src
        ((PorterDuffView) this.findViewById(R.id.view11)).setMode(PorterDuff.Mode.CLEAR);
        ((PorterDuffView) this.findViewById(R.id.view12)).setMode(PorterDuff.Mode.ADD);

        ((PorterDuffView) this.findViewById(R.id.view21)).setMode(PorterDuff.Mode.SRC);
        ((PorterDuffView) this.findViewById(R.id.view22)).setMode(PorterDuff.Mode.DST);

        ((PorterDuffView) this.findViewById(R.id.view31)).setMode(PorterDuff.Mode.SRC_OVER);
        ((PorterDuffView) this.findViewById(R.id.view32)).setMode(PorterDuff.Mode.DST_OVER);

        ((PorterDuffView) this.findViewById(R.id.view41)).setMode(PorterDuff.Mode.SRC_IN);
        ((PorterDuffView) this.findViewById(R.id.view42)).setMode(PorterDuff.Mode.DST_IN);

        ((PorterDuffView) this.findViewById(R.id.view51)).setMode(PorterDuff.Mode.SRC_OUT);
        ((PorterDuffView) this.findViewById(R.id.view52)).setMode(PorterDuff.Mode.DST_OUT);

        ((PorterDuffView) this.findViewById(R.id.view61)).setMode(PorterDuff.Mode.SRC_ATOP);
        ((PorterDuffView) this.findViewById(R.id.view62)).setMode(PorterDuff.Mode.DST_ATOP);

        ((PorterDuffView) this.findViewById(R.id.view71)).setMode(PorterDuff.Mode.XOR);
        ((PorterDuffView) this.findViewById(R.id.view72)).setMode(PorterDuff.Mode.OVERLAY);

        ((PorterDuffView) this.findViewById(R.id.view81)).setMode(PorterDuff.Mode.DARKEN);
        ((PorterDuffView) this.findViewById(R.id.view82)).setMode(PorterDuff.Mode.LIGHTEN);

        ((PorterDuffView) this.findViewById(R.id.view91)).setMode(PorterDuff.Mode.MULTIPLY);
        ((PorterDuffView) this.findViewById(R.id.view92)).setMode(PorterDuff.Mode.SCREEN);
    }

}

manifest中关闭硬件加速

android:hardwareAccelerated=”false”

 <activity
            android:name=".MyActivityI"
            android:hardwareAccelerated="false">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

定义PorterDuffView


/**
 * 1、关闭硬件加速
 * 2、使用图层(离屏绘制)
 */

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class PorterDuffView extends View {

    private int width = 100;
    private int height = 100;
    private Bitmap dstBmp;
    private Bitmap srcBmp;
    private Paint mPaint;

    public PorterDuffView(Context context, AttributeSet attrs) {
        super(context, attrs);
        dstBmp = makeDst(width, height);
        srcBmp = makeSrc(width, height);
        mPaint = new Paint();
    }

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


        int layerID = canvas.saveLayer(0, 0, width * 2, height * 2, mPaint, Canvas.ALL_SAVE_FLAG);

        canvas.drawBitmap(dstBmp, 0, 0, mPaint);//目标:黄色圆形

        mPaint.setXfermode(new PorterDuffXfermode(mode));
        canvas.drawBitmap(srcBmp, width / 2, height / 2, mPaint);//源:蓝色矩形

        mPaint.setXfermode(null);

        canvas.restoreToCount(layerID);

    }

    static Bitmap makeDst(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

        p.setColor(0xFFFFCC44);
        c.drawOval(new RectF(0, 0, w, h), p);
        return bm;
    }

    static Bitmap makeSrc(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

        p.setColor(0xFF66AAFF);
        c.drawRect(0, 0, w, h, p);
        return bm;
    }

    private PorterDuff.Mode mode = PorterDuff.Mode.CLEAR;

    public void setMode(PorterDuff.Mode mode) {
        this.mode = mode;
        invalidate();
    }

}

布局

把背景设置粉红色

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff4081">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1"
                android:text="CLEAR"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="ADD"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="SRC"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="DST"
                android:textSize="16sp" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.android.imooc.PorterDuffView
                android:id="@+id/view11"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view12"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view21"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view22"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />
        </LinearLayout>

        <!--000000000000000000000000000000000000000000000000000000000000000-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1"
                android:text="SRC_OVER"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="DST_OVER"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="SRC_IN"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="DST_IN"
                android:textSize="16sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.android.imooc.PorterDuffView
                android:id="@+id/view31"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view32"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />


            <com.android.imooc.PorterDuffView
                android:id="@+id/view41"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view42"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />
        </LinearLayout>

        <!--000000000000000000000000000000000000000000000000000-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1"
                android:text="SRC_OUT"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="DST_OUT"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="SRC_ATOP"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="DST_ATOP"
                android:textSize="16sp" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.android.imooc.PorterDuffView
                android:id="@+id/view51"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view52"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />


            <com.android.imooc.PorterDuffView
                android:id="@+id/view61"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view62"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

        </LinearLayout>

        <!--00000000000000000000000000000000000000000000000-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1"
                android:text="XOR"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="OVERLAY"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="DARKEN"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="LIGHTEN"
                android:textSize="16sp" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.android.imooc.PorterDuffView
                android:id="@+id/view71"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view72"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view81"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

            <com.android.imooc.PorterDuffView
                android:id="@+id/view82"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

        </LinearLayout>

        <!--00000000000000000000000000000000000000000000000000-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1"
                android:text="MULTIPLY"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="SCREEN"
                android:textSize="16sp" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.android.imooc.PorterDuffView
                android:id="@+id/view91"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />


            <com.android.imooc.PorterDuffView
                android:id="@+id/view92"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1" />

        </LinearLayout>

    </LinearLayout>
</ScrollView>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值