无效仅适用于自定义视图

发布于 2024-10-31 10:43:46 字数 804 浏览 5 评论 0原文

所以我创建了一个名为“drawable view”的视图

 class DrawableView extends View{
        Context mContext;

        int touches=0,k,Xoffs,clicks=0;


  double x_1 = 0,x_2=0;

        private float mLastTouchX, mLastTouchY;

        public DrawableView(Context context) {
        super(context);
        mContext = context;
        }

....
    @Override
    protected void onDraw(Canvas canvas){

        Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

canvas.drawColor(Color.BLUE);
myPaint.setColor(Color.WHITE);

canvas.drawCircle(200, 100, 20, myPaint);


    }

..... more code....

}

,它只能在 ondraw 命令中失效!即:调用“invalidate();” ondraw 命令末尾会导致其循环。

我曾多次尝试调用 g_draw.invalidate();或 g_draw.postInvalidate(); (g_draw是创建的Drawable View的名称)来自其他类甚至主活动类,它不起作用。为什么以及如何解决它?

谢谢

so i created a view called "drawable view"

 class DrawableView extends View{
        Context mContext;

        int touches=0,k,Xoffs,clicks=0;


  double x_1 = 0,x_2=0;

        private float mLastTouchX, mLastTouchY;

        public DrawableView(Context context) {
        super(context);
        mContext = context;
        }

....
    @Override
    protected void onDraw(Canvas canvas){

        Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

canvas.drawColor(Color.BLUE);
myPaint.setColor(Color.WHITE);

canvas.drawCircle(200, 100, 20, myPaint);


    }

..... more code....

}

and it can only be invalidated within the ondraw command! ie: calling "invalidate();" at the end of the ondraw command causes it to loop.

I have tried many times to call g_draw.invalidate(); or g_draw.postInvalidate(); (g_draw is the name of the created Drawable View)from other classes and even the main activity class and it doesnt work. why and how can i fix it?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无敌元气妹 2024-11-07 10:43:46

如果您想连续调用 onDraw,请尝试在另一个线程中执行它。创建一个线程,并从其 run 方法尝试执行 postInvalidate。

它总是对我有用。

另一件事是,当你画一个圆一次时,下一次不会有任何区别 - 它看起来会是一样的。

If you want continious onDraw invoking try doing it in another thread. Create a thread, and from its run method try doing postInvalidate.

It always worked for me.

Another thing is that when you draw a circle once, next time wont make any difference - it will look the same.

浅紫色的梦幻 2024-11-07 10:43:46

您可能想在 DrawableView 类中的某个位置调用 invalidate() 。例如,如果您希望视图在任何触摸事件后重新绘制自身,您可以执行以下操作:

public boolean onTouchEvent( MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_UP){
        invalidate();
    }
}

这就是我在益智游戏中绘制可移动部件的方式。

You may want to call invalidate() somewhere in your DrawableView class. For example, if you want your view to redraw itself after any touch event, you would do something like this:

public boolean onTouchEvent( MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_UP){
        invalidate();
    }
}

This is how I draw the movable pieces in my puzzle game.

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