【问题标题】:Most efficient way to draw Buttons on top of a View?在视图顶部绘制按钮的最有效方法?
【发布时间】:2011-01-29 18:55:25
【问题描述】:

我正在开发的应用程序包含一个填满屏幕的大型自定义视图。我在自定义视图的每一侧的中心放置了一个按钮,可用于在该方向上移动绘制的对象。将自定义视图视为相机的“取景器”,我希望用户能够使用所有四个侧面的按钮来平移取景器。

这些按钮可以正常工作,但是当按下一个按钮时,下面的自定义视图会经历大量重绘(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="&gt;" android:id="@+id/rightButton" android:layout_height="60sp" android:layout_width="40sp" android:layout_centerVertical="true"></Button>
    <Button android:id="@+id/leftButton" android:text="&lt;" 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>

【问题讨论】:

    标签: android layout custom-view


    【解决方案1】:

    为什么按钮是自定义视图的一部分?为什么不让它们成为您的活动布局的一部分,以便布局包含所有按钮和您的自定义视图?

    这可能无法完全解决您的问题,因为如果调用其 onDraw 方法会阻塞 UI 线程,您可能需要进一步优化/重新考虑如何实现自定义视图。

    【讨论】:

    • 对不起,如果我不清楚,按钮是布局的一部分,而不是自定义视图,但它们位于自定义视图的顶部。
    • 您是否在按下按钮时使自定义视图无效?
    • 我是,但是在我的日志中,我可以看到自定义视图被重绘了 4 次而不是所需的 1 次。我想防止按下按钮导致视图重绘,而是手动重绘按下后使视图失效一次。
    • 有不少,你想看哪一部分?
    【解决方案2】:

    我发现实际上我的计算太准确了,而且我的可怜的 Droid 工作太辛苦了。现在一切都很顺利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多