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
package com.DeviceTest.view;
 
import java.util.ArrayList;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
 
public class KeyTestView extends View{
   private final static String TAG = "KeyTestView";
   
   private int COLOR_NOR = Color.GRAY;
   private int COLOR_DOWN = Color.YELLOW;
   private int COLOR_PASS = Color.GREEN;
   private Paint mPaint = null;
   private Paint mWordPaint = null;
   private final static int ROW = 3;
   private final static int LINE = 2;
   private final static int PER_GAP = 3;
   private int per_width = 0;
   private int per_height = 0;
   ArrayList<KeyStruct> mKeyArray = new ArrayList<KeyStruct>();
   public KeyTestView(Context context, AttributeSet attrs) {
       super(context, attrs);
       mPaint = new Paint();
       mPaint.setColor(COLOR_NOR);
       mPaint.setStyle(Style.FILL);
       mWordPaint = new Paint();
       mWordPaint.setColor(Color.WHITE);
   }
 
   @Override
   protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
       super.onLayout(changed, left, top, right, bottom);
       per_width = (this.getWidth() - PER_GAP * (ROW + 1)) / ROW;
       per_height = (this.getHeight() - PER_GAP * (LINE + 1))/ LINE;
   }
   
   @Override
   protected void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       int current_x = PER_GAP;
       int current_y = PER_GAP;
       int i = 0;
       while(i < mKeyArray.size()){
           int color = COLOR_NOR;
           if(mKeyArray.get(i).getIsPass()){
               color = COLOR_PASS;
           }
           if(mKeyArray.get(i).getIsDown()){
               color = COLOR_DOWN;
           }
           mPaint.setColor(color);
           
           canvas.drawRect(current_x, current_y, current_x + per_width, current_y + per_height, mPaint);
           canvas.drawText(mKeyArray.get(i).getKeyName(), current_x, current_y + per_height / 2+5, mWordPaint);
           i ++;
           if(i % ROW == 0){
               current_x = PER_GAP;
               current_y += (per_height + PER_GAP);
           }else{
               current_x += (per_width + PER_GAP);
           }
       }
   }
   
   public void addKey(String name, int code){
       KeyStruct mkey = new KeyStruct(name, code);
       mKeyArray.add(mkey);
   }    
   public void setKeyDown(int code){
       for(KeyStruct key : mKeyArray){
           if(key.getKeyCode() == code){
               key.setIsDown(true);
               break;
           }
       }
       invalidate();
   }
   public void setKeyUp(int code){
       for(KeyStruct key : mKeyArray){
           if(key.getKeyCode() == code){
               key.setIsDown(false);
               break;
           }
       }
       invalidate();
   }
   
   private class KeyStruct{
       private String keyname;
       private int keycode;
       private boolean isdown;
       private boolean pass;
       public KeyStruct(String name, int code){
           this.keyname = name;
           this.keycode = code;
           this.isdown = false;
           this.pass = false;
       }
       public void setIsDown(boolean tmp){
           this.isdown = tmp;
           if(tmp)
               this.pass = true;
       }
       public boolean getIsDown(){
           return this.isdown;
       }
       public boolean getIsPass(){
           return this.pass;
       }
       public String getKeyName(){
           return this.keyname;
       }
       public int getKeyCode(){
           return this.keycode;
       }
   }
}