android canvas畫圖 切割畫布(clipRect)


android切割畫布的歷程不算很難,可是理解起來也比較麻煩,這里寫一下我的理解 但是不一定正確: 
canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);最后一個參數有多個選擇分別是://DIFFERENCE是第一次不同於第二次的部分顯示出來 
            //REPLACE是顯示第二次的 
            //REVERSE_DIFFERENCE 是第二次不同於第一次的部分顯示 
            //INTERSECT交集顯示 
            //UNION全部顯示 
            //XOR補集 就是全集的減去交集生育部分顯示

android canvas畫圖 切割畫布(clipRect) - Wallace - 懶羊羊的南瓜屋 

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;

public class sBook extends View{
    Context mContext;
    Paint mPaint;
    Path mPath;
    public sBook(Context context) {
        super(context);
        init();
    }
    
    public sBook(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public sBook(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    
    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(6);
        mPaint.setTextSize(16);
        mPaint.setTextAlign(Paint.Align.RIGHT); 
        
        mPath = new Path();
    }
    
    protected void onDraw(Canvas canvas){
        canvas.drawColor(Color.GRAY);            
        
        canvas.save();
        canvas.translate(10, 10);
        drawScene(canvas); 
        canvas.restore(); 
        
        canvas.save();
        canvas.translate(160, 10);
        canvas.clipRect(10, 10, 90, 90);
        canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);
        drawScene(canvas);
        canvas.restore();
       
        canvas.save();
        canvas.translate(10, 160);
        mPath.reset();
//        canvas.clipPath(mPath); // makes the clip empty
//        mPath.addCircle(50, 50, 50, Path.Direction.CCW);
        mPath.cubicTo(0, 0, 100, 0, 100, 100);
        mPath.cubicTo(100, 100, 0, 100, 0, 0);
        canvas.clipPath(mPath, Region.Op.REPLACE);
        drawScene(canvas);
        canvas.restore();
       
        canvas.save();
        canvas.translate(160, 160);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);
        drawScene(canvas);
        canvas.restore();
       
        canvas.save();
        canvas.translate(10, 310);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);
        drawScene(canvas);
        canvas.restore();
       
        canvas.save();
       
        canvas.translate(160, 310);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE);
        drawScene(canvas);
        canvas.restore(); 
    }
    
    private void drawScene(Canvas canvas) {
        canvas.clipRect(0, 0, 100, 100);
       
        canvas.drawColor(Color.WHITE);
       
        mPaint.setColor(Color.RED);
        canvas.drawLine(0, 0, 100, 100, mPaint);
       
        mPaint.setColor(Color.GREEN);
        canvas.drawCircle(30, 70, 30, mPaint);
       
        mPaint.setColor(Color.BLUE);
        canvas.drawText("Clipping", 100, 30, mPaint);
    }
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com