Android View.getDrawingCache 比例位图不正确

发布于 2024-10-01 22:11:41 字数 2587 浏览 2 评论 0原文

我正在为不同高度的消息创建一个列表视图。视图是 xml 布局,设置为在所有分辨率屏幕上布局相同。为了提高性能,我通过调用 View.getDrawingCache() 将这些视图转换为位图图像。我正在获取位图图像,但它似乎正在缩放文本,尤其是在高分辨率屏幕中。这使得文本有点模糊,在某些情况下,它的边缘也会被切断。如果我布局视图而不是位图图像,所有内容都会正确缩放,但我没有获得我想要的性能。我的项目 xml 布局如下所示:

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"
    android:padding="5px"
    android:background="#FFFFFF">
    <ImageView android:id="@+id/contact_photo"
        android:layout_width="48dip"
        android:layout_height="48dip"/>
    <ImageView android:id="@+id/network_icon"
        android:layout_alignParentRight="true"
        android:layout_width="16dip"
        android:layout_height="16dip"/>
    <TextView android:id="@+id/created_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
     android:layout_toLeftOf="@+id/network_icon"
     android:textColor="#000000"/>
    <TextView android:id="@+id/sender_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_toRightOf="@+id/contact_photo"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#000000"/>
    <TextView android:id="@+id/summary"
        android:layout_below="@+id/sender_name"
        android:layout_toRightOf="@+id/contact_photo" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:maxLines="4"
        android:textColor="#000000" />
    <LinearLayout 
        android:id="@+id/AttachmentLayout" 
        android:layout_below="@+id/summary"
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_width="wrap_content"/>
</RelativeLayout>

视图测量片段:

LayoutParams params = child.getLayoutParams();
if (params == null) {
    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
final int index = layoutMode == LAYOUT_MODE_ABOVE ? 0 : -1;
child.setDrawingCacheEnabled(true);
addViewInLayout(child, index, params, true);

final int itemWidth = (int)(getWidth() * ITEM_WIDTH);
child.measure(MeasureSpec.EXACTLY | itemWidth, MeasureSpec.UNSPECIFIED);

我还添加了执行视图测量的代码片段。有谁知道如何防止文本缩放?

I am creating a list view for message of different heights. The views are xml layout which are setup to layout the same on all resolution screens. To increase performance I am converting these view into a bitmap image by calling View.getDrawingCache(). I am getting the Bitmap image but it seems to be scaling the text, especially in the high resolution screens. This is leaving the text a little blurry and in some case it is also being cut off at the edges. If I layout the View rather than the bitmap image, everything is scaled correctly but I am not getting the performance i desire. My item xml layout looks like this:

Xml Item View:

<?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"
    android:padding="5px"
    android:background="#FFFFFF">
    <ImageView android:id="@+id/contact_photo"
        android:layout_width="48dip"
        android:layout_height="48dip"/>
    <ImageView android:id="@+id/network_icon"
        android:layout_alignParentRight="true"
        android:layout_width="16dip"
        android:layout_height="16dip"/>
    <TextView android:id="@+id/created_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
     android:layout_toLeftOf="@+id/network_icon"
     android:textColor="#000000"/>
    <TextView android:id="@+id/sender_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_toRightOf="@+id/contact_photo"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#000000"/>
    <TextView android:id="@+id/summary"
        android:layout_below="@+id/sender_name"
        android:layout_toRightOf="@+id/contact_photo" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:maxLines="4"
        android:textColor="#000000" />
    <LinearLayout 
        android:id="@+id/AttachmentLayout" 
        android:layout_below="@+id/summary"
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_width="wrap_content"/>
</RelativeLayout>

View Measure Snippet:

LayoutParams params = child.getLayoutParams();
if (params == null) {
    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
final int index = layoutMode == LAYOUT_MODE_ABOVE ? 0 : -1;
child.setDrawingCacheEnabled(true);
addViewInLayout(child, index, params, true);

final int itemWidth = (int)(getWidth() * ITEM_WIDTH);
child.measure(MeasureSpec.EXACTLY | itemWidth, MeasureSpec.UNSPECIFIED);

I have also added the snippet of code preforming the measurement of the view. Does anyone know how to prevent the text from scaling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尾戒 2024-10-08 22:11:41

好吧...伙计们,我明白了。这真的很简单,从一开始就应该知道这一点。解决方案是确保将其添加到 AndroidManifest.xml 中。我将支持的分辨率设置为任意,因为我的 xml 布局文件设计为自动调整大小。

Ok... folks I figure it out. It was really simple and should have know this from the beginning. The solution is to make sure you add the to the AndroidManifest.xml. I set the supported resolution to any because my xml layout files are designed to automation re-size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文