自定義View 圓盤
阿新 • • 發佈:2018-12-15
佈局activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.example.paint.view.CustomDiskView android:id="@+id/disk" android:layout_width="wrap_content" android:layout_height="match_parent" /> <com.example.paint.view.CustomBeginView android:id="@+id/begin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/disk" android:layout_marginLeft="160dp" android:layout_marginTop="145dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
外面的大圓盤
public class CustomDiskView extends View { Paint paint; int degress=0; RectF rectF; int textdegress=15; public CustomDiskView(Context context) { super(context); } public CustomDiskView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //外邊大圓盤 String[] strings=new String[]{"一","二","三","四","五","六"};//定義文字 int[] colors=new int[]{Color.YELLOW,Color.DKGRAY,Color.CYAN,Color.GREEN,Color.LTGRAY,Color.BLUE};//定義顏色 for(int i=0;i<6;i++){ paint.setColor(colors[i]);//每個扇形的顏色都不相同 Path path=new Path(); paint.setTextSize(24);//設定文字的大小 canvas.drawArc(rectF,degress,60,true,paint); path.addArc(rectF,textdegress,60); paint.setColor(Color.BLACK);//寫文字 canvas.drawTextOnPath(strings[i],path,60,60,paint); degress+=60; textdegress+=60; } } private void init() { paint=new Paint(); paint.setStrokeWidth(10); paint.setStyle(Paint.Style.FILL); rectF=new RectF(); rectF.left=100; rectF.right=600; rectF.top=100; rectF.bottom=600; } }
裡面小圓加箭頭
public class CustomBeginView extends View { Paint paint; public CustomBeginView(Context context) { super(context); } public CustomBeginView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //自定義寬和高 setMeasuredDimension(120,150); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //中心圖形 和文字 paint.setColor(Color.BLACK); canvas.drawArc(0,-120,120,120,60,60,true,paint); paint.setColor(Color.RED); canvas.drawCircle(60,90,60,paint); paint.setColor(Color.BLACK); canvas.drawText("start",10,100,paint); } private void init() { //例項化 paint=new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(10); paint.setTextSize(50); paint.setStyle(Paint.Style.FILL); } }
MainActivity
public class MainActivity extends AppCompatActivity {
private CustomDiskView customDiskView;
private CustomBeginView customBeginView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customDiskView = findViewById(R.id.disk);
customBeginView = findViewById(R.id.begin);
customBeginView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//隨機數旋轉
float degress= (float) (720+Math.random()*1000);
//旋轉動畫
RotateAnimation rotateAnimation = new RotateAnimation(0,degress,350,350);
rotateAnimation.setDuration(5000);//動畫時長
rotateAnimation.setFillAfter(true);//保持動畫完之後的效果
customDiskView.setAnimation(rotateAnimation);
customDiskView.startAnimation(rotateAnimation);
}
});
}
}
效果圖 點中間小圓 外面大圓旋轉