你好下面是可能會有些幫助您
需要配合圖像
最終長STARTTIME = SystemClock.uptimeMillis() ;
// Part 1: Decode image
Bitmap unscaledBitmap = ScalingUtilities.decodeResource(getResources(), mSourceId,
mDstWidth, mDstHeight, ScalingLogic.FIT);
// Part 2: Scale image
Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, mDstWidth,
mDstHeight, ScalingLogic.FIT);
unscaledBitmap.recycle();
// Calculate memory usage and performance statistics
final int memUsageKb = (unscaledBitmap.getRowBytes() * unscaledBitmap.getHeight())/1024;
final long stopTime = SystemClock.uptimeMillis();
// Publish results
mResultView.setText("Time taken: " + (stopTime - startTime)
+ " ms. Memory used for scaling: " + memUsageKb + " kb.");
mImageView.setImageBitmap(scaledBitmap);
下一步如何裁剪圖像
final long startTime = SystemClock.uptimeMillis();
// Part 1: Decode image
Bitmap unscaledBitmap = ScalingUtilities.decodeResource(getResources(), mSourceId,
mDstWidth, mDstHeight, ScalingLogic.CROP);
// Part 2: Scale image
Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, mDstWidth,
mDstHeight, ScalingLogic.CROP);
unscaledBitmap.recycle();
// Calculate memory usage and performance statistics
final int memUsageKb = (unscaledBitmap.getRowBytes() * unscaledBitmap.getHeight())/1024;
final long stopTime = SystemClock.uptimeMillis();
// Publish results
mResultView.setText("Time taken: " + (stopTime - startTime)
+ " ms. Memory used for scaling: " + memUsageKb + " kb.");
mImageView.setImageBitmap(scaledBitmap);
解碼資源功能
public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
Options options = new 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;
}
calculateSampleSize功能
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;
}
}
}
createScaledBitmap功能
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(),
Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
ScalingLogic功能
public static enum ScalingLogic {
CROP, FIT
}
calculateDstRect功能
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);
}
}