Oem*_*erA 14 layout android imageview
在我的Android应用程序中,我有一个活动,显示以下大小的图像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>
Run Code Online (Sandbox Code Playgroud)
我尝试设置ImageView的ScaleType,但没有ScalingType可以按比例缩放ImageView.如何在横向模式和纵向模式下按比例缩放图像以适合整个屏幕?
基本上我想要的是像ScaleType.CENTER_CORP,但它也设置图像的比例高度,所以我可以看到所有这些而不仅仅是图像的一部分.
编辑原因我知道我对你的"怪异"任务感到困惑.
我想用图像向你展示.这就是我目前使用布局获得的内容.我希望通过根据需要缩放图像来填充整个灰色区域.我怎么能做到这一点?

当我设置ScaleType为CENTER_CROP我得到这个

但这不是我想要的,因为你没有看到整个图像只是中心的一部分.
这就是我想要的:

我希望这能帮助你理解我想要完成的事情.谁知道怎么做?
编辑2:
它看起来怪异,有点混乱,我试图来显示图像,其在高度上比屏幕尺寸更大,但由于我使用的是ScrollView在我的例子布局,应该没有问题,如果他想用户可以滚动看到未显示的区域.
希望这有助于理解我正在尝试做什么.
Oem*_*erA 29
我想每一个真正ScaleType的我ImageView有fill_parent和wrap_content,但没有一次成功.我也试过了我在Google上找到的所有内容,但对我来说没有任何用处,所以我自己想出了一些东西.
很明显,ImageView没有缩放我的图像,就像我想要缩放,所以我不得不自己缩放它.缩放位图后,我会将新的Bitmap设置为ImageView的图像源.这非常好用,在G1和摩托罗拉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>
Run Code Online (Sandbox Code Playgroud)
活动
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() );
}
}
Run Code Online (Sandbox Code Playgroud)
Util类
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果有更好或更准确的方法来完成相同的缩放,请告诉我,因为我无法相信这样一个微不足道的事情是如此难以实现.
我真的希望看到一个更好的方法来做到这一点.
谢谢你的阅读.
| 归档时间: |
|
| 查看次数: |
37087 次 |
| 最近记录: |