自定义View小球

activity:

private MyView v;
int x = 30;
List<Location> list=new ArrayList<>();
int y = 30;
int width;
int att = 0;
  int height;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {
        super.handleMessage(msg);
        if (msg.what==0){
            Random random = new Random();
           // int y = (int) (100 * Math.sin(x * Math.PI / 180) + 400);
            //定义飞行姿态
                            if(att == 0){
                                x++;
                                y++;
                            }
                            if(att == 1){
                                x--;
                                y++;
                            }
                            if(att == 2){
                                x--;
                                y--;
                            }
                            if(att == 3){
                                x++;
                                y--;
                            }
            //改变飞行姿态
                            if(x > 345){
                                if(att == 0){
                                    att = 1;
                                }else{
                                    att = 2;
                                }
                            }
                            if(y > 470){
                                if(att == 1){
                                    att = 2;
                                }else{
                                    att =3;
                                }
                            }
                            if(x < 0){
                                if(att == 2){
                                    att = 3;
                                }else{
                                    att = 0;
                                }
                            }
                            if(y < 0){
                                if(att == 3){
                                    att = 0;
                                }else{
                                    att = 1;
                                }
                            }

            v.setXy(x,y);

            v.invalidate();

        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    height=v.getMeasuredHeight();
            width=v.getMeasuredWidth();
    Log.i("zzz", "onCreate:wwww "+height);
    list.add(new Location(200,300,200,300));
    int width = v.getWidth();
    int height = v.getHeight();
    Log.i("aaa", "onCreate: width"+width);
    Log.i("aaa", "onCreate: height"+height);
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true){
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(0)    ;
                    }
                });
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }).start();

}

private void initView() {
    v = (MyView) findViewById(R.id.view);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    boolean b = super.dispatchTouchEvent(event);
    Log.i("aa", "dispatchTouchEvent: activitydispatchTouchEvent" + b);
    return b;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean b = super.onTouchEvent(event);
    Log.i("aa", "dispatchTouchEvent: activitydionTouchEvent"+b);

    // 自己处理触摸事件
    return b;
}

自定义View 类
@SuppressLint(“AppComPatCu”)
public class MyView extends View {
int x;
int y;
int left;
int right;
int top;
int bottm;
int currentX;
int currentY;
public MyView(Context context) {
super(context);
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    Log.i("ww", "onMeasure: top"+getTop());
    Log.i("ww", "onMeasure: b"+getBottom());
    Log.i("ww", "onMeasure: r"+getRight());
    Log.i("ww", "onMeasure:g "+getLeft());
    Log.i("ww", "onMeasure:h "+getHeight());
    Log.i("ww", "onMeasure:w "+getWidth());
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
}

//950 935
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setStrokeWidth(100);//宽度描边
paint.setAntiAlias(true);//抗锯齿
Random random = new Random();
int a=random.nextInt(255);
int b=random.nextInt(255);
int c=random.nextInt(255);
paint.setColor(Color.rgb(a,b,c));
// paint.setStyle(Paint.Style.STROKE);
// canvas.drawArc(60+x,20+x,30+x,50+x,0,180,true,paint);

    //控制圆心的坐标,不超出画布边界

        canvas.drawCircle(x,y,50,paint);
  // canvas.drawOval(left,top,right,bottm,paint);



  //  paint.setColor(Color.BLACK);
    //   paint.setStyle(Paint.Style.STROKE);
    //  canvas.drawArc(60+x,20+x,30+x,50+x,0,180,true,paint);
//    canvas.drawCircle(100+y,100+x,50,paint);
      //Paint.Style.FILL填充内部
    //Paint.Style.FILL_AND_STROKE填充内部和描边
    //Paint.Style.STROKE只描边
    //   paint.setColor(Color.RED);
    //     setLayerType(LAYER_TYPE_SOFTWARE,null);//硬件加速影响画虚线
   // paint.setPathEffect(new DashPathEffect(new float[]{10,5,5,5},0));//画实线

// canvas.drawLine(80,80,300,80,paint);
}
public void setXy(int x,int y){
this.x=x;
this.y=y;
}

@Override
public boolean onFilterTouchEventForSecurity(MotionEvent event) {
    boolean b = super.onFilterTouchEventForSecurity(event);
    Log.i("aa", "dispatchTouchEvent: onFilterTouchEventForSecurity" + b);
    return b;
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    boolean b = super.dispatchTouchEvent(event);
    Log.i("aa", "dispatchTouchEvent: dispatchTouchEvent" + b);
    return b;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean b = super.onTouchEvent(event);
    Log.i("aa", "dispatchTouchEvent: onTouchEvent"+b);
    this.currentX = (int) event.getX(); //触摸座标X
    this.currentY = (int) event.getY(); //触摸座标Y
    this.invalidate();
    // 自己处理触摸事件
    return b;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值