自定義控制元件View
阿新 • • 發佈:2018-11-21
先在佈局中寫
<com.luyao.dell.yuan.YurnTableView
android:layout_width="match_parent"
android:layout_height="match_parent" />
然後在定義一個class
不用再MainActivity裡寫東西
public class YurnTableView extends View implements View.OnClickListener { private Paint paint; private int widthPixels; private int heightPixels; private int centerX; private int centerY; private int[] colors; private String[] desc = new String[]{"女生", "性感", "知性", "聰明", "賢惠", "真好"}; private RotateAnimation rotateAnimation; private boolean isRote; public YurnTableView(Context context) { this(context, null); } public YurnTableView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, -1); } public YurnTableView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); widthPixels = displayMetrics.widthPixels; heightPixels = displayMetrics.heightPixels; centerX = widthPixels / 2; centerY = heightPixels / 2; init(context); colors = new int[]{Color.BLUE, Color.GRAY, Color.GREEN, Color.GRAY, Color.BLACK, Color.YELLOW}; //初始化旋轉動畫 initAnimation(); this.setOnClickListener(this); }
private void initAnimation() { rotateAnimation = new RotateAnimation(0, 360, centerX, centerY); rotateAnimation.setDuration(800); rotateAnimation.setFillAfter(true); rotateAnimation.setRepeatCount(-1); rotateAnimation.setInterpolator(new LinearInterpolator()); rotateAnimation.setRepeatMode(Animation.RESTART); } private void init(Context context) { paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(600, 600); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //移動畫布的座標原點 canvas.translate(centerX, centerY); //畫6個圓弧 RectF rectF = new RectF(-300, -300, 300, 300); float start = 60; for (int i = 0; i < 6; i++) { paint.setColor(colors[i]); canvas.drawArc(rectF, start * i, 60, true, paint); } //中心的那個圓 paint.setColor(Color.RED); canvas.drawCircle(0, 0, 100, paint); paint.setColor(Color.WHITE); paint.setTextSize(40); //獲取文字寬度和高度 Rect rect = new Rect(); paint.getTextBounds("start", 0, 5, rect); int width = rect.width(); int height = rect.height(); canvas.drawText("start", -width / 2, height / 2, paint); //繪製圓上的描述資訊 RectF rectF1 = new RectF(-200, -200, 200, 200); for (int i = 0; i <6; i++) { paint.setColor(Color.WHITE); Path path = new Path(); path.addArc(rectF1, start * i + 15, 60); canvas.drawTextOnPath(desc[i], path, 0, 0, paint); } } //點選事件 @Override public void onClick(View view) { if (isRote) { stopAnima(); setRoundDom(); } else { startAnima(); } } private void startAnima() { isRote = true; startAnimation(rotateAnimation); } private void stopAnima() { isRote = false; clearAnimation(); } //給一個隨機的抽獎結果 private void setRoundDom() { double random = Math.random(); RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (360 * random), centerX, centerY); rotateAnimation.setDuration(100); rotateAnimation.setFillAfter(true); startAnimation(rotateAnimation); } }