gpt4 book ai didi

android - 摆脱具有每个项目背景的 ListView 中的 overdraw

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:38:15 31 4
gpt4 key购买 nike

我正在使用 ListView,其中的列表项具有背景资源。我想尽可能多地摆脱 overdraw 。我知道 Performance Case Study在 Romain Guy 的博客上发帖,但我无法完全优化 ListView 。简化示例的代码显示在本文底部。该示例基本上只是带有 ListView 的“新 Activity ”向导。我的问题基于此示例。这是初始未优化案例的带有和不带有 overdraw 标记的屏幕截图:

Naive case: 2x-3x overdraw for the entire screen

见证页面有灰色背景(在我的真实项目中是纹理),列表项有白色背景(在我的真实项目中是九色补丁)。 overdraw 是戏剧性的,屏幕的任何部分都被绘制一次,列表项在显示内容之前被绘制三次。

很容易去掉Window中的decor view背景,切掉一层完整的overdraw。如果列表包含足够的项目来填满整个屏幕,我可以砍掉 ListView 背景并找到一个非常好的地方:

Optimized case: backgrounds gone, almost no overdraw

不幸的是,当列表中的项目少于填满整个屏幕时,这将不起作用。如果我不设置任何背景,则列表项下方没有任何内容。如果我在 ListView (或任何父级)上设置背景,我将对所有确实存在的列表项进行完全 overdraw :

Deoptimized case: handles partial lists correctly at the cost of some 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com