在视图顶部绘制按钮最有效的方法?
我正在开发的应用程序由一个充满屏幕的大型自定义视图组成。我在自定义视图每一侧的中心放置了一个按钮,可用于沿该方向移动绘制的对象。将自定义视图视为相机的“取景器”,我希望用户能够使用四个侧面的按钮平移取景器。
这些按钮工作正常,但是当按下一个按钮时,下面的自定义视图会经历大量重绘(重绘 4 次,而不是 1 次),并且 UI 会出现相当大的滞后。
有没有办法阻止自定义视图由于按钮的动画而重绘?
如果我遗漏了一些明显的东西,请原谅我。
预先感谢您的帮助...Android 很棒!
XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.app.CustomView
android:id="@+id/CustomView" android:layout_above="@+id/menuButton" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content" android:layout_below="@+id/CustomView" android:layout_width="wrap_content" android:layout_alignRight="@+id/CustomView" android:id="@+id/toolsButton" android:text="Tools" android:textSize="24sp"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/outButton" android:text="-" android:textSize="30sp" android:layout_toLeftOf="@+id/inButton" android:layout_alignTop="@+id/inButton" android:layout_width="40sp"></Button>
<Button android:layout_height="wrap_content" android:layout_alignRight="@+id/CustomView" android:layout_alignBottom="@+id/CustomView" android:text="+" android:textSize="30sp" android:id="@+id/inButton" android:layout_width="wrap_content"></Button>
<Button android:layout_alignTop="@+id/CustomView" android:layout_centerHorizontal="true" android:layout_height="40sp" android:layout_width="60sp" android:text="^" android:id="@+id/upButton"></Button>
<Button android:layout_alignBottom="@+id/CustomView" android:layout_centerHorizontal="true" android:text="v" android:layout_height="40sp" android:layout_width="60sp" android:id="@+id/downButton"></Button>
<Button android:layout_alignRight="@+id/CustomView" android:text=">" android:id="@+id/rightButton" android:layout_height="60sp" android:layout_width="40sp" android:layout_centerVertical="true"></Button>
<Button android:id="@+id/leftButton" android:text="<" android:layout_height="60sp" android:layout_width="40sp" android:layout_centerVertical="true"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/CustomView" android:id="@+id/menuButton" android:textSize="24sp" android:text="Functions" android:layout_alignParentBottom="true"></Button>
</RelativeLayout>
The app I am working on consists of one large custom view that fills the screen. I placed one button in the center of each side of the custom view that can be used to move the drawn object in that direction. Think of the custom view as a "viewfinder" of a camera and I would like the user to be able to pan the viewfinder with buttons on all four sides.
The buttons work perfectly, but when one is pressed, the custom view underneath undergoes lots of redrawing (4 redraws instead of 1) and the UI lags quite a bit.
Is there some way to stop the custom view from redrawing because of a button's animation?
Forgive me if I am missing something obvious.
Thanks in advance for any help... Android rocks!!!
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.app.CustomView
android:id="@+id/CustomView" android:layout_above="@+id/menuButton" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content" android:layout_below="@+id/CustomView" android:layout_width="wrap_content" android:layout_alignRight="@+id/CustomView" android:id="@+id/toolsButton" android:text="Tools" android:textSize="24sp"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/outButton" android:text="-" android:textSize="30sp" android:layout_toLeftOf="@+id/inButton" android:layout_alignTop="@+id/inButton" android:layout_width="40sp"></Button>
<Button android:layout_height="wrap_content" android:layout_alignRight="@+id/CustomView" android:layout_alignBottom="@+id/CustomView" android:text="+" android:textSize="30sp" android:id="@+id/inButton" android:layout_width="wrap_content"></Button>
<Button android:layout_alignTop="@+id/CustomView" android:layout_centerHorizontal="true" android:layout_height="40sp" android:layout_width="60sp" android:text="^" android:id="@+id/upButton"></Button>
<Button android:layout_alignBottom="@+id/CustomView" android:layout_centerHorizontal="true" android:text="v" android:layout_height="40sp" android:layout_width="60sp" android:id="@+id/downButton"></Button>
<Button android:layout_alignRight="@+id/CustomView" android:text=">" android:id="@+id/rightButton" android:layout_height="60sp" android:layout_width="40sp" android:layout_centerVertical="true"></Button>
<Button android:id="@+id/leftButton" android:text="<" android:layout_height="60sp" android:layout_width="40sp" android:layout_centerVertical="true"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/CustomView" android:id="@+id/menuButton" android:textSize="24sp" android:text="Functions" android:layout_alignParentBottom="true"></Button>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么按钮是自定义视图的一部分?为什么不让它们成为您的活动布局的一部分,以便该布局包含所有按钮和您的自定义视图?
这可能无法完全解决您的问题,因为如果调用其
onDraw
方法会阻塞 UI 线程,您可能需要进一步优化/重新考虑自定义视图的实现方式。Why are the buttons part of the custom view? Why not make them part of your activities layout, so that the layout consists of all the buttons and your custom view?
This may not completely solve your problem, since you may need to further optimize/rethink how your custom view is implemented if invoking its
onDraw
method blocks the UI thread.我发现我的计算实际上太准确了,而且我可怜的机器人也太辛苦了。现在一切都很顺利。
I found that I was actually making my calculations too accurate and was working my poor Droid too hard. Everything draws smoothly now.