【Android动画渲染及混合模式】

Android动画渲染及混合模式

public class GradientLayout extends View {

private Paint mPaint;
private Shader mShader;
private Bitmap mBitmap;

public GradientLayout(Context context) {
    this(context,null);
}

public GradientLayout(Context context, AttributeSet attrs) {
    this(context,attrs,0);
}

public GradientLayout(Context context,AttributeSet attrs,int defStyleAttr) {
    super(context,attrs,defStyleAttr);
    init();
}

private void init(){

    mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.idea);

mPaint = new Paint();//初始化

// mPaint.setColor(Color.RED);
// mPaint.setARGB(255,255,255,0);//设置Paint对象颜色。,范围为0~255
// mPaint.setAlpha(200);//设置alpha不透明度,范围为0~255
mPaint.setAntiAlias(true);//抗锯齿
//
mPaint.setStyle(Paint.Style.FILL);//描边效果
// mPaint.setStrokeWidth(4);//描边宽度
//
// mPaint.setStrokeCap(Paint.Cap.ROUND);//圆角效果
// mPaint.setStrokeJoin(Paint.Join.MITER);//拐角风格
// mPaint.setShader(new SweepGradient(200,200,Color.BLUE,Color.RED));//设置环形渲染器
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));//设置图层混合模式
// mPaint.setColorFilter(new LightingColorFilter(0x00ffff,0x000000));//设置颜色过滤器
// mPaint.setFilterBitmap(true);//设置双线性过滤
// mPaint.setMaskFilter(new BlurMaskFilter(10,BlurMaskFilter.Blur.NORMAL));//设置画笔遮罩滤镜,传入度数和样式
// mPaint.setTextSize(38);
// mPaint.setTextScaleX(2);//设置文本缩放倍数
// mPaint.setTextAlign(Paint.Align.LEFT);//对其方式
// mPaint.setUnderlineText(true);//设置下划线
//
// String str = “xxxxx”;
// Rect rect = new Rect();
// mPaint.getTextBounds(str,0,str.length(),rect);//测量文本大小,将文本大小信息存放在rect中
// mPaint.measureText(str);//获取文本的宽
// mPaint.getFontMetrics();//获取字体度量对象

}

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

// /**

// * 1.线性渲染,LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[], @Nullable float positions[],@)

// * colors:渐变数组
// * positions:位置数组,position的取值范围[0,1],作用是指定某个位置的颜色值,如果传null,渐变就线性变化
// * tile:用于指定控件区域大于指定的渐变区域时,空白区域的颜色填充方法
// */
// // mShader = new LinearGradient(0,0,500,500,new int[]{Color.RED,Color.BLUE},null, Shader.TileMode.CLAMP);
//
// mShader = new LinearGradient(0,0,500,500,new int[]{Color.RED,Color.BLUE,Color.GREEN},new float[]{0.f,0.7f,1},Shader.TileMode.CLAMP);
// mPaint.setShader(mShader);
canvas.drawCircle(250,250,250,mPaint);
// canvas.drawRect(0,0,500,500,mPaint);

// /**

// * 环形渲染:RadialGradient(float centerX, float centerY, float radious, @ColorInt int colors[], @Nullable float stops[], TileMode)

// * centerX,centerY:shader的中心坐标,开始渐变的坐标;
// * radius:渐变的半径;
// * centerColor,edgeColor:中心点渐变颜色,边界渐变颜色;
// * colors:渐变颜色数组;
// * stoops:渐变位置数组,类似扫描渐变的positions数组,取值[0,1],中心点为0,半径到达位置为1.0f;
// * tileMode:shader未覆盖以外的填充模式;
// */
// mShader = new RadialGradient(250,250,250,new int[]{Color.GREEN,Color.YELLOW,Color.RED},null,Shader.TileMode.CLAMP);
// mPaint.setShader(mShader);
// canvas.drawCircle(250,250,250,mPaint);

// /**

// * 扫描渲染:SweepGradient(float cx, float cy, int color0, int color1) // * 参数:

// * cx cy:扫描的中心;
// * color0:扫描的起始颜色;
// * color1:扫描的终止颜色;
// * colors,positions:类似LinearGradient,用于颜色渐变,positions为null时,根据颜色线性渐变;
// */
// mShader = new SweepGradient(250,250,Color.RED,Color.GREEN);
// mPaint.setShader(mShader);
// canvas.drawCircle(250,250,250,mPaint);

// /**

// * 位图渲染:BitmapShader(Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY) // * bitmap:用来做模板的Bitmap;

// * tileX,tileY:横向/纵向的着色规则,类型是TileMode;
// * REPEAT:绘制区域超过渲染区域的部分,重复排版;
// * CLAMP:绘制区域超过渲染区域的部分,会以最后一个像素拉伸排版;
// * MIRROR:绘制区域超过渲染区域的部分,镜像翻转排版;
// */
// mShader = new BitmapShader(mBitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
// mPaint.setShader(mShader);
// canvas.drawRect(0,0,mBitmap.getWidth(),mBitmap.getHeight(), mPaint);
canvas.drawCircle(250,250,250,mPaint);

    /**

* 组合渲染 ComposeShader(@NonNull Shader shaderA,@NonNull Shader shaderB,@NonNull Xfermode mode){ * this(shaderA,shaderB,mode.proterDuffMode);

     * }
     */
    BitmapShader bitmapShader = new BitmapShader(mBitmap,Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
    LinearGradient linearGradient = new LinearGradient(0,0,1000,1600,new int[]{Color.RED,Color.GREEN,Color.BLUE},null,Shader.TileMode.REPEAT);
    mPaint.setShader(mShader);
    canvas.drawCircle(250,250,250,mPaint);
}

/**

* 图层混合模式:PorterDuff.Mode * 它将所绘制图形的像素与 Canvas中对应位置的像素按照一定规则进行混合,形成新的像素值,从而更新Canvas中最终的像素颜色值;

* 18中模式:

  • 18中模式:
    • Mode.CLEAR Mode.SRC Mode.DST
    • Mode.SRC_OVER Mode.DST_OVER Mode.SRC_IN
    • Mode.DST_IN Mode.SRC_OUT Mode.DST_OUT
    • Mode.SRC_ATOP Mode.DST_ATOP Mode.XOR
    • Mode.DARKEN Mode.LIGHTEN Mode.MULTIPLY
    • Mode.SCREEN Mode.OVERLAY Mode.ADD
      */

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值