在我的 Android 应用程序中,我有一个 Activity,它显示尺寸为 244 x 330
的图像。
我想以全设备宽度显示这些图像。
我的布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView android:id="@+id/news_image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="18dip"
android:layout_marginRight="18dip"
android:background="#aaaaaa" />
</LinearLayout>
</ScrollView>
我尝试设置 ImageView 的 ScaleType,但没有按比例缩放 ImageView 的 ScalingType。 如何在横向模式和纵向模式下按比例缩放图像以适合整个屏幕?
基本上我想要的是 ScaleType.CENTER_CORP 之类的东西,但它还设置了图像的比例高度,这样我就可以看到所有图像,而不仅仅是图像的一部分。
编辑,因为我知道我的“奇怪”任务让您感到困惑。
我想用一张图片给你看。 这就是我目前的布局。我想通过根据需要缩放图像来填充整个灰色区域。我怎样才能做到这一点?
当我将 ScaleType
设置为 CENTER_CROP
时,我得到了这个
但这不是我想要的,因为您看到的不是整个图像,只是中心的一部分。
这就是我想要的:
我希望这能帮助您理解我要完成的任务。 有人知道怎么做吗?
编辑 2:
我试图显示一个高度大于屏幕尺寸的图像,这可能看起来很奇怪而且有点令人困惑,但由于我在我的示例布局中使用了 ScrollView
,所以这应该没问题,如果用户想看到未显示的区域,他可以滚动。
希望这有助于理解我正在尝试做的事情。
最佳答案
我用 fill_parent
和 wrap_content
尝试了 ImageView
中的每个 ScaleType
,但没有一个起作用。我还尝试了我在 Google 上找到的所有内容,但对我也没有任何效果,所以我自己想出了一些办法。
很明显,ImageView 没有像我希望的那样缩放我的图像,所以我必须自己缩放它。缩放位图后,我会将新位图设置为 ImageView 的图像源。这在 G1 和 Motorola Milestone 2 上工作得很好并且看起来非常好。
这是我的所有代码
布局:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/news_wrapper">
<ImageView android:id="@+id/news_image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="18dip"
android:layout_marginRight="18dip"
android:background="#aaaaaa" />
</LinearLayout>
</ScrollView>
Activity
public class ScalingImages extends Activity {
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_margin);
this.imageView = (ImageView)findViewById(R.id.news_image);
// The image is coming from resource folder but it could also
// load from the internet or whatever
Drawable drawable = getResources().getDrawable(R.drawable.img);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
// Get scaling factor to fit the max possible width of the ImageView
float scalingFactor = this.getBitmapScalingFactor(bitmap);
// Create a new bitmap with the scaling factor
Bitmap newBitmap = Util.ScaleBitmap(bitmap, scalingFactor);
// Set the bitmap as the ImageView source
this.imageView.setImageBitmap(newBitmap);
}
private float getBitmapScalingFactor(Bitmap bm) {
// Get display width from device
int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
// Get margin to use it for calculating to max width of the ImageView
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams)this.imageView.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
int rightMargin = layoutParams.rightMargin;
// Calculate the max width of the imageView
int imageViewWidth = displayWidth - (leftMargin + rightMargin);
// Calculate scaling factor and return it
return ( (float) imageViewWidth / (float) bm.getWidth() );
}
}
实用类
public class Util {
public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
int scaleHeight = (int) (bm.getHeight() * scalingFactor);
int scaleWidth = (int) (bm.getWidth() * scalingFactor);
return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
}
}
如果有更好或更准确的方法来完成相同的缩放请告诉我,因为我不敢相信这么微不足道的事情如此难以完成。
我真的希望看到更好的方法来做到这一点。
感谢阅读。
关于android - 将 ImageView 缩放到设备宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4877694/