public class ImageConvertShowActivity extends Activity {
private ImageView iv_examples, iv_method01, iv_method02;
private static final int WIDTH = 200;
private static final int HEIGHT = 150;
private static final float SHADOW_RADIUS = 12.0f;
private static final Paint SHADOW_PAINT = new Paint();
private static final Paint SCALE_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_size_show);
iv_examples = (ImageView) findViewById(R.id.iv_examples);
iv_method01 = (ImageView) findViewById(R.id.iv_method01);
iv_method02 = (ImageView) findViewById(R.id.iv_method02);
iv_examples.setImageResource(R.drawable.img_01);
Resources resources = getResources();
iv_method01.setImageBitmap(getBitmap1(BitmapFactory.decodeResource(resources, R.drawable.img_01), WIDTH, HEIGHT));
}
public Bitmap getBitmap1(Bitmap bitmap, int width, int height) {
final int bitmapWidth = bitmap.getWidth();
final int bitmapHeight = bitmap.getHeight();
final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);
final int scaledWidth = (int) (bitmapWidth * scale);
final int scaledHeight = (int) (bitmapHeight * scale);
return createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, false, SHADOW_PAINT);
}
private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset, boolean clipShadow, Paint paint) {
Matrix m= new Matrix();
final int width = src.getWidth();
final int height = src.getHeight();
final float sx = dstWidth / (float) width;
final float sy = dstHeight / (float) height;
m.setScale(sx, sy);
Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);
return b;
}
private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset, boolean clipShadow, Paint paint) {
int scaledWidth = width;
int scaledHeight = height;
final Canvas canvas = new Canvas();
canvas.translate(offset / 2.0f, offset / 2.0f);
Bitmap bitmap;
final Rect from = new Rect(x, y, x + width, y + height);
final RectF to = new RectF(0, 0, width, height);
if (m == null || m.isIdentity()) {
bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
paint = null;
} else {
RectF mapped = new RectF();
m.mapRect(mapped, to);
scaledWidth = Math.round(mapped.width());
scaledHeight = Math.round(mapped.height());
bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
canvas.translate(-mapped.left, -mapped.top);
canvas.concat(m);
}
canvas.setBitmap(bitmap);
canvas.drawRect(0.0f, 0.0f, width, height, paint);
canvas.drawBitmap(source, from, to, SCALE_PAINT);
return bitmap;
}
}