2017-09-24 24 views
0

當我創建一個活動來顯示圖像時,分辨率低的圖像只是簡單地佔用它所需的空間,而不僅僅適合屏幕。這是我的活動: The Activity that I created如何將低分辨率圖像放大並適合安卓屏幕?

活動的XML代碼是這樣的:

<LinearLayout 
    android:layout_width="match_parent" 
    android:gravity="center" 
    android:layout_height="match_parent"> 
     <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/nick"/> 
</LinearLayout> 

我想給的ImageView以使圖像適應屏幕與所選圖像的比例 Galary Activity

Galary應用程序中的圖像的分辨率比我在活動中使用的圖像小!那麼這是如何完成的?

回答

0

使用不同的scaleType,您可以將其放大。但是這會導致像素化和其他問題。最好使用更大的圖像並縮小比例(或兩個圖像,全尺寸和縮略圖),而不是縮放大多數圖像。

編輯:好的,重讀您的問題時,縮放類型是不夠的。嘗試使用此作爲自定義視圖:

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 



public class HeightScaleImageView extends ImageView { 

    public HeightScaleImageView(Context context) { 
     super(context); 
    } 

    public HeightScaleImageView(Context context, AttributeSet attributeSet) { 
     super(context, attributeSet); 
    } 

    public HeightScaleImageView(Context context, AttributeSet attributeSet, int defStyle) { 
     super(context, attributeSet, defStyle); 
    } 

    @Override 
    public void setImageResource(int resId) { 
     super.setImageResource(resId); 
     requestLayout(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = 0; 
     int height = 0; 
     //Scale to parent width 
     width = MeasureSpec.getSize(widthMeasureSpec); 
     Drawable drawable = getDrawable(); 
     if (drawable != null) { 
      height = width * getDrawable().getIntrinsicHeight()/getDrawable().getIntrinsicWidth(); 
     } 
     super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); 
    } 
} 
+0

我已經使用了每個縮放選項。沒有用!是像素化是一個問題,但我希望這個功能在我的應用程序。因爲用戶可能會選擇一個像素化圖像。 –

+0

@FebinMathew檢查我的編輯。您是對的,縮放類型不足以將高寬比縮放爲帶有wrap_content高度的match_parent。上面的代碼應該可以工作 –