2012-04-14 120 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); 
    } 
}