你有兩個選擇這裏
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;
}
爲Nexus 10的正確的文件夾是**繪製-sw1600dp **,爲Nexus S的**繪製,華電國際** –
好吧,但我敢肯定有更多不同的分辨率不同於繪製的文件夾。那麼有沒有其他方法? – user3367856
通常,對於PHONES,您遵循規則drawable-XYZdpi(XYZ = l | m | h | xh | xxh | xxxh)和TABLETS drawable-swZYXdp(ZYX是px中的較小寬度,因此:...,1024 ,1280,1600,...)。數量有限的平板電腦分辨率以及電話分辨率。 –