1

我正在尝试实现像这样的用户界面..

http://www.shrenikvikam.com/wp-content/uploads/2011/04/214e422a43E11S3.png-150x134.png

但是我在实现这个时遇到了一些麻烦。有人可以告诉我这里面的错误吗?

 public class Meter extends View{
static final int ORBIT_COLOR = Color.argb(255, 66, 66, 66);
static final double RAD_CIRCLE = 2*Math.PI;    // Number radians in a circle
private Paint paint;                           // Paint object controlling format of screen draws
private ShapeDrawable planet;                  // Planet symbol
private int planetRadius = 7;                  // Radius of spherical planet (pixels)
private int sunRadius = 12;                    // Radius of Sun (pixels)
private float X0 = 0;                          // X offset from center (pixels)
private float Y0 = 0;                          // Y offset from center (pixels)
private float X;                               // Current X position of planet (pixels)
private float Y;                               // Current Y position of planet (pixels)
private float centerX;                         // X for center of display (pixels)
private float centerY;                         // Y for center of display (pixels)
private float R0;                              // Radius of circular orbit (pixels)
private int nsteps = 120;                      // Number animation steps around circle 
private double theta;                          // Angle around orbit (radians)
private double dTheta;                         // Angular increment each step (radians)
private double direction = -1;                 // Direction: counter-clockwise -1; clockwise +1
private float lastTouchX;    // x coordinate of symbol i at last touch
private float lastTouchY;    // x coordinate of symbol i at last touch
private int divisions = 120; // Since it requires temperature change from 0 -120
private double oneSegmentLength = (2 * Math.PI * R0)/(double)120;


public Meter(Context context) {
    super(context);
    // Initialize angle and angle step (in radians) 
    theta = 30;
    //dTheta = RAD_CIRCLE/((double) nsteps);     // Angle increment in radians
    dTheta  = ((360-60)/(double)divisions);

    planet = new ShapeDrawable(new OvalShape());
    planet.getPaint().setColor(Color.WHITE);
    planet.setBounds(0, 0, 2*planetRadius, 2*planetRadius); 

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setTextSize(14);
    paint.setStrokeWidth(1);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int action = ev.getAction();

    switch (action) {
        // MotionEvent class constant signifying a finger-down event
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
            lastTouchX = x;
            lastTouchY = y;
            newXY();
            break;

        }
        // MotionEvent class constant signifying a finger-drag event
                    case MotionEvent.ACTION_MOVE: {
            float newX = ev.getX();
            float newY = ev.getY();
            float dx = newX - lastTouchX;
            float dy = newY - lastTouchY;
            int diff = (int) (Math.abs(ev.getX()) %  Math.abs(oneSegmentLength));
            if(diff == 0){
                if(Math.abs(dx) > Math.abs(dy)) {
                    if(dx>0) direction = 1;
                    else direction = -1;
                    newXY();
                } else {
                    newXY();
                }
                Log.d("MOVE", "dx ->" + dx + " one seg->" + oneSegmentLength);
                invalidate();
            }
            break;


        }
        // MotionEvent class constant signifying a finger-up event
        case MotionEvent.ACTION_UP: {
            Log.d("ACTION MOVE","Value ->");
            final float x = ev.getX();
            final float y = ev.getY();
            lastTouchX = x;
            lastTouchY = y;
            newXY();
            break;

        }
    }
    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    drawBackground(paint, canvas);
    canvas.save();
    canvas.translate(X + X0, Y + Y0);
    planet.draw(canvas);
    canvas.restore();
}

// Called by onDraw to draw the background
private void drawBackground(Paint paint, Canvas canvas){
    paint.setColor(Color.YELLOW);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(centerX + X0, centerY + Y0, sunRadius, paint);
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(ORBIT_COLOR);
    canvas.drawCircle(centerX + X0, centerY + Y0, R0, paint);
}

//Method to increment angle theta and compute the new X and Y .
private void newXY(){
    theta += dTheta;
    Log.d("THETA VAL", "->" + theta);
    //if(theta > RAD_CIRCLE) theta -= RAD_CIRCLE;  // For convenience, keep angle 0-2pi
    if(theta > 150)theta = 30;

    if(theta > 30 && theta <120){
        X =  (float)(R0*Math.sin(direction*theta)) + centerX - planetRadius;
        Y =  centerY - (float)(R0*Math.cos(direction*theta)) - planetRadius;
    }
    //Log.i("ANIMATOR", "X="+X+" Y="+Y);
}

@Override
protected void onSizeChanged  (int w, int h, int oldw, int oldh){
    // Coordinates for center of screen
    centerX = w/2;
    centerY = h/2;
    // Make orbital radius a fraction of minimum of width and height of display
    R0 = (float) (0.90*Math.min(centerX, centerY));
    oneSegmentLength = (2 * Math.PI * R0)/(double)120;
    // Set the initial position of the planet (translate by planetRadius so center of planet
    // is at this position)
    X = centerX - planetRadius ;
    Y = centerY - R0 - planetRadius;
}

}

我指的是这段代码来做这个实现......

http://eagle.phys.utk.edu/guidry/android/animatorDemo.html

我只是画一个圆圈并尝试在 0 -120 度之间实现相同的运动..

4

0 回答 0