移动的Andr​​oid查看和preventing的onDraw被称为一遍又一遍 [英] Moving Android View and preventing onDraw to be called over and over again

查看:144
本文介绍了移动的Andr​​oid查看和preventing的onDraw被称为一遍又一遍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我延长了查看级,我称之为 MyView的。 (我增加了一些特性,基本上说的话被绘制的对象,而处理这种)

我移动这个类的一个对象每隔几毫秒,这个伟大工程。我使用 this.layout(左,上,右,下); 移动MyView的。但好像每次视图被移动时,它是重画(的onDraw被调用)。这是必要的吗? MyView的的内容时,它被移动不会改变。事实上,内容永远不会改变 - 它以一种方式创建的,到处移动,后来被破坏

我用的onDraw ,因为每次创建时,我希望它绘制一些文字或它的一些数字。我用来扩展的ImageView ,并使用 setImageResource 但我意识到我想比这只是不同的图像和创建图像的详细各种文本和数字内容这个对象可以有感觉不对。

同样重要的是,我的对象可以接收onTouch事件,今天精细其中工程。

我的onDraw法包含的时刻:(这种情况将会改变,当我得到它不重绘所有的时间)

  @覆盖
公共无效的onDraw(帆布油画){
    涂料P =新的油漆();
    p.setARGB(255,0,255,255);
    canvas.drawRect(0,0,32,32,p)的;
}

所以,问题是:我可以有一个对象,它是的创建时动态绘制,有可能为接收触摸事件(我能解决这个法子所以它的不是很重要),和移动它没有它的重绘(最重要的)?

的原因是:我将这个类的几个对象(可能是100+),我相信,应用程序将被重画的对象所有的时间减慢

编辑:
如果这是不可能的一景,在那里的我可以使用任何替代?


解决方案

  

但好像每次视图被移动时,它是重画(的onDraw被调用)。这是必要的吗?


A 查看对象总是会调用的onDraw 每当它的移动,因为无效的方法必须被调用,使文本或任何你在一个对象绘制将在新的位置涂上。


  

我可以创建时动态绘制的对象,有
  可能收到触摸事件


是的,这取决于你想要达到什么,你可以覆盖的方法<一个href=\"http://developer.android.com/reference/android/view/View.html#onTouchEvent%28android.view.MotionEvent%29\"相对=nofollow> onTouchEvent 在你的类扩展查看或实施<一个href=\"http://developer.android.com/reference/android/view/View.html#setOnTouchListener%28android.view.View.OnTouchListener%29\"相对=nofollow> TouchListener 您的对象

请注意:不建议画(100+)我还记得一些谷歌家伙提到这一点,那么多视图对象,我会的联系,只要我觉得编辑这个

I'm extending the View-class, to what I call MyView. (I've added some properties which basically says what is drawn on the object, and handling this)

I'm moving an object of this class once every few milliseconds, this works great. I'm using this.layout(left, top, right, bottom); to move the MyView. But it seems like every time the view is moved, it is redrawn (onDraw is called). Is this necessary? The contents of MyView does not change when it is moved. In fact, the contents will never change - it's created in one way, moves around, and later gets destroyed.

I'm using onDraw because every time it is created I want it to draw some text or some numbers on it. I used to extend ImageView and use setImageResource but I realized I want more than just different images on this and creating images for all kinds of text and number content this object can have did not feel right.

It is also important that my object can receive onTouch events, which works fine today.

My onDraw-method contains at the moment: (this will change, when I get it to not repaint all the time)

@Override
public void onDraw(Canvas canvas) {
    Paint p = new Paint();
    p.setARGB(255, 0, 255, 255);
    canvas.drawRect(0, 0, 32, 32, p);
}

So the question is: Can I have an object that is drawn dynamically when created, have the possibility to receive touch events (I could solve this another way so it's not very important), and move it around without it being repainted (most important)?

The reason is: I'm going to have several objects (could be 100+) of this class and I believe that the application will be slowed down by repainting the objects all the time.

Edit: If it's not possible with a View, is there any alternative that I can use?

解决方案

But it seems like every time the view is moved, it is redrawn (onDraw is called). Is this necessary?

Yes.

A View object will ALWAYS call onDraw whenever its moved since invalidate method must be called so the text or whatever you are drawing in that object will be painted in the new position.

Can I have an object that is drawn dynamically when created, have the possibility to receive touch events

Yes, depending on what you want to achieve, you can override the method onTouchEvent on your class that extends View or implement a TouchListener to your object

Note: It is not advisable to draw that much view objects (100+) I remember some Google guys mentioning this, I'll edit this with the link as soon as I find it.

这篇关于移动的Andr​​oid查看和preventing的onDraw被称为一遍又一遍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆