简体   繁体   English

调整图像大小以适应多种屏幕尺寸

[英]resizing images to fit multiple screen sizes

I have an app, that scales my images for multiple screen sizes on android phones. 我有一个应用程序,可以在Android手机上为多种屏幕尺寸缩放我的图像。

The layout is created programatically (no xml), I just define the size of the image, when I save it in my photo editor, for example 60x60. 以编程方式创建布局(无xml),我只是定义图像的大小,当我将其保存在我的照片编辑器中时,例如60x60。

It seems to scale up as much as possible on a tablet, but could be bigger. 它似乎在平板电脑上尽可能地扩展,但可能更大。 I tried changing the size of the image to 100x100, this fills the tablet screen a little more, but now it does not scale down that image on smaller devices... very frustrated. 我尝试将图像的大小更改为100x100,这使平板电脑屏幕更加充实,但现在它不会缩小图像在较小的设备上......非常沮丧。

Here is my code: 这是我的代码:

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 Code: 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) +" "); }

Best way to have the image supported in multiple devices with multiple screen sizes and densities is to place your 60*60 image in the drawable folder and 100*100 image in the drawable-large-hdpi or drawable-large-mdpi folder or which ever folder from which your device will pick up the drawable. 在具有多种屏幕尺寸和密度的多个设备中支持图像的最佳方法是将60 * 60图像放在drawable-large-hdpi或drawable-large-mdpi文件夹中的可绘制文件夹和100 * 100图像中您的设备将从中获取可绘制的文件夹。 This way when you run the application in the normal size phone or on any large screen devices. 这种方式在正常大小的手机或任何大屏幕设备上运行应用程序时。 images will be picked up from the respective drawable folders and scale according to the device density. 将从相应的可绘制文件夹中拾取图像并根据设备密度进行缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM