自定小球沿着轨迹走动

这里是画了一个8
activity:
private Myview mv;
int chang=0;
private Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==0){
mv.mathPoint(chang);

        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
    mv = (Myview) findViewById(R.id.mv);
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true){
                chang++;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(0);
                    }
                });


                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

自定义view里面

//5.写一个自定义view(8分)
public class Myview extends View {
int x=20;
int y=20;
int d=0;
boolean isup=true;
Path path;
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);
}

@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);
    Paint paint = new Paint();
    path=new Path();
    paint.setColor(Color.GRAY);

    paint.setAntiAlias(true);//抗锯齿
    paint.setStrokeWidth(30);
    //2.画一个圆(5分)
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(200,200,150,paint);
    //2.画一个圆(5分)
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(200,500,150,paint);


    initdraw(canvas);
}
//9.画一个8字型的灰色路径
private void initdraw(Canvas canvas) {
    Paint paint = new Paint();

    int a = new Random().nextInt(255);
    int b = new Random().nextInt(255);
    int c = new Random().nextInt(255);
    paint.setColor(Color.rgb(a,b,c));
    paint.setAntiAlias(true);//抗锯齿



    paint.setStrokeWidth(30);
    //2.画一个圆(5分)

    canvas.drawCircle(x,y,20,paint);





}

public void MyXy(int x,int y){
    this.x=x;
    this.y=y;
    invalidate();
}


int x1=0;
int y1=0;
public  void mathPoint(int num){
    int a0=num+10;
    a0=num%360;
   if (isup){
        x1=(int)(200+150*Math.cos(a0*3.14/180));
        y1=(int)(200+150*Math.sin(a0*3.14/180));
       if (y1==349){
           isup=false;
       }
    }
    else {
     //   a0=(a0-180)*-1;
       x1=(int)(200+150*Math.cos(-a0*3.14/180));
       y1=(int)(500+150*Math.sin(-a0*3.14/180));
       if (y1==350){
           isup=true;
       }
   }
    MyXy(x1,y1);
}

}

你可能感兴趣的:(自定小球沿着轨迹走动)