2014-03-01 48 views
0

我在Nexus S上的按钮(480x800)看起来不错。它需要比屏幕高度的四分之一少。当我在Nexus 10(2560x1600)上打开我的应用程序时,该按钮看起来要小得多。 我正在寻找一种方法,我的按钮按比例正确缩放到屏幕大小。因此,我的按钮甚至在Nexus 10上占据了与Nexus S上相同的空白屏幕空间。我知道有可绘制的文件夹,但它们不能正确解决我的问题。例如:Nexus 10和Galaxy Nexus都是xhdpi。虽然Nexus 10的屏幕比较大。所以我的按钮会在这些设备上占用不同的空间。按比例缩放元素的屏幕尺寸

谢谢!

+0

为Nexus 10的正确的文件夹是**绘制-sw1600dp **,为Nexus S的**绘制,华电国际** –

+0

好吧,但我敢肯定有更多不同的分辨率不同于绘制的文件夹。那么有没有其他方法? – user3367856

+0

通常,对于PHONES,您遵循规则drawable-XYZdpi(XYZ = l | m | h | xh | xxh | xxxh)和TABLETS drawable-swZYXdp(ZYX是px中的较小宽度,因此:...,1024 ,1280,1600,...)。数量有限的平板电脑分辨率以及电话分辨率。 –

回答

0

你有两个选择这里

1.有效使用可绘制文件夹 的各种绘制的文件夹有一个与之相关的尺寸密度参数。例如,这些是根据尺寸参数的变化:ldpi = 240X320,mdpi = 320X480 hdpi = 480X800,并且类似明智。另一个参数是密度:mdpi:160 dpi,hdpi:240-dpi,xhdpi:320-dpi。

您需要在PhotoShop中设计图像,记住这些参数,然后将相应的图像放入适当的可绘制变体(drawable-large-ldpi,drawable-xlarge-mdpi等)中。我的意思是,在photoShop或其他图像编辑软件中,您需要为绘图画布指定像素密度和尺寸参数(如上所述),然后在该画布上创建图像。 (希望你明白我的意思:)

2.通过编程缩放图像 我已经在图像缩放问题绊了一些,每次在我的项目时间,由于缺乏时间(和懒惰)我将会满足于不是最佳解决方案。但最近我发现了一些时间来打击这个特殊问题。这是我的解决方案,我希望它也能帮助你。

Bitmap scaleDownLargeImageWithAspectRatio(Bitmap image) 
     { 
      int imaheVerticalAspectRatio,imageHorizontalAspectRatio; 
      float bestFitScalingFactor=0; 
      float percesionValue=(float) 0.2; 

      //getAspect Ratio of Image 
      int imageHeight=(int) (Math.ceil((double) image.getHeight()/100)*100); 
      int imageWidth=(int) (Math.ceil((double) image.getWidth()/100)*100); 
      int GCD=BigInteger.valueOf(imageHeight).gcd(BigInteger.valueOf(imageWidth)).intValue(); 
      imaheVerticalAspectRatio=imageHeight/GCD; 
      imageHorizontalAspectRatio=imageWidth/GCD; 
      Log.i("scaleDownLargeImageWIthAspectRatio","Image Dimensions(W:H): "+imageWidth+":"+imageHeight); 
      Log.i("scaleDownLargeImageWIthAspectRatio","Image AspectRatio(W:H): "+imageHorizontalAspectRatio+":"+imaheVerticalAspectRatio); 

      //getContainer Dimensions 
      int displayWidth = getWindowManager().getDefaultDisplay().getWidth(); 
      int displayHeight = getWindowManager().getDefaultDisplay().getHeight(); 
      //I wanted to show the image to fit the entire device, as a best case. So my ccontainer dimensions were displayWidth & displayHeight. For your case, you will need to fetch container dimensions at run time or you can pass static values to these two parameters 

      int leftMargin = 0; 
      int rightMargin = 0; 
      int topMargin = 0; 
      int bottomMargin = 0; 
      int containerWidth = displayWidth - (leftMargin + rightMargin); 
      int containerHeight = displayHeight - (topMargin + bottomMargin); 
      Log.i("scaleDownLargeImageWIthAspectRatio","Container dimensions(W:H): "+containerWidth+":"+containerHeight); 

      //iterate to get bestFitScaleFactor per constraints 
      while((imageHorizontalAspectRatio*bestFitScalingFactor <= containerWidth) && 
        (imaheVerticalAspectRatio*bestFitScalingFactor<= containerHeight)) 
      { 
       bestFitScalingFactor+=percesionValue; 
      } 

      //return bestFit bitmap 
      int bestFitHeight=(int) (imaheVerticalAspectRatio*bestFitScalingFactor); 
      int bestFitWidth=(int) (imageHorizontalAspectRatio*bestFitScalingFactor); 
      Log.i("scaleDownLargeImageWIthAspectRatio","bestFitScalingFactor: "+bestFitScalingFactor); 
      Log.i("scaleDownLargeImageWIthAspectRatio","bestFitOutPutDimesions(W:H): "+bestFitWidth+":"+bestFitHeight); 
      image=Bitmap.createScaledBitmap(image, bestFitWidth,bestFitHeight, true); 

      //Position the bitmap centre of the container 
      int leftPadding=(containerWidth-image.getWidth())/2; 
      int topPadding=(containerHeight-image.getHeight())/2; 
      Bitmap backDrop=Bitmap.createBitmap(containerWidth, containerHeight, Bitmap.Config.RGB_565); 
      Canvas can = new Canvas(backDrop); 
      can.drawBitmap(image, leftPadding, topPadding, null); 

      return backDrop; 
     }