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
package com.DeviceTest.view;
 
import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
 
public class GsensorBall extends View {
   private final static String TAG = "GsensorBall";
   private Paint mPaint = new Paint();
   private Rect mRect = new Rect();
   
   private float center_point_x = 0;
   private float center_point_y = 0;
   private float draw_center_point_x = 0;
   private float draw_center_point_y = 0;
   private float ball_radius = 0;
   private final static float SCALE = 14.0f;
   private static float MOVESCALE = 12.0f;
   private int currentcolor = Color.RED;
   public GsensorBall(Context context) {
       this(context, null, 0);
   }
 
   public GsensorBall(Context context, AttributeSet attrs) {
       this(context, attrs, 0);
   }
 
   public GsensorBall(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
   }
   
   @Override
   protected void onLayout(boolean changed, int left, int top, int right,
           int bottom) {
       super.onLayout(changed, left, top, right, bottom);
       center_point_x = this.getWidth()/2.0f;
       center_point_y = this.getHeight()/2.0f;
       MOVESCALE = this.getWidth() > this.getHeight() ? this.getHeight()/18.0f : this.getWidth()/18.0f;
       if(draw_center_point_x == 0){
           draw_center_point_x = center_point_x;
           draw_center_point_y = center_point_y;
       }
       ball_radius = this.getWidth() > this.getHeight() ? this.getHeight()/SCALE : this.getWidth()/SCALE;
 
       mPaint.setColor(Color.RED);
       mPaint.setStyle(Style.FILL);
   }
   
   public void setXYZ(float x, float y, float z){
       draw_center_point_x = center_point_x + y * MOVESCALE;
       draw_center_point_y = center_point_y + x * MOVESCALE;
       if(draw_center_point_x != 0 || draw_center_point_y != 0){
           currentcolor = Color.GREEN;
       }
//        Log.d(TAG, "__________________-------- setXYZ(),   draw_center_point_x = " + draw_center_point_x + 
//                "    draw_center_point_y = " + draw_center_point_y);
       this.invalidate();
   }
   
   public void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       //Log.d(TAG, ball_radius+"__________________-------- onDraw(),   draw_center_point_x = " + draw_center_point_x + 
       //        "    draw_center_point_y = " + draw_center_point_y);
       mPaint.setColor(Color.RED);
       canvas.drawCircle(center_point_x, center_point_y, ball_radius / 2, mPaint);
       mPaint.setColor(currentcolor);
       canvas.drawCircle(draw_center_point_x, draw_center_point_y, ball_radius, mPaint);
   }
}