android随机小球碰撞(一)边界检测

1. 通过自定义View绘制小球

创建Ball类,封装与小球有关的属性元素
在构造方法中随机生成0-360的方向值

package com.pcf.randomball.bean;

import java.util.HashMap;
import java.util.Random;

public class Ball {
   

    int radius;//半径大小  单位px
    int color;//色值
    int alpha;//透明度
    int x;//圆心x坐标
    int y;//圆心y坐标
    int degree;//方向
    int speed//速度 1-3
    int axisX;//X轴矢量方向值
    int axisY;//Y轴矢量方向值

    public Ball() {
   
        //生成一个在0-360范围内的随机数 为小球的方向
        this.degree = new Random().nextInt(360);
        //生成一个在1-3范围内的随机数 为小球的速度
        this.speed = new Random().nextInt(3)+1;
    }

    public Ball(int radius, int color, int alpha, int x, int y) {
   
        this.radius = radius;
        this.color = color;
        this.alpha = alpha;
        this.x = x;
        this.y = y;
        //生成一个在0-360范围内的随机数 为小球的方向
        this.degree = new Random().nextInt(360);
        //生成一个在1-3范围内的随机数 为小球的速度
        this.speed = new Random().nextInt(3)+1;
        if (degree == 0) {
   
            axisX = 0;
            axisY = 1;
        } else if (degree == 90) {
   
            axisX = 1;
            axisY = 0;
        } else if (degree == 180) {
   
            axisX = 0;
            axisY = -1;
        } else if (degree == 270) {
   
            axisX = -1;
            axisY = 0;
        } else if (degree > 0 && degree < 90) {
   
            axisX = 1;
            axisY = 1;
        } else if (degree > 90 && degree < 180) {
   
            axisX = 1;
            axisY = -1;
        } else if (degree > 180 && degree < 270) {
   
            axisX = -1;
            axisY = -1;
        } else if (degree > 270 && degree < 360) {
   
            axisX = -1;
            axisY = 1;
        }
    }

    public int getRadius() {
   
        return radius;
    }

    public void setRadius(int radius) {
   
        this.radius = radius;
    }

    public int getColor() {
   
        return color;
    }

    public void setColor(int color) {
   
        this.color = color;
    }

    public int getAlpha() {
   
        return alpha;
    }

    public void setAlpha(int alpha) {
   
        this.alpha = alpha;
    }
    
    public int getX() {
   
        return x;
    }

    public void setX(int x) {
   
        this.x = x;
    }

    public int getY() {
   
        return y;
    }

    public void setY(int y) {
   
        this.y = y;
    }

    public int getDegree() {
   
        return degree;
    }

    public void setDegree(int degree) {
   
        this.degree = degree;
    }
    
    public long getSpeed() {
   
        return speed;
    }

    public void setSpeed(long speed) {
   
        this.speed = speed;
    }

    public int getAxisX() {
   
        return axisX;
    }

    public void setAxisX(int axisX) {
   
        this.axisX = axisX;
    }

    public int getAxisY() {
   
        return axisY;
    }

    public void setAxisY(int axisY) {
   
        this.axisY = axisY;
    }
}

创建自定义RandomView类继承自View类
实现构造方法与OnDraw OnLayout OnMeasure方法
在OnLayout方法中获取控件实际宽高
在OnDraw方法里绘制圆形小球

package com.pcf.randomball.bean;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
/**
 * 随机小球View
 * */
public class RandomView extends View {
   
	
    public RandomView(Context context) {
   
        super(context);
    }

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

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

    public RandomView(Context context, AttributeSet attrs,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值