/
RippleAdjuster.java
165 lines (145 loc) · 4.6 KB
/
RippleAdjuster.java
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (C) 2017 CoorChice <icechen_@outlook.com>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
* <p>
* Last modified 17-4-17 下午5:44
*/
package com.coorchice.supertextview.SuperTextView.Adjuster;
import com.coorchice.library.SuperTextView;
import com.coorchice.supertextview.R;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import java.io.IOException;
import java.io.InputStream;
/**
* Project Name:SuperTextView
* Author:CoorChice
* Date:2017/4/17
* Notes:
*/
public class RippleAdjuster extends SuperTextView.Adjuster {
private static final float DEFAULT_RADIUS = 50;
private PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
private int rippleColor = Color.parseColor("#ce938d");
private float x = -1;
private float y = -1;
private Paint paint;
private float density;
private float radius = DEFAULT_RADIUS;
private RectF rectF = new RectF();
private float velocity = 2f;
private Path solidPath;
private RectF solidRectF;
private Bitmap src;
private Canvas srcCanvas;
private Bitmap dst;
private Canvas dstCanvas;
public RippleAdjuster(int rippleColor) {
this.rippleColor = rippleColor;
initPaint();
}
private void initPaint() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(rippleColor);
}
@Override
protected void adjust(SuperTextView v, Canvas canvas) {
int width = v.getWidth();
int height = v.getHeight();
if (srcCanvas == null){
src = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
srcCanvas = new Canvas(src);
}
if (dstCanvas == null){
dst = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
dstCanvas = new Canvas(dst);
}
if (density == 0) {
density = v.getResources().getDisplayMetrics().density;
}
if (x == -1) {
x = width / 2;
}
if (y == -1) {
x = height / 2;
}
if (radius < ((float) width) * 1.1) {
radius = (radius + velocity);
} else {
v.stopAnim();
}
rectF.setEmpty();
rectF.set(0, 0, width, height);
if (solidPath == null) {
solidPath = new Path();
} else {
solidPath.reset();
}
if (solidRectF == null) {
solidRectF = new RectF();
} else {
solidRectF.setEmpty();
}
float strokeWidth = v.getStrokeWidth();
solidRectF.set(strokeWidth, strokeWidth, v.getWidth() - strokeWidth,
v.getHeight() - strokeWidth);
solidPath.addRoundRect(solidRectF, v.getCorners(), Path.Direction.CW);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
srcCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
srcCanvas.drawPath(solidPath, paint);
paint.setColor(rippleColor);
dstCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
dstCanvas.drawCircle(x, y, radius * density, paint);
// 创建一个图层,在图层上演示图形混合后的效果
int sc = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
sc = canvas.saveLayer(0, 0, width, height, null);
} else {
sc = canvas.saveLayer(0, 0, width, height, null, 0);
}
canvas.drawBitmap(src, 0, 0, paint);
paint.setXfermode(xfermode);
canvas.drawBitmap(dst, 0, 0, paint);
paint.setXfermode(null);
canvas.restoreToCount(sc);
}
@Override
public boolean onTouch(SuperTextView v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
radius = DEFAULT_RADIUS;
v.setAutoAdjust(true);
v.startAnim();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
v.stopAnim();
v.setAutoAdjust(false);
break;
}
return true;
}
}