Android - 如何在ImageView中适应图片大小

4

我正在实现一个小部件,尝试在图像视图中显示一个大图像(8mpx),如下所示:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" android:id="@+id/widget"
    android:background="#000000"
    android:padding="15dp"
    android:layout_margin="5dp"
    android:layout_gravity="top|center_vertical|center_horizontal"


    >
<LinearLayout android:background="#ffffff" android:padding="1dp" android:layout_width="wrap_content" android:layout_weight="1"
android:layout_height="wrap_content"  >
     <ImageView 
       android:adjustViewBounds="true"
       android:id="@+id/image"
       android:src="@drawable/sample"
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="top|center_horizontal"
       android:scaleType="fitXY"
       android:maxWidth="200dip"
       android:maxHeight="300dip"
       />
</LinearLayout>

在模拟器中一切似乎都很顺利,但是当我部署到设备上时,出现了“加载小部件时出现问题”的消息。模拟器是HVGA,我的设备分辨率为480x800。我做错了什么吗?谢谢!
按照你们的建议,我已经截取了logcat的屏幕截图。如下图所示:

嗨!是的,我想显示完整的图像并调整大小(同时保持纵横比)。 - maephisto
1
请问您能否编辑您的问题并提供来自logcat的完整错误输出?另外,您是否正在使用硬件加速?如果是,请尝试关闭它并查看发生了什么。我曾尝试使用硬件加速加载非常大的图像,但出现了一个静默的OpenGL错误,指出没有足够的内存来加载图像,而这个错误只在logcat输出中显示。 - Asim Ihsan
是的,logcat 中的错误似乎与内存有关... - maephisto
这个问题听起来很类似... https://dev59.com/UlfUa4cB1Zd3GeqPNP1e - Kevin Burton
4个回答

7
imageview.setScaleType(ScaleType.CENTER_INSIDE); 

谢谢!我尝试了你的想法,但是这样做,我在模拟器和设备上都收到了错误消息。 - maephisto

6
请尝试下面的代码:
<ImageView 
   android:adjustViewBounds="true"
   android:id="@+id/image"
   android:src="@drawable/sample"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_gravity="top|center_horizontal"
   android:scaleType="centerInside"
   />

尝试了一下!上述代码在模拟器和设备上都报错。如果我将weight属性添加到你的XML中,它只能在模拟器上正常工作,在设备上不行。 - maephisto
而不是小部件,我在设备屏幕上收到了“加载小部件出错”的消息。 - maephisto

2

使用这个

imageview.setImageResource(your_image);
imageview.setScaleType(ScaleType.MATRIX);       

谢谢!我尝试了一下,但是这样做,模拟器和设备上都显示错误信息。 - maephisto
在XML中删除android:layout_gravity="top|center_horizontal"和android:scaleType="fitXY"。 - RajaReddy PolamReddy

2

虽然这篇文章有些老,但仍有参考价值...

Logcat 显示了问题:

"allocation too large for this process"

你尝试渲染的图像太大,无法适应内存。你需要缩小图片的尺寸,但不要尝试创建位图的缩放版本,否则会遇到同样的问题。解决方法是将位图加载到内存中,但仅获取其尺寸,然后创建一个新的位图,其中采样大小减小图像的总体大小。

实际上,你甚至不需要加载图像来获取其原始大小以执行此操作,但通常这样做是有意义的,因为你可以选择适当的采样大小。

例如:

假设你的位图是从 InputStream 获取到的:

InputStream in = ... // Your Bitmap Stream

// Decode JUST the dimensions
Options dimensionOptions = new Options();
dimensionOptions.inJustDecodeBounds = true;

BitmapFactory.decodeStream(in, null, dimensionOptions);

// Get the dimensions of the raw
int rawWidth = dimensionOptions.outWidth;

// Choose a target width for the image (screen width would make sense)
int targetWidth = 480;

// Select the sample size which best matches our target size.
// This must be an int
float sampleSize = (float)rawWidth / (float)targetWidth;

// Assume lower, which will result in a larger image
int sample = (int) FloatMath.floor(sampleSize);

// Use this to decode the original image data
Options scaleOptions = new Options();
scaleOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; // 4 bytes per pixel
scaleOptions.inSampleSize = sample;

// Now create a bitmap using the sample size
Bitmap scaled = BitmapFactory.decodeStream(in, null, scaleOptions);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接