圆形随手指移动

package com.bwie.playbass;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private int screenW;//屏幕宽度
    private int screenH;//屏幕高度
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        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;
        //圆点默认X坐标
        private float cx=50;
        //圆点默认Y坐标
        private float cy=50;
        //定义半径
        private int radius=100;
        //定义画笔颜色
        private int colorArray[] ={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();
            //随机设置画笔颜色
            setPainRandomColor();
            //绘制小圆作为小球
            canvas.drawCircle(cx,cy,radius,paint);
        }
            //为画笔设置随机颜色
        private void setPainRandomColor() {
            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_POINTER_UP:
                    cx=(int)event.getX();
                    cy=(int)event.getY();
                    postInvalidate();
                    break;
            }
            return true;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值