2012-04-14 118 views
1

我開始拼湊一個遊戲,但對屏幕分辨率和密度有些困惑。縮放位圖以適應854寬屏

我有一個800像素寬的圖像,並已添加到可繪製的mdpi文件夾中。 它得到繪製好,但在854像素寬屏幕上,有一個54像素的差距。

什麼是使圖像適合屏幕的最佳方式是什麼?

感謝

+0

你可以指定哪些PROGRAMM這是在語言/環境? – Mikey 2012-04-14 12:38:55

+0

@Mikey - 谷歌說它的機器人 – Attila 2012-04-14 12:43:57

回答

0

的follwing類添加到您的項目,改變你的佈局像這樣

查看

<my.package.name.AspectRatioImageView 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/my_image" 
    android:id="@+id/my_image" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:adjustViewBounds="true" /> 

package my.package.name; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

/** 
* ImageView which scales an image while maintaining 
* the original image aspect ratio 
* 
*/ 
public class AspectRatioImageView extends ImageView { 

    /** 
    * Constructor 
    * 
    * @param Context context 
    */ 
    public AspectRatioImageView(Context context) { 

     super(context); 
    } 

    /** 
    * Constructor 
    * 
    * @param Context context 
    * @param AttributeSet attrs 
    */ 
    public AspectRatioImageView(Context context, AttributeSet attrs) { 

     super(context, attrs); 
    } 

    /** 
    * Constructor 
    * 
    * @param Context context 
    * @param AttributeSet attrs 
    * @param int defStyle 
    */ 
    public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) { 

     super(context, attrs, defStyle); 
    } 

    /** 
    * Called from the view renderer. 
    * Scales the image according to its aspect ratio. 
    */ 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = width * getDrawable().getIntrinsicHeight()/getDrawable().getIntrinsicWidth(); 
     setMeasuredDimension(width, height); 
    } 
}