Android View.getDrawingCache 比例位图不正确
我正在为不同高度的消息创建一个列表视图。视图是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧...伙计们,我明白了。这真的很简单,从一开始就应该知道这一点。解决方案是确保将其添加到 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.