gpt4 book ai didi

android - Android 的 setDrawingCacheEnabled() 是如何工作的?

转载 作者:IT老高 更新时间:2023-10-28 22:22:51 27 4
gpt4 key购买 nike

我有一个用户界面,其中根布局是一个相对布局。它有许多 View ,例如字段、按钮等。

还有其他两个面板最初是不可见的。当用户单击一个按钮时,其中一个面板从左侧滑入,另一个从底部滑入。问题是 Nexus S 的帧速率太差了。

我想使用 setDrawingCacheEnabled(true) 来(我希望)加快这两个面板的动画。作为一个简化的示例,这大致是我在其中一个面板中滑动的方式。我称之为“detailPanel”。

public class MyActivity extends Activity {
// Initially invisible.
private View detailPanel;

// A simple slide animation.
private Animation detailEnterAnimation;

...

public void someButtonClicked(View view) {
detailPanel.startAnimation(detailEnterAnimation);
detailPanel.setVisibility(View.VISIBLE);
detailPanel.setDrawingCacheEnabled(true);
}
}

现在在 DetailPanel 中,我尝试使用缓存。我希望通过缓存渲染位图可以提高性能:

public class DetailPanel extends LinearLayout {
public void draw(Canvas canvas) {
Bitmap cache = getDrawingCache();
if (cache != null) {
canvas.drawBitmap(cache, 0, 0, null);
} else {
super.draw(canvas);
}
}
}

位图非空且大小合适,但全黑。

为什么我的位图是黑色的?那个代码可能是错误的。我已经尝试了一百万种不同的方法,但就是无法弄清楚。

最佳答案

来自 Android 文档 (http://developer.android.com/reference/android/view/View.html#setDrawingCacheEnabled%28boolean%29):

Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers.

也许这是你的情况?

关于android - Android 的 setDrawingCacheEnabled() 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7763532/

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