OnDraw() 方法没有被调用
// DrawTargetMarker.java
public class DrawTargetMarker extends View {
//Class type variables
private SurfaceHolder mHolder;
private static final String TAG = DrawTargetMarker.class.getSimpleName();
private int mX, mY;
private int mRadius = 10;
//Constructor
public DrawTargetMarker(Context context, int curX, int curY){
super(context);
this.mX = curX;
this.mY = curY;
// Forces the canvas to draw the object
invalidate();
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
// Creating paint instance
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// Setting the paint's style
paint.setStyle(Style.FILL);
// Setting the width of the stroke
paint.setStrokeWidth(5);
// Setting the color of the object to be drawn
paint.setColor(Color.BLACK);
// Draw the circle using the specified paint
canvas.drawCircle(mX, mY, mRadius, paint);
}
}
// RecordShots.java
...
...
....
recordShots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recordCount.setText(String.valueOf(count++));
Log.i(TAG, "Points Recorded ( " + ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_x() + "," + ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_y() + ")");
mDrawTargetMarker = new DrawTargetMarker(getApplicationContext(), ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_x(), ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_y());
// points recorded will be showing two values like (120,20), depending upon the object
// position in the view.
}
});
所以,我的查询是当我单击按钮时,以 (x,y) 的形式向我显示视图中对象的确切位置,但我尝试将 x,y 与上下文一起传递给视图,所以在 x,y 处我需要放置一个对象并重复该过程直到结束。
因此 Control 正在移动到视图,但问题是无法调用 OnDraw() 方法,该方法负责在指定的 x,y 中绘制对象。
// DrawTargetMarker.java
public class DrawTargetMarker extends View {
//Class type variables
private SurfaceHolder mHolder;
private static final String TAG = DrawTargetMarker.class.getSimpleName();
private int mX, mY;
private int mRadius = 10;
//Constructor
public DrawTargetMarker(Context context, int curX, int curY){
super(context);
this.mX = curX;
this.mY = curY;
// Forces the canvas to draw the object
invalidate();
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
// Creating paint instance
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// Setting the paint's style
paint.setStyle(Style.FILL);
// Setting the width of the stroke
paint.setStrokeWidth(5);
// Setting the color of the object to be drawn
paint.setColor(Color.BLACK);
// Draw the circle using the specified paint
canvas.drawCircle(mX, mY, mRadius, paint);
}
}
// RecordShots.java
...
...
....
recordShots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recordCount.setText(String.valueOf(count++));
Log.i(TAG, "Points Recorded ( " + ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_x() + "," + ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_y() + ")");
mDrawTargetMarker = new DrawTargetMarker(getApplicationContext(), ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_x(), ((TargetTrackerGlobalVariable) getApplicationContext()).getCurrent_y());
// points recorded will be showing two values like (120,20), depending upon the object
// position in the view.
}
});
So , My query is when I click on the button, is shows me the exact position of the object in the view in the form of (x,y) but I tried to pass that x,y along with the context to the view, so at that x,y I need to place a object and repeat the process until I end.
So the Control is moving to the view, but the problem is that the OnDraw() method fails to be called, that is responsible for drawing the object in that specified x,y.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,您需要将
DrawTargetMarker
附加到 Activity 的主视图(请参阅 setContentView)。当您使用意图启动 Activity 时,应直接调用 View 的 onDraw() 方法。您不应直接调用DrawTargetMarker
构造函数。Ideally, you need to attach
DrawTargetMarker
to the activity's main view (See setContentView). And when you launch the Activity using an intent, the View's onDraw() method should be called directly. You should not be calling theDrawTargetMarker
constructor directly.