How To Work With Bitmap in Android 2
How To Work With Bitmap in Android 2
In Android, you can create bitmaps with anti-aliased rounded corners on the fly using the code snippet
below.
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, rect, rect, paint);
return output;
}
squaredBitmap.recycle();
return bitmap;
}
return bitmap;
}
try {
FileOutputStream fo = new FileOutputStream(f);
fo.write(outStream.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
}
if (getIntent().hasExtra("byteArray")) {
ImageView preview = new ImageView(this);
byte[] byteArray = getIntent().getByteArrayExtra("byteArray");
Bitmap b = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
preview.setImageBitmap(b);
}
iv = (ImageView) findViewById(R.id.iv);
// BG
Paint paintBg = new Paint();
paintBg.setColor(Color.GRAY);
canvas.drawRect(0, 0, 200, 200, paintBg);
// GRADIENT
int gradientWidth = bitmap.getWidth();
int gradientHeight = 40;
// CIRCLE
Paint paintCircle = new Paint();
paintCircle.setColor(Color.GREEN);
paintCircle.setAntiAlias(true);
canvas.drawCircle(100, 100, 40, paintCircle);
Result
How to draw text on Bitmap
iv = (ImageView) findViewById(R.id.iv);
// BG
Paint paintBg = new Paint();
paintBg.setColor(Color.GRAY);
canvas.drawRect(0, 0, 200, 200, paintBg);
iv.setImageBitmap(bitmap);
Result
newWidth = maxWidthAndHeight
// Calculate the new height for the scaled bitmap
newHeight = Math.round(maxWidthAndHeight / ratio)
}else{
val ratio:Float = this.height.toFloat() / this.width.toFloat()
return Bitmap.createScaledBitmap(
this,
newWidth,
newHeight,
false
)
}
// Extension function to resize bitmap using new width value by keeping aspect ratio
fun Bitmap.resizeByWidth(width:Int):Bitmap{
val ratio:Float = this.width.toFloat() / this.height.toFloat()
val height:Int = Math.round(width / ratio)
return Bitmap.createScaledBitmap(
this,
width,
height,
false
)
}
// Extension function to resize bitmap using new height value by keeping aspect ratio
fun Bitmap.resizeByHeight(height:Int):Bitmap{
val ratio:Float = this.height.toFloat() / this.width.toFloat()
val width:Int = Math.round(height / ratio)
return Bitmap.createScaledBitmap(
this,
width,
height,
false
)
}
Rotate a bitmap
imageView.setImageBitmap(compressBitmap(bitmap,5))
}
try{
// Compress the bitmap and save in jpg format
val stream:OutputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream)
stream.flush()
stream.close()
}catch (e:IOException){
e.printStackTrace()
}