2012-03-20 135 views
3

我有一款應用程序,可以縮放Android手機上多種屏幕尺寸的圖像。調整圖像大小以適應多種屏幕尺寸

佈局以編程方式創建(無xml),我只是在將圖片保存在我的照片編輯器中時定義圖片的大小,例如60x60。

它似乎儘可能在平板電腦上擴大規模,但可能會更大。 我嘗試將圖像大小更改爲100x100,這會使平板電腦屏幕更加平滑,但現在它不會在較小的設備上縮小該圖像......非常令人沮喪。

這裏是我的代碼:

Android Manifest: enabling resizable attribute and supported screens 

     <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.anndconsulting.numbertouch" 
     android:versionCode="1" 
     android:versionName="1.0" 
     android:installLocation="auto" 
     android:resizeable="true" 
     > 
    <supports-screens 
     android:smallScreens="true" 
     android:normalScreens="true" 
     android:largeScreens="true" 
     android:anyDensity="true" 
     /> 

Game.Java代碼:

//create a layout 
    ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    ll.setBackgroundResource(R.drawable.background); 
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.FILL_PARENT)); 

    //create another 5 linear layouts which will host 5 buttons horizontally 
    linearLayout1 = new LinearLayout(this); 
    linearLayout1.setOrientation(LinearLayout.HORIZONTAL); 
    linearLayout1.setGravity(Gravity.CENTER); 

    linearLayout2 = new LinearLayout(this); 
    linearLayout2.setOrientation(LinearLayout.HORIZONTAL); 
    linearLayout2.setGravity(Gravity.CENTER); 

    linearLayout3 = new LinearLayout(this); 
    linearLayout3.setOrientation(LinearLayout.HORIZONTAL); 
    linearLayout3.setGravity(Gravity.CENTER); 

    linearLayout4 = new LinearLayout(this); 
    linearLayout4.setOrientation(LinearLayout.HORIZONTAL); 
    linearLayout4.setGravity(Gravity.CENTER); 

    linearLayout5 = new LinearLayout(this); 
    linearLayout5.setOrientation(LinearLayout.HORIZONTAL); 
    linearLayout5.setGravity(Gravity.CENTER); 

    Then i create a 5x5 array of buttons (which I saved as 100x100) in the drawable  folder 

    for (int i = 0; i < 25; i++) { 
      buttonsa.add(createButton(i)); 
     } 
     Collections.shuffle(buttonsa); 

     //add first 5 buttons to first layout 
      for (int i=0;i<5;i++) { 
      linearLayout1.addView(buttonsa.get(i)); 


     } 
     //add remaining 5 to second layout 
     for (int i=5;i<10;i++){ 
      linearLayout2.addView(buttonsa.get(i)); 

     } 
     for (int i=10;i<15;i++){ 
      linearLayout3.addView(buttonsa.get(i)); 

     } 
     for (int i=15;i<20;i++){ 
      linearLayout4.addView(buttonsa.get(i)); 

     } 
     for (int i=20;i<25;i++){ 
      linearLayout5.addView(buttonsa.get(i)); 

     } 
    ll.addView(linearLayout1); 
     ll.addView(linearLayout2); 
     ll.addView(linearLayout3); 
     ll.addView(linearLayout4); 
     ll.addView(linearLayout5); 

my create button function below: 

private Button createButton(final int i) { 
final Button b = new Button(this); 
int mode = mGameSettings.getInt(GAME_PREFERENCES_GAME_MODE, 0); 
if (mode == 1) { 

    b.setText(" "+letter[i]); 
    b.setWidth(20); <-- this is where I thought I could change but does not 
    b.setHeight(20); <-- same here, just fills the small screen with big image 
          and cuts of the rest. 
    } 
    else { 
    b.setText((i+1) +" "); } 

回答

0

有與多個屏幕大小和密度的多個設備支持的圖像最好的辦法是把你的可繪製文件夾中的60 * 60圖像和drawable-large-hdpi或drawable-large-mdpi文件夾中的100 * 100圖像,或者設備將從中提取可繪製文件的文件夾。這種方式在普通尺寸的手機或任何大屏幕設備上運行應用程序。圖像將從相應的可繪製文件夾中拾取並根據設備密度進行縮放。

相關問題