《“透视”个人大数据》项目开发小记 --(三)Android APP 开发(2)自定ImageView

      在以图片为主的项目数据表界面中,普遍的应用了自定义的ImageView,这增强了图片的表现力。这里以“事件表”介绍一个自定ImageView的实例。这个自定ImageView通过clipPath实现了圆角图片的显示,通过描绘Vector 图标在图片中,绘出了“选择标记”,“星级标记”,“定时任务标记”,“加密标记”及加密类型。

---- 自定的 ImageView

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
package com.bi3eview.newstart60.local.SelfWidget;
 
import android.graphics.Matrix;
import android.support.v7.widget.AppCompatImageView;
import android.util.DisplayMetrics;
import android.graphics.RectF;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
 
import com.bi3eview.newstart60.local.COMCONST;
 
public class TextImageView extends AppCompatImageView {
    private static final int TEXT_SIZE = 12;// sp
    private static int BACKGROUND_COLOR = Color.WHITE;
    private static int TEXT_COLOR = Color.BLUE;
    private static int MARKICON_COLOR = Color.YELLOW;
    private static int SELMARK_COLOR = Color.GREEN;
    private static int STAR_COLOR = Color.RED;
 
    public static String secretName = "se";
    public static String topSecretName = "tse";
    private int secretLockClass = 0;
    private int tasknum = 0;
    private int starClassno = 0;
    private boolean selmarkblv = false;
 
    public TextImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
 
    }
 
    public void setSecretClassno(int isecretClass) {
        secretLockClass = isecretClass;
    }
 
    public void setTimerTasknum(int iTasknum) {
        tasknum = iTasknum;
    }
    public void setStarClassno(int iStarClassno) {
        starClassno = iStarClassno;
    }
    public void setSelectMark(Boolean setselblv) {
        selmarkblv = setselblv;
    }
 
    @Override
    public void draw(Canvas canvas) {
        // 设置图片圆角修剪路经
        canvas.save();
        Path borderpath = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        float fCornerRadius = h/6;
        borderpath.addRoundRect(new RectF(0,0,w,h),fCornerRadius,fCornerRadius,Path.Direction.CW);
        canvas.clipPath(borderpath);
        super.draw(canvas);
        canvas.restore();
        // 描绘标记
        fCornerRadius = 0;
        canvas.save();
        int iwidth = canvas.getWidth();
        int iheight = canvas.getHeight();
        final Paint paint = new Paint();
 
        final DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        paint.setColor(MARKICON_COLOR);
        int ifnsize = (int)(TEXT_SIZE*dm.scaledDensity);
        paint.setTextSize(ifnsize);// 设置文字大小
        int ishapesize = (int)(19*dm.scaledDensity);
        paint.setStrokeWidth(1);
        Matrix cmaxtrix = new Matrix();
        Path starpath;
        /** 描绘定时任务标记 */
        if(tasknum > 0) {
            starpath = PathParser.doPath(COMCONST.VECTORPATH_TASK);
            cmaxtrix.setScale(2, 2);
            starpath.transform(cmaxtrix);
            starpath.offset(iwidth - ishapesize - ishapesize / 4, iheight - ishapesize - ishapesize / 4);
            canvas.drawPath(starpath, paint);
        }
        /** 描绘选择标记 */
        if(selmarkblv == true) {
            starpath = PathParser.doPath(COMCONST.VECTORPATH_SELMARK);
            cmaxtrix.reset();
            cmaxtrix.setScale(4, 4);
            starpath.transform(cmaxtrix);
            starpath.offset(iwidth/2 - ishapesize, iheight/2 - ishapesize/2);
            paint.setColor(SELMARK_COLOR);
            canvas.drawPath(starpath, paint);
        }
        /** 描绘星级标记 */
        if(starClassno >= 10){
            int istarjn = starClassno/10;
            starpath = PathParser.doPath(COMCONST.VECTORPATH_STAR);
            cmaxtrix.reset();
            cmaxtrix.setScale((float)1.5, (float)1.5);
            starpath.transform(cmaxtrix);
            starpath.offset(ishapesize/6, ishapesize/3);
            paint.setColor(STAR_COLOR);
            canvas.drawPath(starpath, paint);
            if(istarjn > 1) {
                for(int j = 1;j < istarjn;j++) {
                    starpath.offset(ishapesize, 0);
                    canvas.drawPath(starpath, paint);
                }
            }
        }
        /** 描绘“加密标记”及标注加密类型 */
        if(secretLockClass > 0) {
            paint.setColor(MARKICON_COLOR);
            starpath = PathParser.doPath(COMCONST.VECTORPATH_LOCK);
            cmaxtrix.reset();
            cmaxtrix.setScale(2, 2);
            starpath.transform(cmaxtrix);
            starpath.offset(ishapesize / 4, iheight - ishapesize-ishapesize/5);// (100,100);
            canvas.drawPath(starpath, paint);
            String secretTxt = secretName;//" 秘密 ";
            if(secretLockClass == 2) secretTxt = topSecretName;//" 绝密 ";
            int iclen = secretTxt.length();
            // 设置Rect Path
            Path path = new Path();
            int iLeft = ishapesize / 3+ishapesize;
            if(iclen > 2) iclen = iclen-1;
            int iRight = iLeft+ifnsize*iclen+ifnsize / 3;
            path.addRect(iLeft, iheight-ifnsize-ifnsize/2, iRight, iheight-ifnsize/3, Path.Direction.CW);
            path.setFillType(Path.FillType.WINDING);
            paint.setColor(BACKGROUND_COLOR);
            canvas.drawPath(path, paint);
            paint.setColor(TEXT_COLOR);
            canvas.drawText(secretTxt,ishapesize / 2+ishapesize,iheight-ifnsize/2,paint);
        }
        canvas.restore();
 
    }
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
 
}

  ----- 自定ImageView中用到 vector path Parser 的相关代码及数据

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package com.bi3eview.newstart60.local.SelfWidget;
 
import android.graphics.Path;
import android.graphics.RectF;
import android.util.Log;
 
public class PathParser {
 
    private static final String TAG = "SvgToPath.TAG";
 
    /*
     * This is where the hard-to-parse paths are handled.
     * Uppercase rules are absolute positions, lowercase are relative.
     * Types of path rules:
     * <p/>
     * <ol>
     * <li>M/m - (x y)+ - Move to (without drawing)
     * <li>Z/z - (no params) - Close path (back to starting point)
     * <li>L/l - (x y)+ - Line to
     * <li>H/h - x+ - Horizontal ine to
     * <li>V/v - y+ - Vertical line to
     * <li>C/c - (x1 y1 x2 y2 x y)+ - Cubic bezier to
     * <li>S/s - (x2 y2 x y)+ - Smooth cubic bezier to (shorthand that assumes the x2, y2 from previous C/S is the x1, y1 of this bezier)
     * <li>Q/q - (x1 y1 x y)+ - Quadratic bezier to
     * <li>T/t - (x y)+ - Smooth quadratic bezier to (assumes previous control point is "reflection" of last one w.r.t. to current point)
     * </ol>
     * <p/>
     * Numbers are separate by whitespace, comma or nothing at all (!) if they are self-delimiting, (ie. begin with a - sign)
     */
    public static Path doPath(String s) {
        int n = s.length();
        ParserHelper ph = new ParserHelper(s);
        ph.skipWhitespace();
        Path p = new Path();
        float lastX = 0;
        float lastY = 0;
        float lastX1 = 0;
        float lastY1 = 0;
        float contourInitialX = 0;
        float contourInitialY = 0;
        RectF r = new RectF();
        char cmd = 'x';
        while (ph.pos < n) {
            char next = s.charAt(ph.pos);
            if (!Character.isDigit(next) && !(next == '.') && !(next == '-')) {
                cmd = next;
                ph.advance();
            } else if (cmd == 'M') { // implied command
                cmd = 'L';
            } else if (cmd == 'm') { // implied command
                cmd = 'l';
            } else { // implied command
                //ignore
            }
            p.computeBounds(r, true);
            // Log.d(TAG, "  " + cmd + " " + r);
            // Util.debug("* Commands remaining: '" + path + "'.");
            boolean wasCurve = false;
            switch (cmd) {
                case 'M':
                case 'm': {
                    float x = ph.nextFloat();
                    float y = ph.nextFloat();
                    if (cmd == 'm') {
                        p.rMoveTo(x, y);
                        lastX += x;
                        lastY += y;
                    } else {
                        p.moveTo(x, y);
                        lastX = x;
                        lastY = y;
                    }
                    contourInitialX = lastX;
                    contourInitialY = lastY;
                    break;
                }
                case 'Z':
                case 'z': {
                    /// p.lineTo(contourInitialX, contourInitialY);
                    p.close();
                    lastX = contourInitialX;
                    lastY = contourInitialY;
                    break;
                }
                case 'L':
                case 'l': {
                    float x = ph.nextFloat();
                    float y = ph.nextFloat();
                    if (cmd == 'l') {
                        p.rLineTo(x, y);
                        lastX += x;
                        lastY += y;
                    } else {
                        p.lineTo(x, y);
                        lastX = x;
                        lastY = y;
                    }
                    break;
                }
                case 'H':
                case 'h': {
                    float x = ph.nextFloat();
                    if (cmd == 'h') {
                        p.rLineTo(x, 0);
                        lastX += x;
                    } else {
                        p.lineTo(x, lastY);
                        lastX = x;
                    }
                    break;
                }
                case 'V':
                case 'v': {
                    float y = ph.nextFloat();
                    if (cmd == 'v') {
                        p.rLineTo(0, y);
                        lastY += y;
                    } else {
                        p.lineTo(lastX, y);
                        lastY = y;
                    }
                    break;
                }
                case 'C':
                case 'c': {
                    wasCurve = true;
                    float x1 = ph.nextFloat();
                    float y1 = ph.nextFloat();
                    float x2 = ph.nextFloat();
                    float y2 = ph.nextFloat();
                    float x = ph.nextFloat();
                    float y = ph.nextFloat();
                    if (cmd == 'c') {
                        x1 += lastX;
                        x2 += lastX;
                        x += lastX;
                        y1 += lastY;
                        y2 += lastY;
                        y += lastY;
                    }
                    p.cubicTo(x1, y1, x2, y2, x, y);
                    lastX1 = x2;
                    lastY1 = y2;
                    lastX = x;
                    lastY = y;
                    break;
                }
                case 'S':
                case 's': {
                    wasCurve = true;
                    float x2 = ph.nextFloat();
                    float y2 = ph.nextFloat();
                    float x = ph.nextFloat();
                    float y = ph.nextFloat();
                    if (cmd == 's') {
                        x2 += lastX;
                        x += lastX;
                        y2 += lastY;
                        y += lastY;
                    }
                    float x1 = 2 * lastX - lastX1;
                    float y1 = 2 * lastY - lastY1;
                    p.cubicTo(x1, y1, x2, y2, x, y);
                    lastX1 = x2;
                    lastY1 = y2;
                    lastX = x;
                    lastY = y;
                    break;
                }
                case 'A':
                case 'a': {
                    float rx = ph.nextFloat();
                    float ry = ph.nextFloat();
                    float theta = ph.nextFloat();
                    int largeArc = (int) ph.nextFloat();
                    int sweepArc = (int) ph.nextFloat();
                    float x = ph.nextFloat();
                    float y = ph.nextFloat();
                    if (cmd == 'a') {
                        x += lastX;
                        y += lastY;
                    }
                    drawArc(p, lastX, lastY, x, y, rx, ry, theta, largeArc == 1, sweepArc == 1);
                    lastX = x;
                    lastY = y;
                    break;
                }
                case 'T':
                case 't': {
                    wasCurve = true;
                    float x = ph.nextFloat();
                    float y = ph.nextFloat();
                    if (cmd == 't') {
                        x += lastX;
                        y += lastY;
                    }
                    float x1 = 2 * lastX - lastX1;
                    float y1 = 2 * lastY - lastY1;
                    p.cubicTo( lastX, lastY, x1, y1, x, y );
                    lastX = x;
                    lastY = y;
                    lastX1 = x1;
                    lastY1 = y1;
                    break;
                }
                case 'Q':
                case 'q': {
                    wasCurve = true;
                    float x1 = ph.nextFloat();
                    float y1 = ph.nextFloat();
                    float x = ph.nextFloat();
                    float y = ph.nextFloat();
                    if (cmd == 'q') {
                        x += lastX;
                        y += lastY;
                        x1 += lastX;
                        y1 += lastY;
                    }
                    p.cubicTo( lastX, lastY, x1, y1, x, y );
                    lastX1 = x1;
                    lastY1 = y1;
                    lastX = x;
                    lastY = y;
                    break;
                }
                default:
                    Log.w(TAG, "Invalid path command: " + cmd);
                    ph.advance();
            }
            if (!wasCurve) {
                lastX1 = lastX;
                lastY1 = lastY;
            }
            ph.skipWhitespace();
        }
        return p;
    }
 
    /*
     * Elliptical arc implementation based on the SVG specification notes
     * Adapted from the Batik library (Apache-2 license) by SAU
     */
    private static void drawArc(Path path, double x0, double y0, double x, double y, double rx,
                                double ry, double angle, boolean largeArcFlag, boolean sweepFlag) {
        double dx2 = (x0 - x) / 2.0;
        double dy2 = (y0 - y) / 2.0;
        angle = Math.toRadians(angle % 360.0);
        double cosAngle = Math.cos(angle);
        double sinAngle = Math.sin(angle);
 
        double x1 = (cosAngle * dx2 + sinAngle * dy2);
        double y1 = (-sinAngle * dx2 + cosAngle * dy2);
        rx = Math.abs(rx);
        ry = Math.abs(ry);
 
        double Prx = rx * rx;
        double Pry = ry * ry;
        double Px1 = x1 * x1;
        double Py1 = y1 * y1;
 
        // check that radii are large enough
        double radiiCheck = Px1 / Prx + Py1 / Pry;
        if (radiiCheck > 1) {
            rx = Math.sqrt(radiiCheck) * rx;
            ry = Math.sqrt(radiiCheck) * ry;
            Prx = rx * rx;
            Pry = ry * ry;
        }
 
        // Step 2 : Compute (cx1, cy1)
        double sign = (largeArcFlag == sweepFlag) ? -1 : 1;
        double sq = ((Prx * Pry) - (Prx * Py1) - (Pry * Px1))
                / ((Prx * Py1) + (Pry * Px1));
        sq = (sq < 0) ? 0 : sq;
        double coef = (sign * Math.sqrt(sq));
        double cx1 = coef * ((rx * y1) / ry);
        double cy1 = coef * -((ry * x1) / rx);
 
        double sx2 = (x0 + x) / 2.0;
        double sy2 = (y0 + y) / 2.0;
        double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1);
        double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1);
 
        // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle)
        double ux = (x1 - cx1) / rx;
        double uy = (y1 - cy1) / ry;
        double vx = (-x1 - cx1) / rx;
        double vy = (-y1 - cy1) / ry;
        double p, n;
 
        // Compute the angle start
        n = Math.sqrt((ux * ux) + (uy * uy));
        p = ux; // (1 * ux) + (0 * uy)
        sign = (uy < 0) ? -1.0 : 1.0;
        double angleStart = Math.toDegrees(sign * Math.acos(p / n));
 
        // Compute the angle extent
        n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
        p = ux * vx + uy * vy;
        sign = (ux * vy - uy * vx < 0) ? -1.0 : 1.0;
        double angleExtent = Math.toDegrees(sign * Math.acos(p / n));
        if (!sweepFlag && angleExtent > 0) {
            angleExtent -= 360f;
        } else if (sweepFlag && angleExtent < 0) {
            angleExtent += 360f;
        }
        angleExtent %= 360f;
        angleStart %= 360f;
 
        RectF oval = new RectF((float) (cx - rx), (float) (cy - ry), (float) (cx + rx), (float) (cy + ry));
        path.addArc(oval, (float) angleStart, (float) angleExtent);
    }
 
}

  ------ vector path 数据表

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
package com.bi3eview.newstart60.local;
 
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.database.Cursor;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.text.InputFilter;
import android.widget.Toast;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
 
public class COMCONST {
    // 。。。。。。
    // vector path
    public static final String VECTORPATH_TASK = "M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8zM12.5,7H11v6l5.25,3.15 0.75,-1.23 -4.5,-2.67z";
    public static final String VECTORPATH_STAR = "M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z";
    public static final String VECTORPATH_SELMARK = "M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z";
    public static final String VECTORPATH_LOCK = "M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM12,17c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM15.1,8L8.9,8L8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1 1.71,0 3.1,1.39 3.1,3.1v2z";;
    // 。。。。
}

  

 

posted @   newstart60  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示