首页 > 其他分享 >自定义组件,通过回调实现跟随手指的小球

自定义组件,通过回调实现跟随手指的小球

时间:2022-11-24 17:07:18浏览次数:41  
标签:canvas 自定义 小球 event Paint 组件 import android public


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 DrawView extends View {
public float currentX = 40;
public float currentY = 50;
Paint p = new Paint();

public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
p.setColor(Color.RED);
canvas.drawCircle(currentX, currentY, 15, p);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
this.currentX = event.getX();
this.currentY = event.getY();
this.invalidate();
return true;
}

}


标签:canvas,自定义,小球,event,Paint,组件,import,android,public
From: https://blog.51cto.com/u_15890522/5884198

相关文章