如何通过保持宽高比在ImageView中填充图像?
问题描述:
我看到这个同一主题的许多问题,并通过使用如何通过保持宽高比在ImageView中填充图像?
android:adjustViewBounds="true"
android:scaleType="fitCenter"
还是我的图像显示尝试,因为它我is.If改变
机器人:scaleType =“fitCenter”
到 fitXY
工作正常。但根据文件这一个不保持宽高比。所以我怎样才能改变代码工作如预期?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/card"
android:layout_marginTop="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/card" >
<ImageView
android:id="@+id/idImage"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/783454" />
</LinearLayout>
</LinearLayout>
![在这里输入的形象描述] [1] ![在这里输入的形象描述] [2] 凡IAM缺少概念?
答
请看这段代码,这是我如何缩放位图以适合屏幕。也许这会对你的工作有所帮助并给你提供建议。
private void loadImage() {
ImageView imageView = (ImageView)findViewById(R.id.imageView);
Bitmap imageBitmap = ... load original image bitmap;
Bitmap scaledBitmap = imageBitmap;
// Scaling
int imgSrcHeight = imageBitmap.getHeight();
int imgSrcWidth = imageBitmap.getWidth();
int scaledHeight = 0;
int scaledWidth = 0;
int ctnrHeight = imageView.getMeasuredHeight();
int ctnrWidth = imageView.getMeasuredWidth();
int mHeight = imgSrcHeight - ctnrHeight;
int mWidth = imgSrcWidth - ctnrWidth;
if(mHeight > 0 && mWidth > 0)
{
if(mHeight > mWidth)
{
// scale to fit height
if(mHeight > 0)
{
scaledHeight = ctnrHeight;
// if height < 0 it means it's already inside of content
int coefOverhight = (ctnrHeight * 100)/imgSrcHeight;
scaledWidth = (int)(imgSrcWidth * ((coefOverhight)/100.0));
}
}
else
{
// scale to fit width
if(mWidth > 0)
{
scaledWidth = ctnrWidth;
int coefOverwidth = (ctnrWidth * 100)/imgSrcWidth;
scaledHeight = (int)(imgSrcHeight * ((coefOverwidth)/100.0));
}
}
}
else
{
scaledHeight = imgSrcHeight;
scaledWidth = imgSrcWidth;
}
scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, scaledWidth, scaledHeight, true);
imageView.setImageBitmap(scaledBitmap);
}
什么是您预期的结果? – kcoppock 2012-07-30 16:09:19
像保持宽高比的第二屏。 – ADIT 2012-07-30 16:19:29
你......不能。保持宽高比,左图像的大小可以达到。为了让它更高,你必须使它比屏幕更宽。 – kcoppock 2012-07-30 16:22:15