会移动的小球

package com.baidu.bb;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MySelfBall extends View {

    public MySelfBall(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    private float current_x=40;
    private float current_y=50;
    // 自定义画笔
    Paint paint = new Paint();
 

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        
        // 设置画笔的颜色
        paint.setColor(Color.RED);
        // 绘制一个小球
        canvas.drawCircle(current_x, current_y, 15, paint);

    }
    //重写ontouchevent
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        
        this.current_x=event.getX();
        this.current_y=event.getY();
        this.invalidate();
        return true;
    }

}

原文地址:https://www.cnblogs.com/1995yu/p/5381121.html