自定義隨機顏色的圓形小球view隨手指移動

Dewey-W發表於2017-11-03

下面程式碼就可實現 隨機顏色的圓形小球view隨手指移動 效果:


package com.bwie.CustomBallView_RandomColor;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.util.Random;

//自定義顏色的圓形小球隨手指移動view
public class BallActivity extends Activity {
    private int screenW;        //螢幕寬度
    private int screenH;        //螢幕高度
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Display dis = this.getWindowManager().getDefaultDisplay();
        // 設定全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 獲取螢幕寬度
        screenW = dis.getWidth();
        // 獲取螢幕高度
        screenH = dis.getHeight();

        setContentView(new BallView(this));
    }

    //自定義繪圖類
    class BallView extends View{
        private Paint paint;        //定義畫筆
        private float cx = 50;      //圓點預設X座標
        private float cy = 50;      //圓點預設Y座標
        private int radius = 188;
        //定義顏色陣列
        private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED};
        private int paintColor = colorArray[0]; //定義畫筆預設顏色

        public BallView(Context context) {
            super(context);
            //初始化畫筆
            initPaint();
        }
        private void initPaint(){
            paint = new Paint();
            //設定消除鋸齒
            paint.setAntiAlias(true);
            //設定畫筆顏色
            paint.setColor(paintColor);
        }

        //重寫onDraw方法實現繪圖操作
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //將螢幕設定為白色
            canvas.drawColor(Color.WHITE);
            //修正圓點座標
            revise();
            //隨機設定畫筆顏色
            setPaintRandomColor();
            //繪製小圓作為小球
            canvas.drawCircle(cx, cy, radius, paint);
        }

        //為畫筆設定隨機顏色
        private void setPaintRandomColor(){
            Random rand = new Random();
            int randomIndex = rand.nextInt(colorArray.length);
            paint.setColor(colorArray[randomIndex]);
        }

        //修正圓點座標
        private void revise(){
            if(cx <= radius){
                cx = radius;
            }else if(cx >= (screenW-radius)){
                cx = screenW-radius;
            }
            if(cy <= radius){
                cy = radius;
            }else if(cy >= (screenH-radius)){
                cy = screenH-radius;
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // 按下
                    cx = (int) event.getX();
                    cy = (int) event.getY();
                    // 通知重繪
                    postInvalidate();   //該方法會呼叫onDraw方法,重新繪圖
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 移動
                    cx = (int) event.getX();
                    cy = (int) event.getY();
                    // 通知重繪
                    postInvalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    // 抬起
                    cx = (int) event.getX();
                    cy = (int) event.getY();
                    // 通知重繪
                    postInvalidate();
                    break;
            }

            /*
             * 備註1:此處一定要將return super.onTouchEvent(event)修改為return true,原因是:
             * 1)父類的onTouchEvent(event)方法可能沒有做任何處理,但是返回了false。
             * 2)一旦返回false,在該方法中再也不會收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。
             */
            //return super.onTouchEvent(event);
            return true;
        }
    }
}

相關文章