Canvas的flag具体的含义

示例代码:

package com.loaderman.customviewdemo;

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

public class MATRIX_SAVE_FLAG_View extends View {
    private Paint mPaint;
    public MATRIX_SAVE_FLAG_View(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint = new Paint();

        mPaint.setColor(Color.GRAY);
    }

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

        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(40);
        canvas.drawRect(100,0,200,100,mPaint);
        canvas.restore();

        mPaint.setColor(Color.BLACK);
        canvas.drawRect(100,0,200,100,mPaint);

//    canvas.save(Canvas.MATRIX_SAVE_FLAG);
//    canvas.clipRect(100,0,200,100);
//    canvas.drawColor(Color.GRAY);
//    canvas.restore();
//
//    canvas.drawColor(Color.BLACK);
}
}

package com.loaderman.customviewdemo;

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


public class CLIP_SAVE_FLAG_View extends View {
    private Paint mPaint;
    public CLIP_SAVE_FLAG_View(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint = new Paint();

    }

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

//        canvas.drawColor(Color.RED);
//        canvas.save(Canvas.CLIP_SAVE_FLAG);
//        canvas.clipRect(100,0,200,100);
//        canvas.restore();
//
//        canvas.drawColor(Color.YELLOW);

    mPaint.setColor(Color.GRAY);
    canvas.drawRect(100,0,200,100,mPaint);

    canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.rotate(40);
    canvas.restore();

    mPaint.setColor(Color.BLACK);
    canvas.drawRect(100,0,200,100,mPaint);
}
}

package com.loaderman.customviewdemo;

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


public class ALPHA_COLOR_FALG_VIEW extends View {
    private Paint mPaint;
    public ALPHA_COLOR_FALG_VIEW(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(Color.GRAY);
//
//        canvas.saveLayer(0,0,300,300,mPaint,Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
//        mPaint.setColor(Color.BLACK);
//        canvas.drawRect(100,100,200,200,mPaint);
//        canvas.restore();


        canvas.drawColor(Color.GRAY);

        canvas.saveLayer(0,0,300,300,mPaint,Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(40);
        mPaint.setColor(Color.BLACK);
        canvas.drawRect(100, 100, 200, 200, mPaint);
        canvas.restore();
    }
}

package com.loaderman.customviewdemo;

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


public class CLIP_SAVE_FLAG_View extends View {
    private Paint mPaint;
    public CLIP_SAVE_FLAG_View(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint = new Paint();

    }

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

//        canvas.drawColor(Color.RED);
//        canvas.save(Canvas.CLIP_SAVE_FLAG);
//        canvas.clipRect(100,0,200,100);
//        canvas.restore();
//
//        canvas.drawColor(Color.YELLOW);

    mPaint.setColor(Color.GRAY);
    canvas.drawRect(100,0,200,100,mPaint);

    canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.rotate(40);
    canvas.restore();

    mPaint.setColor(Color.BLACK);
    canvas.drawRect(100,0,200,100,mPaint);
}
}

 

posted on 2019-01-04 14:04  LoaderMan  阅读(1551)  评论(0编辑  收藏  举报

导航