我一直在开发一个简单的Android应用程序,其中圈子将在屏幕上弹跳。我现在想添加一个onClick或onClickListener,以便用户可以与图像进行交互。作为Android开发的新手,我不知道如何做到这一点。
有人可以伸出援助之手吗?
以下是大部分代码:
public class MainActivity extends AppCompatActivity {
ArrayList<Circle> circles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View {
public Paint p;
private int w, h;
public MyView(Context context) {
super(context);
p = new Paint();
circles = new ArrayList<>();
circles.add(new Circle(100, Color.GREEN, 150, 200, 8, 8));
circles.add(new Circle(200, Color.BLUE, 500, 500, 4, 7));
circles.add(new Circle(70, Color.RED, 1000, 750, 20, 12));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.w = w;
this.h = h;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
// Drawing circles using loop and canvas.drawCircle()
invalidate();
}
}
}
答案 0 :(得分:0)
在onTouchEvent上使用
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
}
MotionEvent.ACTION_DOWN 新触控已开始
MotionEvent.ACTION_MOVE 手指正在移动
MotionEvent.ACTION_UP 手指上升
MotionEvent.ACTION_CANCEL 当前事件已取消,某事 否则控制了触摸事件
MotionEvent.ACTION_POINTER_DOWN 指针向下(多点触控)
MotionEvent.ACTION_POINTER_UP 指针向上(多点触控)