//Add Private
private Context mContext;
private android.content.res.Resources mResources;
//onCreate
mContext = getApplicationContext();
mResources = getResources();
//onButton
// Bitmap to draw on the canvas
Bitmap bitmap = BitmapFactory.decodeResource(
mResources,
R.drawable.icon
);
// Define an offset value between canvas and bitmap
int offset = 50;
// Initialize a new Bitmap to hold the source bitmap
Bitmap dstBitmap = Bitmap.createBitmap(
bitmap.getWidth() + offset * 2, // Width
bitmap.getHeight() + offset * 2, // Height
Bitmap.Config.ARGB_8888 // Config
);
// Initialize a new Canvas instance
Canvas canvas = new Canvas(dstBitmap);
// Draw a solid color on the canvas as background
canvas.drawColor(Color.LTGRAY);
canvas.drawBitmap(
bitmap, // Bitmap
offset, // Left
offset, // Top
null // Paint
);
// Display the newly created bitmap on app interface
imageview1.setImageBitmap(dstBitmap);
Komentar