0
// 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 中绘制对象。

4

1 回答 1

1

理想情况下,您需要附加DrawTargetMarker到活动的主视图(请参阅setContentView)。当你使用一个意图启动 Activity 时,应该直接调用 View 的 onDraw() 方法。您不应该DrawTargetMarker直接调用构造函数。

于 2011-04-08T10:30:50.820 回答