package com.example.test; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * * @author Young * */ public class FiveView2 extends View { private float position=-1; private Paint paint; public FiveView2(Context context) { super(context); init(); } public FiveView2(Context context, AttributeSet attrs) { super(context, attrs); init(); } public FiveView2(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @SuppressLint("NewApi") public FiveView2(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } private void init() { paint=new Paint(); paint.setAntiAlias(true); paint.setColor(Color.YELLOW); paint.setStrokeWidth(5); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width=getWidth()-getPaddingLeft()-getPaddingRight(); float r=width/2.0f/5.0f; float outR=r*0.9f; float inR=outR*sin(18)/sin(180-36-18); canvas.translate(getPaddingLeft()+r , getHeight()/2.0f ); Path completePath = getCompletePath(outR, inR); Path halfPath = getHalfPath(outR, inR); for (int i =0; i < 5; i++) { canvas.rotate(-18); paint.setStyle(Style.STROKE); canvas.drawPath(completePath, paint); if (position-getPaddingLeft()<2*r*i) { }else if ((position-getPaddingLeft())<2*r*i+r) { paint.setStyle(Style.STROKE); canvas.drawPath(completePath, paint); paint.setStyle(Style.FILL); canvas.drawPath(halfPath, paint); }else{ paint.setStyle(Style.FILL); canvas.drawPath(completePath, paint); } canvas.rotate(18); canvas.translate(2*r ,0 ); } } @Override public boolean onTouchEvent(MotionEvent event) { position=event.getX(); invalidate(); return true; } private Path getHalfPath(float outR, float inR) { Path path; path=new Path(); path.moveTo(outR*cos(72*4), outR*sin(72*4)); path.lineTo(inR*cos(72*1+36), inR*sin(72*1+36)); path.lineTo(outR*cos(72*2), outR*sin(72*2)); path.lineTo(inR*cos(72*2+36), inR*sin(72*2+36)); path.lineTo(outR*cos(72*3), outR*sin(72*3)); path.lineTo(inR*cos(72*3+36), inR*sin(72*3+36)); path.close(); return path; } private Path getCompletePath(float outR, float inR) { Path path=new Path(); path.moveTo(outR*cos(72*0), outR*sin(72*0)); path.lineTo(inR*cos(72*0+36), inR*sin(72*0+36)); path.lineTo(outR*cos(72*1), outR*sin(72*1)); path.lineTo(inR*cos(72*1+36), inR*sin(72*1+36)); path.lineTo(outR*cos(72*2), outR*sin(72*2)); path.lineTo(inR*cos(72*2+36), inR*sin(72*2+36)); path.lineTo(outR*cos(72*3), outR*sin(72*3)); path.lineTo(inR*cos(72*3+36), inR*sin(72*3+36)); path.lineTo(outR*cos(72*4), outR*sin(72*4)); path.lineTo(inR*cos(72*4+36), inR*sin(72*4+36)); path.close(); return path; } private float cos(int num){ return (float) Math.cos(num*Math.PI/180); } private float sin(int num){ return (float) Math.sin(num*Math.PI/180); } }