OnDraw() 方法没有被调用

发布于 2024-10-30 21:08:58 字数 2015 浏览 1 评论 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 中绘制对象。

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-11-06 21:08:59

理想情况下,您需要将 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 the DrawTargetMarker constructor directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文