Android的 - 在画廊根据设备的分辨率调整图像 [英] Android - resize image in gallery based on device resolution

查看:106
本文介绍了Android的 - 在画廊根据设备的分辨率调整图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序从Web图像拉。
然而,当我设置:

I have an Android app that pulls in images from the Web. However when I set:

<supports-screens android:anyDensity="true" />

库出现被设置为所支持的最低分辨率。

the gallery appears to be set to the minimum supported resolution.

下面是画廊布局定义:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout android:id="@+id/GalleryLayout"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="#000000"
              >

<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/gallery"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:spacing="1dip"
         android:gravity="center_vertical|center_horizontal|center"
/>

</LinearLayout>

和getView类:

and the getView class:

ImageView i = new ImageView(mContext);
i.setImageDrawable(drawablesFromUrl[position]);
i.setLayoutParams(new Gallery.LayoutParams(400, 300));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);

有没有一种方法来检测使用的分辨率,然后调整图像画廊,使他们延伸到屏幕的宽度?

Is there a way to detect the resolution used and then resize the images in the gallery so they stretch to the width of the screen?

推荐答案

尝试

i.setAdjustViewBounds(true);

也时,千万别用硬codeD像素值( i.setLayoutParams(新Gallery.LayoutParams(400,300));

使用

int width = (int) getResources().getDimension(R.dimen.image_width)
int height = (int) getResources().getDimension(R.dimen.image_height)
i.setLayoutParams(new Gallery.LayoutParams(width, height));

和一些XML文件(下RES /值)

and in some xml file (under res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="image_height">400dp</dimen>
  <dimen name="image_width">300dp</dimen>
</resources>

这篇关于Android的 - 在画廊根据设备的分辨率调整图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆