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 にオブジェクトを配置し、終了するまでプロセスを繰り返す必要があります。

したがって、コントロールはビューに移動していますが、問題は、指定された x、y でオブジェクトを描画する責任がある OnDraw() メソッドが呼び出されないことです。

4

1 に答える 1

1

DrawTargetMarker理想的には、アクティビティのメイン ビューにアタッチする必要があります ( setContentViewを参照)。また、インテントを使用してアクティビティを起動するときは、View の onDraw() メソッドを直接呼び出す必要があります。DrawTargetMarkerコンストラクターを直接呼び出すべきではありません。

于 2011-04-08T10:30:50.820 に答える