0
我正在開發一個android應用程序,我想減少我的icones。 我已經嘗試了不同的方式,但每次返回的drawable的密度都很低。 但是,當我在photoshop上調整同一圖像時,它會保持良好的密度。 下面的代碼是我找到的最好的。Android:調整可繪製的大小
請問你能幫我改進嗎?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView newImage = (ImageView) findViewById(R.id.image);
Bitmap unscaledBitmap = decodeResource(getBaseContext().getResources(),R.drawable.test2, 120, 120, ScalingLogic.FIT);
newImage.setImageBitmap(createScaledBitmap(unscaledBitmap, 120, 120, ScalingLogic.FIT));
newImage.setImageBitmap(createScaledBitmap(unscaledBitmap, 120, 120, ScalingLogic.FIT));
}
public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
dstHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);
return unscaledBitmap;
}
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static enum ScalingLogic {
CROP, FIT
}
public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float)srcWidth/(float)srcHeight;
final float dstAspect = (float)dstWidth/(float)dstHeight;
if (srcAspect > dstAspect) {
return srcWidth/dstWidth;
} else {
return srcHeight/dstHeight;
}
} else {
final float srcAspect = (float)srcWidth/(float)srcHeight;
final float dstAspect = (float)dstWidth/(float)dstHeight;
if (srcAspect > dstAspect) {
return srcHeight/dstHeight;
} else {
return srcWidth/dstWidth;
}
}
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
final float srcAspect = (float)srcWidth/(float)srcHeight;
final float dstAspect = (float)dstWidth/(float)dstHeight;
if (srcAspect > dstAspect) {
final int srcRectWidth = (int)(srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth)/2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
} else {
final int srcRectHeight = (int)(srcWidth/dstAspect);
final int scrRectTop = (int)(srcHeight - srcRectHeight)/2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float)srcWidth/(float)srcHeight;
final float dstAspect = (float)dstWidth/(float)dstHeight;
if (srcAspect > dstAspect) {
return new Rect(0, 0, dstWidth, (int)(dstWidth/srcAspect));
} else {
return new Rect(0, 0, (int)(dstHeight * srcAspect), dstHeight);
}
} else {
return new Rect(0, 0, dstWidth, dstHeight);
}
}
}
感謝您的回答,但我無法一個一個地定製所有圖像,因爲從文件exctracted約1000圖像.. –