【问题标题】:Is it time-consuming to set TextView background as a drawable for the purpose of having round corners?将 TextView 背景设置为可绘制以具有圆角的目的是否耗时?
【发布时间】:2013-06-28 04:29:39
【问题描述】:

我发现很多网上资源都建议将一个9-patch文件作为TextView的背景,以便让TextView有圆角。

但我认为拉伸图像文件很耗时。

如果我们在 TextView 上方创建一个 LinearLayout,并在左侧有一个小的左圆角可绘制 ImageView,在右侧有一个右圆角可绘制的 ImageView。

然后放一个TextView。

然后使用类似的方法创建一个显示底部两个角的 LinearLayout。

这个解决方案会好一点吗?

【问题讨论】:

  • 我感觉这是premature optimization的情况。使用 9-patch 会更容易且更强大的解决方案。
  • 我会说这是一个过早优化的令人震惊的案例。 @.@ 先测试,如果确实发现性能问题,请修复。
  • 现在您还有两个在线资源推荐 9-patch :)
  • 更不用说,与仅使用 9-patch 相比,我希望向层次结构添加额外的布局和视图会导致性能净损失
  • 为什么不用xml创建圆角背景呢?它可以减小您的应用程序大小并且可以拉伸。

标签: android android-drawable


【解决方案1】:

我主要使用 xml 可绘制对象来创建 ButtonTextView 背景,您只需添加带有属性的 <corners 标记即可为背景添加圆角。如果你准备好使用这种方法,你可以这样做

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BBBBBB" android:endColor="#CCCCCC" android:angle="270"    android:type="linear"/>

    <corners android:radius="5dp"/> <!-- gives your background rounded corners -->
    <stroke android:width="1dp" android:color="#60EEEEEE"/> <!-- creates a border around the image -->
</shape>

【讨论】:

    【解决方案2】:

    用户界面没有固定的脚本,你可以设计不同我可以设计不同。

    使用 9-patch 图像需要时间,因为您需要先创建它们,但它们确实很有帮助,但我更喜欢创建一个可绘制的形状并将其用作 textview 背景

    喜欢这个

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#FFFFFFFF"/>
        <corners android:radius="10px"/>
        <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> 
    </shape>
    

    并使用这个drawable作为我的textviews背景

    【讨论】:

      【解决方案3】:

      只是为了搞笑,我将以下内容放在一起进行性能测试:

      public class OverNineThousandPatchActivity extends Activity {
          boolean flag = false;
      
          public void onCreate (Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              Button button = new Button(this);
              button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
              setContentView(button);
      
              long startTime = SystemClock.uptimeMillis();
              long iterations = 100000;
      
              for (long i = 0; i < iterations; i++) {
                  button.setBackgroundResource(flag ? R.drawable.panel_shadow : R.drawable.progress_overlay);
                  flag = !flag;
              }
      
              long total = SystemClock.uptimeMillis() - startTime;
              Log.d("OverNineThousand", "Total elapsed: " + total + " Individual: " + (total / iterations));
          }
      }
      

      创建一个视图,然后连续 100,000 次将背景设置为两个交替的九个补丁图像。尝试了五次不同的运行:

      Total Time | Time Per setBackgroundResource()
                 |
      5,177 ms   | .05177 ms
      4,793 ms   | .04793 ms
      4,851 ms   | .04851 ms
      4,957 ms   | .04957 ms
      4,957 ms   | .04957 ms
      

      大约 1/20 毫秒。

      我不会担心的。 ;)

      (*完全不是科学测试)

      【讨论】:

      • 谢谢。只是有点想知道:我认为 setBackgroundResource 不能测量实时。渲染在其他地方执行。
      • 很确定它实际上是同步的,但正如我所说,完全不科学。 :P
      猜你喜欢
      • 2021-03-13
      • 2017-07-05
      • 2016-02-21
      • 2015-11-15
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多