- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 ListView
,其中的列表项具有背景资源。我想尽可能多地摆脱 overdraw 。我知道 Performance Case Study在 Romain Guy 的博客上发帖,但我无法完全优化 ListView 。简化示例的代码显示在本文底部。该示例基本上只是带有 ListView 的“新 Activity ”向导。我的问题基于此示例。这是初始未优化案例的带有和不带有 overdraw 标记的屏幕截图:
见证页面有灰色背景(在我的真实项目中是纹理),列表项有白色背景(在我的真实项目中是九色补丁)。 overdraw 是戏剧性的,屏幕的任何部分都被绘制一次,列表项在显示内容之前被绘制三次。
很容易去掉Window
中的decor view背景,切掉一层完整的overdraw。如果列表包含足够的项目来填满整个屏幕,我可以砍掉 ListView
背景并找到一个非常好的地方:
不幸的是,当列表中的项目少于填满整个屏幕时,这将不起作用。如果我不设置任何背景,则列表项下方没有任何内容。如果我在 ListView (或任何父级)上设置背景,我将对所有确实存在的列表项进行完全 overdraw :
这是次要的坏处,但令人不满意,因为大多数时候我的列表会跑出屏幕。有没有一种可靠的方法可以在 ListView 中的最后一项之后获得背景,而不引入 overdraw ?
Activity :
//MainActivity.java
public class MainActivity extends Activity {
private static final String[] DATA = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet" };
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Optimization 1: getWindow().setBackgroundDrawableResource(android.R.color.transparent);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_main__list_item,
android.R.id.text1, data));
}
}
布局:
<!-- layout/activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#FFDDDDDD"
android:text="Page header information" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFDDDDDD"
android:divider="@null" />
</LinearLayout>
<!-- layout/activity_main__list_item.xml -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@android:color/white" />
最佳答案
我自己想出了一个解决方案,避免了 j__m 的 answer 的嵌套权重问题。 .不利的一面是,它不能轻松地与其他 ListView 自定义组合。这可能比涉及 ListView 高度的 WRAP_CONTENT
的解决方案更有效,因为 ListView 在测量期间不必 getView(...)
其子项。
当然,ListView#onMeasure()
实现非常智能,一旦它有足够的高度来填充其父 View 并转到 getView()
无论如何,在 onDraw()
期间都是相同的位置,因此效率参数似乎没有实际意义。然而,Romain Guy 是 quite insistent ListView 不应使用 WRAP_CONTENT
。
/**
* A list view for use with list items that have their own background drawable. Such list views
* suffer from GPU Overdraw of the item background on top of the list view (ancestor) background.
* <p>
* This subclass detects when its data set contains enough elements to fill all available space
* and start scrolling. If this is the case, it sets its own background to transparent.
* </p>
* <p><strong>Limitation:</strong> Header and Footer views are ignored. If the list view has few
* enough items that it wouldn't scroll, but a header and/or footer are big enough to cause it to
* scroll anyway, the background is not hidden and overdraw is present.
* </p>
* <p>Source: https://stackoverflow.com/q/15625930/49489 CC-BY-SA</p>
*/
public class BackgroundListView extends ListView {
/** We need our own instance, because it's used as a sentinel value later on. */
private static final Drawable TRANSPARENT = new ColorDrawable(0x00000000);
/** If true, the next call to {@code onDraw(Canvas)} evaluates whether to show or hide the background. */
private boolean mNeedsBackgroundCheck = true;
/** The background to restore if the list shrinks. */
private Drawable mOriginalBackground;
public BackgroundListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BackgroundListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BackgroundListView(Context context) {
this(context, null, 0);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
this.mOriginalBackground = this.getBackground();
}
@Override
public void setBackground(Drawable background) {
super.setBackground(background);
Drawable newBackground = getBackground();
if (newBackground != TRANSPARENT) {
this.mOriginalBackground = newBackground;
}
}
@Override
public void setBackgroundResource(int resid) {
super.setBackgroundResource(resid);
Drawable newBackground = getBackground();
if (newBackground != TRANSPARENT) {
this.mOriginalBackground = newBackground;
}
}
@Override
protected void onDraw(Canvas canvas) {
if (mNeedsBackgroundCheck) {
maybeHideBackground();
}
super.onDraw(canvas);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mNeedsBackgroundCheck = true;
};
@Override
protected void handleDataChanged() {
super.handleDataChanged();
mNeedsBackgroundCheck = true;
}
private void maybeHideBackground() {
if (isInEditMode()) {
return;
}
final int maxPosition = getAdapter().getCount() - 1;
final int firstVisiblePosition = getFirstVisiblePosition();
final int lastVisiblePosition = getLastVisiblePosition();
if (firstVisiblePosition > 0 || lastVisiblePosition < maxPosition) {
setBackground(TRANSPARENT);
} else {
setBackground(mOriginalBackground);
}
mNeedsBackgroundCheck = false;
}
}
关于android - 摆脱具有每个项目背景的 ListView 中的 overdraw ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15625930/
我想在不使用函数 m 中的循环的情况下加快计算并获得结果.可重现的例子: N require(rbenchmark) > benchmark(m(r, N), + m1(r, N
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我正在尝试使用 Cython。我使用 setup.py 并构建,而不是让 pyximport 执行此操作。但是,每次我导入模块时,似乎都会调用 pyximport 。 Pyximport 构建失败,一
考虑两个案例 case1 和 case2 以及两个方法 method1 和 method2。假设方法 1 解决案例 1,方法 2 解决案例 2。现在,我有一个程序可能以 case1 或 case2 结
我怎样才能摆脱 CA2202 警告(CA2202:Microsoft.Usage:对象“compressedStream”可以在方法“Compression.InternalDecompress(by
我在这段代码中得到 NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE final Integer id = Ints.tryParse(idString); FailRea
我的 eclipse 中有 Java WebService 代码。我用过@WebService @Webmethod、@XmlElements、@XmlType、@XmlAccessorType 现在
在我正在编写的基于 Sprite 的游戏中,二维网格中的每个字段都包含一堆 Sprite 。大多数情况下,最重要的是最重要的。 在游戏的规则模块中,我有很多这样的代码: public boolean
在 Java 中,我必须设置一个带有值的 POJO 类。然而,要决定使用哪个 setter 函数,我必须取决于 if 条件。我当前的代码如下所示: // Code written in a funct
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭去年。 Improve this
所以我最近接触到了C++中所谓“令人厌恶的函数类型”的怪诞之处(据我所知源自这篇论文: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015
我正在研究配置 QDialog。它有几个类别(General、Appearance 等),当用户点击它们时会加载它们。每个类别都有自己的页面。这些页面本身就是独立的类(每个都有自己的 ui、cpp 和
我一直在开发 Vb.Net应用程序最近,我试图使它尽可能轻量级(即,使二进制文件尽可能小)。 我已经完成了所有琐碎的工作,但是在使用 ILDasm 浏览二进制文件时,我注意到它有一个 My names
An easy way to get rid of *everything* generated by SBT?要求一个简单的方法来清理从 sbt 生成的所有文件,但没有找到。我会要求一个艰难的。我如
如何摆脱默认的 ANTLR 识别错误? 我想用我自己的错误类而不是 ANTLR 的错误来写另一条消息。 我的意思是是否有可能扩展某些 ANTLR 错误类以显示我自己的消息? 更清楚地说,我不想在控制台
使用 woocommerce 的订单面板时,我注意到使用搜索执行了不必要的查询。这是对 Woocommerce 文件(/includes/data-stores/class-wc-order-data
我有一个包含列的大数据框,例如: ID, time, OS, IP 该数据帧的每一行对应一个条目。在某些 ID 的数据框中,存在多个条目(行)。我想摆脱那些多行(显然,对于相同的 ID,其他属性会有所
当我运行测试时,我得到如下代码: objc[8845]: GC: forcing GC OFF because OBJC_DISABLE_GC is set 它还会污染我运行的子流程的输出。我如何摆脱
在 ie8 上,状态栏下方有一个绿色进度指示器,可能表示基于某处某个静态长度值的下载进度。不幸的是,由于“现代”动态 JavaScript、ajax 调用等的性质,该指示器经常会变得困惑,并且栏保持在
我现在正在学习 MVVM,但我了解的东西很少(这里有很多但很少..): 是否每个模型都可能(通过 VM)暴露给 View 有一个 VM? 例如,如果我有一个 Contact 和 Address 实体并
我是一名优秀的程序员,十分优秀!