Android-UI-图形绘制-Paint
程序员文章站
2022-06-16 12:59:53
图形绘制---paint渲染:shader---着色器Canvas的drawXXX这个方法是画具体的形状,画笔的shader定义的就是圆形的着色和外观TieMode--拉伸形式:CLAMP 、REPEAT、 MIRRORCLAMP :拉伸最后一个像素铺满REPEAT:类似电脑壁纸,横向纵向不足的重复放置MIRROR:横向纵向不足处不断翻转镜像平铺shader(五种着色器):BitmapShader--位图渲染、 LinearGradient--线性渲......
图形绘制---paint
渲染:
以位图渲染为例:
BitmapShader.java
public class MyGradientView extends View {
private Paint mPaint;
private Bitmap mBitMap = null;
private int mWidth;
private int mHeight;
private int[] mColors = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
public MyGradientView(Context context) {
super(context);
mBitMap = ((BitmapDrawable) getResources().getDrawable(R.drawable.xyjy2)).getBitmap();
mPaint = new Paint();
mWidth = mBitMap.getWidth();
mHeight = mBitMap.getHeight();
}
public MyGradientView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mBitMap = ((BitmapDrawable) getResources().getDrawable(R.drawable.xyjy2)).getBitmap();
mPaint = new Paint();
mWidth = mBitMap.getWidth();
mHeight = mBitMap.getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
/**
* 位图渲染,BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY)
* Bitmap:构造shader使用的bitmap
* tileX:X轴方向的TileMode
* tileY:Y轴方向的TileMode
*/
BitmapShader bitMapShader = new BitmapShader(mBitMap, Shader.TileMode.REPEAT,
Shader.TileMode.REPEAT);
mPaint.setShader(bitMapShader);
mPaint.setAntiAlias(true);
canvas.drawRect(new Rect(0, 0, 1000, 1600), mPaint);
}
}
滤镜:
FilterView.java (使用颜色矩阵实现底片效果、颜色增强、黑白照片、复古等效果)
package com.dn_alan.myapplication.filter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import com.dn_alan.myapplication.R;
public class FilterView extends View {
Paint paint;
Bitmap bitmap;
public FilterView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//关闭单个View的硬件加速功
// setLayerType(View.LAYER_TYPE_SOFTWARE,null);
//
RectF rectF = new RectF(200,100,bitmap.getWidth()+200,bitmap.getHeight());
paint.reset();
canvas.drawBitmap(bitmap,null, rectF,paint);
// 平移运算---加法
// ColorMatrix colorMartrix = new ColorMatrix(new float[]{
// 1, 0,0,0,0,
// 0,1,0,0,100,
// 0,0,1,0,0,
// 0,0,0,1,0,
// });
// 反相效果 -- 底片效果
// ColorMatrix colorMartrix = new ColorMatrix(new float[]{
// -1, 0,0,0,255,
// 0,-1,0,0,255,
// 0,0,-1,0,255,
// 0,0,0,1,0,
// });
// 缩放运算---乘法 -- 颜色增强
// ColorMatrix colorMartrix = new ColorMatrix(new float[]{
// 1.2f, 0,0,0,0,
// 0,1.2f,0,0,0,
// 0,0,1.2f,0,0,
// 0,0,0,1.2f,0,
// });
/** 黑白照片
*是将我们的三通道变为单通道的灰度模式
*去色原理:只要把R G B 三通道的色彩信息设置成一样,那么图像就会变成灰色,
*同时为了保证图像亮度不变,同一个通道里的R+G+B =1
*/
// ColorMatrix colorMartrix = new ColorMatrix(new float[]{
// 0.213f, 0.715f,0.072f,0,0,
// 0.213f, 0.715f,0.072f,0,0,
// 0.213f, 0.715f,0.072f,0,0,
// 0,0,0,1,0,
// });
// 发色效果---(比如红色和绿色交换)
// ColorMatrix colorMartrix = new ColorMatrix(new float[]{
// 1,0,0,0,0,
// 0, 0,1,0,0,
// 0,1,0,0,0,
// 0,0,0,0.5F,0,
// });
// 复古效果
// ColorMatrix colorMartrix = new ColorMatrix(new float[]{
// 1/2f,1/2f,1/2f,0,0,
// 1/3f, 1/3f,1/3f,0,0,
// 1/4f,1/4f,1/4f,0,0,
// 0,0,0,1,0,
// });
// 颜色通道过滤
//两个矩阵
//本身颜色矩阵 A
//过滤矩阵 c
//a*c=out color
ColorMatrix colorMartrix = new ColorMatrix(new float[]{
1.3F,0,0,0,0,
0,1.3F,0,0,0,
0,0,1.3F,0,0,
0,0,0,1,0,
});
RectF rectF2 = new RectF(200,100 + bitmap.getHeight(),bitmap.getWidth()+200,bitmap.getHeight() * 2);
paint.setColorFilter(new ColorMatrixColorFilter(colorMartrix));
canvas.drawBitmap(bitmap,null, rectF2,paint);
}
}
Xfermode:
PorterDuff所有模式:https://developer.android.google.cn/reference/android/graphics/PorterDuff.Mode?hl=en#ADD
本文地址:https://blog.csdn.net/qq_23956889/article/details/112341410