自定义View(一)---Paint

一、Paint简单的理解是画笔,油漆。而 Paint 接口定义如何为 Graphics2D 操作生成颜色模式。将实现 Paint 接口的类添加到 Graphics2D 上下文中,以便定义 draw 和 fill 方法所使用的颜色模式。
Pain类的常用属性设置方法如下:
setAntiAlias(); //设置画笔的抗锯齿效果

  setColor(); //设置画笔的颜色

  setARGB(); //设置画笔的A、R、G、B值

  setAlpha(); //设置画笔的Alpha值,取值0~255

  setTextSize(); //设置字体的尺寸

  setStyle(); //设置画笔的风格(空心或实心)

  setStrokeWidth(); //设置空心边框的宽度
setDither:设定图像是否使用抖动处理,会使绘制出来的图片 颜色更加平滑,饱满,图像更加清晰。
setFakeBoldText:设置为粗体文本。
setFilterBitmap:对位图进行滤波处理。//参考滤波算法
setHinting:API LEVEL 14:设置暗模式,HINTING_OFF, HINTING_ON
setLetterSpacing:API LEVEL 21:设置文本间距,默认为0,负 数为收紧文本。
setLinearText:设置线性文本
  getColor(); //获取画笔的颜色
二、ColorMatrix(颜色矩阵) setColorMatrix
ColorMatrix 有三个子类
1、ColorMatrixColorFilter
对图像进行颜色方面的处理。ColorMatrix是一个5*4的矩阵。如图所示,在android中,是以以为数组的方式存储的n = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t}的方式进行存储。

 ColorMatrix colorMatrix = new ColorMatrix(new float[]{
                //没一行前4列系数的取值范围为:0.0~2.0,
                //第5列的取值范围为0~255
                0.5f, 0, 0, 0, 0,//R
                0, 0.5f, 0, 0, 0,//G
                0, 0, 0.5f, 0, 0,//B
                0, 0, 0, 1, 0//A
        });
       paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

PS:每一行前4列系数的取值范围为0.0~2.0
第五列的取值范围为0~255

在一张图片中,图像的RGBA(红色、绿色、蓝色、透明度)值决定了该图片所呈现出来的颜色效果。
而图像的RGBA值则存储在一个5*1的颜色分量矩阵C中,由颜色分量矩阵C可以控制图像的颜色效果。
C = [R
G
B
1]

要想改变一张图片的颜色效果,只需要改变图像的颜色分量矩阵或者改变颜色矩阵即可。
2、LightingColorFilter
顾名思义光照颜色过滤,这肯定是跟光照是有关的了。该类有且只有一个构造方法:
参数1:mul全称ColorMultiply,意思为色彩倍增。
参数2:add全称ColorAdd,意思为色彩的添加
这两个值都是16进制的色彩0xAARRGGBB

 paint.setColorFilter(new LightingColorFilter(0xFFFF00FF, 0x00000000));

3、PorterDuffColorFilter

PorterDuffColorFilter跟LightingColorFilter一样,只有一个构造方法:

参数1:16进制的颜色值
参数2:Mode混合模式,PorterDuff内部类Mode中的一个常量值,这个值表示混合模式。

PorterDuffColorFilter(int color, PorterDuff.Mode mode)  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义一个水波进度 View,你需要完成以下几个步骤: 1. 创建一个自定义 View 类,并在构造函数中初始化一些必要的属性,如颜色、线宽等。 2. 重写 onSizeChanged() 方法,在该方法中获取 View 的宽度和高度,并计算出进度条的半径、圆心等相关参数。 3. 重写 onDraw() 方法,在该方法中绘制水波纹效果。 4. 在自定义 View 中添加一个 setProgress() 方法,用于设置进度条的进度。 5. 在布局文件中引入自定义 View,设置 layout_width 和 layout_height 属性,并在代码中调用 setProgress() 方法设置进度条的进度。 下面是一个简单的自定义水波进度 View 的代码示例: ```java public class WaterWaveProgressView extends View { private Paint mPaint; private int mWidth, mHeight; private float mRadius; private float mProgress; public WaterWaveProgressView(Context context) { super(context); init(); } public WaterWaveProgressView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WaterWaveProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(5); mPaint.setStyle(Paint.Style.STROKE); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; mRadius = Math.min(mWidth, mHeight) / 2 * 0.8f; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mPaint); float angle = mProgress / 100 * 360; canvas.drawArc(mWidth / 2 - mRadius, mHeight / 2 - mRadius, mWidth / 2 + mRadius, mHeight / 2 + mRadius, -90, angle, false, mPaint); } public void setProgress(float progress) { mProgress = progress; invalidate(); } } ``` 通过调用 setProgress() 方法来更新进度条的进度,如下所示: ```java WaterWaveProgressView progressView = findViewById(R.id.progress_view); progressView.setProgress(50); // 设置进度为 50% ``` 在布局文件中引入自定义 View: ```xml <com.example.waterwaveprogressview.WaterWaveProgressView android:id="@+id/progress_view" android:layout_width="150dp" android:layout_height="150dp" /> ``` 这样就能够实现一个简单的水波进度 View 了。如果需要更加复杂的效果,可以在 onDraw() 方法中绘制多个水波纹,或者使用 Path 绘制波形等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值