无效仅适用于自定义视图
所以我创建了一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想连续调用 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.
您可能想在 DrawableView 类中的某个位置调用 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:
This is how I draw the movable pieces in my puzzle game.