我四处寻找,仍然找不到一个好的答案。我已经创建了自己的类来扩展 imageView,并且在 onDraw() 方法中我使用画布在我的图像上绘制圆圈。
我现在想做的是在不同位置的图像上绘制一个按钮并为其设置一个 onClick 事件,因此当用户按下该按钮时,它将打开一个新活动。
这是我到目前为止所拥有的......它在正确的位置绘制按钮,除了我的 onClick 方法没有触发
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//1.arrayList Points. 2.arrayLists for points in X, 3.arrayList for points in Y
for(int i=0; i<arrayListPoints.size(); i++){
Button b = new Button(mContext);
LinearLayout ll = new LinearLayout(mContext);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(alPoints_x.get(i), alPoints_y.get(i), 0, 0);
ll.addView(b, layoutParams);
//Measure and layout the linear layout before drawing it
ll.measure(MeasureSpec.getSize(ll.getMeasuredWidth()), MeasureSpec.getSize(ll.getMeasuredHeight()));
ll.layout(0, 0, MeasureSpec.getSize(b.getMeasuredWidth()), MeasureSpec.getSize(b.getMeasuredHeight()));
//Finally draw the linear layout on the canvas
ll.draw(canvas);
//create an onClick event for the button
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast msg = Toast.makeText(mContext, "button clicked \n", Toast.LENGTH_LONG);
msg.show();
} //end of public void
});
}
invalidate();
} //end of onDraw()