如何完美地获得图像

时间:2011-02-14 14:34:25

标签: java android image imageview

在我的应用程序中,当我单击一个按钮时,我应该移动到一个新页面,它会显示一个图像。图像的大小为 480pixels ,高度为 1257像素。在横向模式下图像非常清晰,在纵向模式下,它似乎被拉伸。但我需要双方都完美。

以下是xml代码

<?xml version="1.0" encoding="UTF-8"?>
<AbsoluteLayout
android:id="@+id/finalPage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<LinearLayout android:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"
        android:layout_height="30px"
        android:orientation="vertical"
        >

<ImageView
android:id="@+id/widget41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0px"
android:layout_y="0px"
>
</ImageView>
</LinearLayout>>
</ScrollView>>
</AbsoluteLayout>

这是我的源代码的一部分

public class HelpPage extends Activity 
{
    ImageView BackGroundImage;
    int width,height,orientation;

//  ImageView help;
    public void onCreate(Bundle icicle) 
    {
    super.onCreate(icicle);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

    setContentView(R.layout.help);

    height = displaymetrics.heightPixels;
    width = displaymetrics.widthPixels;

    WebView webview;
    webview = (WebView) findViewById(R.id.webview);
//  webview.loadUrl("file:///android_asset/helptext.html");

    Log.e("FirstImage", "Width = "+width+"Height = "+height+" orientation= "+orientation);

    BackGroundImage = (ImageView) findViewById(R.id.widget41);
    BackGroundImage.setBackgroundResource(R.drawable.help);


     if(width == 320 && height == 480)
     {
         BackGroundImage.setBackgroundResource(R.drawable.help);
     }

     //Landscape mode of 320x480
     else if(width == 480 && height == 320)
     {
         BackGroundImage.setBackgroundResource(R.drawable.help);
     }

     //portrait mode of 480x800
     else if(width == 480 && height == 800)
     {
            BackGroundImage.setBackgroundResource(R.drawable.help);
     }

   //Landscape mode of 480x800
    else if(width == 800 && height == 480)
     {
        BackGroundImage.setBackgroundResource(R.drawable.help1);
     }

   //portrait mode of 480x854
     else if(width == 480 && height == 854)
     {      
       BackGroundImage.setBackgroundResource(R.drawable.help);
     }

   //Landscape mode of 480x854
    else if(width == 854 && height == 480)
     {
        BackGroundImage.setBackgroundResource(R.drawable.help1);
     }


//  help = new ImageView(this);
//  help = (ImageView)findViewById(R.id.helps);
    }
}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

我认为这不是处理此问题的方法,Android可以更轻松地为您完成此操作。我建议你仔细阅读这个android支持页面:http://developer.android.com/guide/practices/screens_support.html

答案 1 :(得分:2)

嗯,首先,您不应该使用AbsoluteLayout,它已被弃用。第二,你为什么要做所有的决议检查?看起来几乎所有人都在执行完全相同的命令。如果您只想以完全相同的尺寸显示图像,只需将ImageView的属性设置为layout_width="wrap_content"layout_height="wrap_content",或者如果您需要将其设置为fill_parent,请设置scaleType="center"。使用center scaleType将图像置于ImageView的边界内,而不应用任何缩放。

检查特定分辨率是一个坏主意;没有严格要求屏幕分辨率是您指定的任何一个。例如,在Galaxy Tab上,那些if语句都不是真的(1024 x 768)。

答案 2 :(得分:0)

尝试将图像放在带水平和垂直滚动条的滚动视图中