[英]Scaling ImageView to device width
在我的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>
我嘗試設置ImageView的ScaleType,但沒有ScalingType可以按比例縮放ImageView。 如何在橫向模式和縱向模式下按比例縮放圖像以適合整個屏幕?
基本上我想要的是像ScaleType.CENTER_CORP,但它也設置圖像的比例高度,所以我可以看到所有這些而不僅僅是圖像的一部分。
編輯原因我知道我對你的“怪異”任務感到困惑。
我想用圖像向你展示。 這就是我目前使用布局獲得的內容。 我希望通過根據需要縮放圖像來填充整個灰色區域。 我怎么能做到這一點?
當我設置ScaleType
到CENTER_CROP
我得到這個
但這不是我想要的,因為你沒有看到整個圖像只是中心的一部分。
這就是我想要的:
我希望這能幫助你理解我想要完成的事情。 誰知道怎么做?
編輯2:
我試圖顯示高度比屏幕尺寸更大的圖像可能看起來很奇怪而且有點混亂但是因為我在我的示例布局中使用ScrollView
這應該沒問題並且如果他想要用戶可以滾動看到未顯示的區域。
希望這有助於理解我正在嘗試做什么。
我嘗試使用fill_parent
和wrap_content
在我的ImageView
每個ScaleType
,但沒有一個工作。 我也試過了我在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>
活動
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() );
}
}
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);
}
}
如果有更好或更准確的方法來完成相同的縮放, 請告訴我,因為我無法相信這樣一個微不足道的事情是如此難以實現。
我真的希望看到一個更好的方法來做到這一點。
謝謝你的閱讀。
最簡單的方法是將android:adjustViewBounds =“true”添加到ImageView並將比例類型設置為“fitCenter”
我在各地搜索,無法找到解決方案。 這是我做的,對我有用,並帶有滾動視圖。
XML文件:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<ImageView
android:id="@+id/manga_page_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
</ScrollView>
確切地說,對你正在尋找的東西有點困惑。 如果你正在縮放以適應屏幕,你有三個選項,如果你保持比例,其中兩個是可行的。 您可以使用ScaleType fitCenter,它會使圖像按比例適合邊界,或者您可以使用centerCrop(您說過您嘗試過),它將拉伸圖像的最短邊以適合容器(意味着一些會被裁掉)在較長的維度上)。 如果沒有裁剪或不成比例地伸展,你不能伸展以適應寬度和高度。
編輯 :好的,我想我現在明白你的意思了。 首先,我將LinearLayout設置為wrap_content作為高度。 其次,在代碼中,這是我能想到的一種方法。 您可以通過首先獲取屏幕尺寸,然后使用新尺寸執行createScaledBitmap
,然后設置背景資源來實現此目的。
final ImageView newsImage = (ImageView)findViewById(R.id.news_image);
//get details on resolution from the display
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//going to set what happens as the layout happens
newsImage.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
int oldHeight, oldWidth, newHeight, newWidth;
//we want the new width to be as wide as the screen
newWidth = metrics.widthPixels;
oldHeight = newsImage.getDrawable().getIntrinsicHeight();
oldWidth = newsImage.getDrawable().getIntrinsicWidth();
//keeping the aspect ratio, the new height should be w1/h1 = w2/h2
newHeight = Math.floor((oldHeight * newWidth) / oldWidth);
LinearLayout.LayoutParams params =
(LinearLayout.LayoutParams)newsImage.getLayoutParams();
params.height = newHeight;
params.width = newWidth;
newsImage.setLayoutParams(params);
newsImage.setScaleType(ScaleType.CENTER);
newsImage.invalidate();
newsImage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}
您是否嘗試過通過ImageView.setScaleType()
編程方式進行設置?
setAdjustViewBounds( false )
也可能有所幫助。
編輯:抱歉,我誤讀了這個問題。 無論如何, setAdjustViewBounds()
仍然可能有所幫助。 從來沒有用過它。
我想以完整的設備寬度顯示這些圖像。
這很簡單。 您只有錯誤的邊距設置。 刪除這些行,您的圖像將以完整的設備寬度顯示:
android:layout_marginLeft="18dip"
android:layout_marginRight="18dip"
我不再看到圖像了,問題已經過時了,以防萬一其他人發現這個並且有同樣的問題。 獲取float screenWidth = getResources().getDisplayMetrics().widthPixels
並以編程方式設置ImageView LayoutParamters以將圖像縮放到screenWidth
。 只是一個想法!!
設法實現我想要的,希望與你的目標一致:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pgviewer);
ImageView PgContainer = (ImageView)findViewById(R.id.imageView1);
String Page = String.valueOf(getIntent().getExtras().getInt("Page"));
try {
PgContainer.setImageBitmap(getBitmapFromAsset(Page));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PgContainer.setScaleType(ScaleType.FIT_CENTER);
PgContainer.scrollTo(0, 0);
PgContainer.setScrollBarStyle(0);
}
然后縮放位圖:
private Bitmap getBitmapFromAsset(String strName) throws IOException
{
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(strName+".png");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
float screenWidth = getResources().getDisplayMetrics().widthPixels;
int ih=bitmap.getHeight();
int iw=bitmap.getWidth();
float scalefactor = screenWidth/iw;
//w = 480, h=~
//int newh = bitmap.getHeight()/bitmap.getWidth();
return android.graphics.Bitmap.createScaledBitmap(bitmap, (int)(iw*scalefactor), (int)(ih*scalefactor), true);
//return bitmap;
}
其中一種方法是將android:adjustViewBounds =“true”設置為ImageView,並將縮放類型設置為xml文件中的“fitCenter”。 這應該按預期工作。 您也可以通過設置ImageView.adjustViewBounds(true)和ImageView.setScaleType(ScaleType.FIT_CENTER)以編程方式執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.