huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
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
package com.DeviceTest.view;
 
import com.DeviceTest.helper.TestCase.RESULT;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.TextView;
import com.DeviceTest.R;
public class MyItemView extends TextView {
   private static final String TAG = "MyItemView";
 
   public static final int PASS_COLOR = Color.rgb(0, 255, 0);
   public static final int FAILED_COLOR = Color.rgb(255, 0, 0);
   public static final int SKIP_COLOR = Color.rgb(0, 0, 255);
   public static final int CLICK_COLOR = Color.rgb(0, 255, 255);
 
   private RESULT mResult = RESULT.UNDEF;
   private int current_color = 0x00000000;
   private Paint mPaint = new Paint();
   private Rect mRect = new Rect();
   private Bitmap checkedbitmap = null;
   private Bitmap uncheckedbitmap = null;
   private int checkicon_left = 0;
   private int checkicon_top = 0;
   private final static int PADDINGLEN = 5;
   private boolean ischeck = true;
   private float scale;
   public MyItemView(Context context) {
       this(context, null, 0);
   }
 
   public MyItemView(Context context, AttributeSet attrs) {
       this(context, attrs, 0);
   }
 
   public MyItemView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
       WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        
        Display display = windowManager.getDefaultDisplay();
        int densityDpi = getResources().getDisplayMetrics().densityDpi;
        scale = densityDpi/120;
       mPaint.setColor(Color.WHITE);
       mPaint.setStrokeWidth(2);
       mPaint.setStyle(Style.STROKE);
//        Drawable tmp = context.getResources().getDrawable(R.drawable.devicetest_icon);
       checkedbitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.btn_check_on);
       uncheckedbitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.btn_check_on_disable);
       setTextColor(Color.WHITE);
       setResult(RESULT.UNDEF);
       setBackgroundResource(R.drawable.itemclickbk);
   }
 
   
   protected void onLayout(boolean changed, int left, int top, int right,
           int bottom) {
       super.onLayout(changed, left, top, right, bottom);
       setTextSize((bottom - top) * 2 / 4/scale);
       checkicon_left = this.getWidth() - checkedbitmap.getWidth() - PADDINGLEN * 2;
       checkicon_top = (this.getHeight() - checkedbitmap.getHeight()) / 2;
   }
 
   public void setResult(RESULT result) {
       int color;
       switch (result) {
       case OK:
           color = PASS_COLOR;
           break;
       case NG:
           color = FAILED_COLOR;
           break;
       case SKIP:
           color = SKIP_COLOR;
           break;
       case UNDEF:
           color = 0xff101010;
           break;
       default:
           return;
       }
       
       mResult = result;
       current_color = color;
       this.setBackgroundColor(color);
   }
 
   public RESULT getResult() {
       return mResult;
   }
   public void setCheck(boolean check){
       this.ischeck = check;
       this.invalidate();
   }
   public boolean getTemcheckclick(){
       return tmpcheckclick;
   }
   public boolean setCheckClick(){
       if(tmpcheckclick){
           if(ischeck)
               ischeck = false;
           else
               ischeck = true;
       }
       this.invalidate();
       return tmpcheckclick;
   }
   public boolean getischeck(){
       return ischeck;
   }
   protected void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       getDrawingRect(mRect);
       canvas.drawRect(mRect, mPaint);
       if(ischeck)
           canvas.drawBitmap(checkedbitmap, checkicon_left, checkicon_top, null);
       else
           canvas.drawBitmap(uncheckedbitmap, checkicon_left, checkicon_top, null);
   }
   
   private boolean tmpcheckclick = false;
   private boolean istouch = false;
   @Override
   public boolean onTouchEvent(MotionEvent event) {
//        Log.d(TAG, " ________ action = " + event.getAction() + "   " + event.getX() + ", " + event.getY() + "   w = " + this.getWidth());
//        Log.d(TAG, "_____________________ onTouchEvent(),  " + event.getPointerCount());
       boolean ret = false;
       switch(event.getAction()){
       case MotionEvent.ACTION_DOWN:
           tmpcheckclick = false;
           this.setBackgroundColor(CLICK_COLOR);
           if(event.getX() > checkicon_left){
               tmpcheckclick = true;
           }
           istouch = true;
           break;
       case MotionEvent.ACTION_MOVE:
           if(event.getX()>0 && event.getX() < this.getWidth() && event.getY() > 0 && event.getY() < this.getHeight()){
               this.setBackgroundColor(CLICK_COLOR);
               if(tmpcheckclick && event.getX() < checkicon_left){
                   tmpcheckclick = false;
               }
           }else{
               tmpcheckclick = false;
               this.setBackgroundColor(current_color);
           }
           break;
       case MotionEvent.ACTION_UP:
           this.setBackgroundColor(current_color);
           istouch = false;
           break;
       }
       return super.onTouchEvent(event);
   }
   public boolean getIsTouch(){
       return istouch;
   }
}