-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderView.java
More file actions
112 lines (86 loc) · 3.63 KB
/
ShaderView.java
File metadata and controls
112 lines (86 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
//自己导包以及R引入
public class ShaderView extends View {
Paint paint;
private int width;
private int height;
private int centerX;
private int centerY;
Bitmap bitmap;
BitmapShader shader;
BitmapFactory.Options options;
//自定义view的限制宽高
private static final int MOST_WIDTH = 800;
private static final int MOST_HEIGHT = MOST_WIDTH;
public ShaderView(Context context) {
this(context, null);
}
public ShaderView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.tt);
options = new BitmapFactory.Options();
BitmapFactory.decodeResource(context.getResources(), R.drawable.tt, options);
shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.MIRROR);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int wSize = MeasureSpec.getSize(widthMeasureSpec); //parent给的参考值, AT_MOST下不能超过改值
int hMode = MeasureSpec.getMode(heightMeasureSpec);
int hSize = MeasureSpec.getSize(heightMeasureSpec); //parent给的参考值, AT_MOST下不能超过改值
Log.d("szx", "onLayout, wSize=" + wSize + ",hSize=" + hSize);
if (wMode == MeasureSpec.EXACTLY) { //match_parent或者精确值
width = wSize;
} else if (wMode == MeasureSpec.AT_MOST) { //wrap_content, UNSPECIFIED不考虑
width = Math.min(wSize, MOST_WIDTH);
}
if (hMode == MeasureSpec.EXACTLY) { //match_parent或者精确值
height = hSize;
} else if (hMode == MeasureSpec.AT_MOST) { //wrap_content, UNSPECIFIED不考虑
height = Math.min(hSize, MOST_HEIGHT);
}
width = height = Math.min(width, height);
Log.d("szx", "onLayout, width=" + width + ",height=" + height);
centerX = width / 2;
centerY = height / 2;
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//
float wScale = (float)width / (float)options.outWidth;
float hScame = (float)height / (float)options.outHeight;
float scale = Math.min(wScale, hScame);
Log.d("szx", "width="+width+",height="+height);
Log.d("szx", "options.outWidth="+options.outWidth+",options.outWidth="+options.outWidth);
Log.d("szx", "wScale="+wScale+",hScame="+hScame+",scale="+scale);
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
shader.setLocalMatrix(matrix);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("szx","onDraw");
paint.setShader(shader);
canvas.drawCircle(centerX, centerY, width/2, paint);
}
}