Android Gauge Animation Question

前端 未结 2 776
天涯浪人
天涯浪人 2020-12-08 09:02

Okay so i\'ve been trying to do this for a couple of days and i am getting no where. So i have the following two images:

The First is a RPM Gauge

相关标签:
2条回答
  • 2020-12-08 09:38

    As you already found, i would use clip:

    • draw background image
    • set clip
    • draw foreground image

    I would use

    Canvas.clipPath()
    

    with path looking like pie slice starting in the center of circle, like this:

    Clip path

    To create clip path use something like:

    public class PieView extends View {
    
        private int width = 200;
        private int angleStart = 135;
        private int sweep = 270;
    
        private Path p;
    
        private Paint paint = new Paint();
    
        public PieView(Context context, AttributeSet attrs) {
        super(context, attrs);
            p = new Path();
            //move into center of the circle
            p.setLastPoint(width/2, width/2);
            //add line from the center to arc at specified angle
            p.lineTo(width/2+(float)Math.cos(Math.toRadians(angleStart))*(width/2), 
                     width/2+(float)Math.sin(Math.toRadians(angleStart))*(width/2));
            //add arc from start angle with specified sweep
            p.addArc(new RectF(0, 0, width, width), angleStart, sweep);
            //from end of arc return to the center of circle
            p.lineTo(width/2, width/2);
    
            paint.setColor(Color.RED);
            paint.setStrokeWidth(1);
            paint.setStyle(Style.STROKE);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawRect(0,0,width,width, paint);
            canvas.drawPath(p,paint);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 09:47

    This is how to draw arcs, from Android ApiDemos: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Arcs.html

    Then you need to use xfermode to remove a part of the top image by using a canvas derived from a bitmap. You can see one example of this approach here: Make certain area of bitmap transparent on touch

    0 讨论(0)
提交回复
热议问题