【问题标题】:android - fitting image in imageviewandroid - 在imageview中拟合图像
【发布时间】:2012-04-29 09:55:05
【问题描述】:

我正在实现一个小部件,我试图在图像视图 (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 的屏幕截图。 这里是:

【问题讨论】:

  • 嗨!是的,我想显示调整大小的完整图像(同时保持纵横比)
  • 您能否使用 logcat 的完整错误输出来编辑您的问题?另外,你用的是硬件加速吗?如果您尝试将其关闭,看看会发生什么。我尝试使用硬件加速加载非常大的图像,并且出现一个静默的 OpenGL 错误,抱怨没有足够的内存来加载图像,并且此错误仅显示在 logcat 输出中。
  • 是的,logcat 中的错误似乎与内存有关...
  • 这个问题听起来很像...stackoverflow.com/questions/6265008/…

标签: android image imageview


【解决方案1】:
imageview.setScaleType(ScaleType.CENTER_INSIDE); 

【讨论】:

  • 谢谢!我试过你的想法,但像这样,我在模拟器和设备上都收到错误消息
【解决方案2】:

试试下面的代码

<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 它仅适用于模拟器,而不适用于设备。
  • 问题中描述的相同。而不是小部件,我在设备屏幕上收到“问题加载小部件”消息
【解决方案3】:

使用这个

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

【讨论】:

  • 谢谢!我试过了,但是像这样,我在模拟器和设备上都收到错误消息
  • 删除xml中的android:layout_gravity="top|center_horizo​​ntal" android:scaleType="fitXY"
【解决方案4】:

旧帖,但你永远不知道...

logcat 显示问题:

“分配对于这个进程来说太大了”

您尝试渲染的图像太大而无法放入内存。您需要缩小图像,但不要尝试创建位图的缩放版本,否则您会遇到同样的问题。解决方案是将位图加载到内存中,但仅针对其尺寸,然后创建一个具有样本大小的新位图,以减小图像的整体大小。

实际上,您甚至不需要加载图像即可获得其原始大小,但这样做通常很有意义,因此您可以选择合适的样本大小。

例如

假设您的 Bitmap 是从 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-07
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多