Canvas saveLayer() 与 restoreToCount()

saveLayer()restoreToCount()saverestore() 大同小异,只是前者多了一些东西

save() 方法保存的是 整个 Canvas ,而 saveLayer() 则可以选择性的保存某个区域的状态

restoreToCount(int) 可以直接传入要恢复到的 Layer 层数,直接就跳到对应的那一层,同时会将该层上面所有的Layer踢出栈,让该层成为栈顶

saveLayer()

saveLayer() 有四个重载方法,这几个方法在 API 21+ 中被添加

int saveLayer(RectF bounds, Paint paint)

int saveLayerAlpha(RectF bounds, int alpha)

int saveLayer(float left, float top, float right, float bottom, Paint paint)

int saveLayerAlpha(float left, float top, float right, float bottom, int alpha)

还有几个在 API 26+ 中被废弃的

saveLayer(RectF bounds, Paint paint, int saveFlags)

int saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags)

int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags)

int saveLayerAlpha(RectF bounds, int alpha, int saveFlags)

参数都很好理解,我们重点讲解下 int saveFlags ,这个是设置改保存哪个对象的,可选的值有

说明
ALL_SAVE_FLAG 保存全部的状态

还有几个在 API 26+ 中被废弃的

说明
CLIP_SAVE_FLAG 保存裁剪的某个区域的状态
CLIP_TO_LAYER_SAVE_FLAG 保存预先设置的范围里的状态
FULL_COLOR_LAYER_SAVE_FLAG 保存彩色涂层
HAS_ALPHA_LAYER_SAVE_FLAG 不透明图层保存
MATRIX_SAVE_FLAG Matrix 信息 (translate,rotate,scale,skew) 的状态保存

范例

我们写一个范例来演示一下 saveLayer()

  1. 创建一个 空的 Android 项目 cn.twle.android.SaveLayer

  2. 下载 /static/i/android/avatar_128x128.png,并放到 res/drawable 目录下

  3. 自定义一个 View 类 MsView.java

    package cn.twle.android.savelayer;
    
    import android.content.Context;
    import android.os.Build;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.support.v7.widget.AppCompatImageView;
    import android.util.AttributeSet;
    
    public class MsView extends AppCompatImageView {
    
        private Bitmap mBitmap;
        private Paint mPaint = new Paint();
    
        public MsView(Context context) {
            this(context,null);
        }
    
        public MsView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avatar_128x128);
    
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
            mPaint.setAntiAlias(true);// 抗锯尺
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if(mBitmap == null)
            {
                return;
            }
    
            RectF bounds = new RectF(0, 0, 400, 400);
    
            if ( Build.VERSION.SDK_INT >= 21)
                canvas.saveLayer(bounds, mPaint);
            else
                canvas.saveLayer(bounds, mPaint, Canvas.ALL_SAVE_FLAG);
    
            canvas.drawColor(0xffff0000);
            canvas.drawBitmap(mBitmap, 200, 200, mPaint);
            canvas.restoreToCount(1);
            canvas.drawBitmap(mBitmap, 400, 400, mPaint);
    
            invalidate();
        }
    }
    
  4. 修改 MainActivity.java 设置 setContentView(new MsView(MainActivity.this))

    package cn.twle.android.savelayer;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MsView(MainActivity.this));
        }
    }
    

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.