android - 在imageview中拟合图像

发布于 2024-12-06 15:52:52 字数 1352 浏览 1 评论 0原文

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

在此处输入图像描述

I'm implementing a widget where i'm trying to display a large image inside an image view (8mpx) like this :

 <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>

In the emulator everything seems ok, but when i deploy to device, i get the "problem loading widget" message.
the emulator is HVGA and my device has a 480x800 resolution.
Any ideea what am i doing wrong ?

Thank you!

==================================================

As advised by you guys i've made a screenshot of the logcat.
Here it is :

enter image description here

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

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

发布评论

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

评论(4

你又不是我 2024-12-13 15:52:52
imageview.setScaleType(ScaleType.CENTER_INSIDE); 
imageview.setScaleType(ScaleType.CENTER_INSIDE); 
述情 2024-12-13 15:52:52

尝试下面的代码

<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"
   />

Try below code

<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"
   />
凉风有信 2024-12-13 15:52:52

用这个

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

use this

imageview.setImageResource(your_image);
imageview.setScaleType(ScaleType.MATRIX);       
暮色兮凉城 2024-12-13 15:52:52

老帖子,但你永远不知道...

logcat 显示了问题:

“分配对于此进程来说太大”

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

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

例如,

假设您的位图是从输入流中获取的:

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);

Old post, but you never know...

The logcat shows the problem:

"allocation too large for this process"

The image you are trying to render is too large to fit into memory. You need to scale the image down, but don't try to create a scaled version of the bitmap or you'll hit the same problem. The solution is the load the Bitmap into memory but ONLY for it's dimensions, then create a new Bitmap with a sample size that reduces the overall size of the image.

Actually you don't even need to load in the image to get it's original size to do this but it often makes sense so you can choose an appropriate sample size.

e.g.

Assuming your Bitmap is obtained from an 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文