android - How to rotate a Bitmap on Canvas center








MainActivity.java



package com.cfsuman.androidtutorials;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
private Resources mResources;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the Resources
mResources = getResources();

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ImageView imageView = findViewById(R.id.imageView);


// Set a click listener for Button widget
button.setOnClickListener(view -> {
// Get the source bitmap to draw on canvas
Bitmap srcBitmap = BitmapFactory.decodeResource(
mResources,
R.drawable.rose_small
);

// Initialize a new Bitmap
Bitmap bitmap = Bitmap.createBitmap(
1400, // Width
1000, // Height
Bitmap.Config.ARGB_8888 // Config
);

// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);

// Draw a solid color on the canvas as background
canvas.drawColor(Color.LTGRAY);

// Initialize a new Paint instance to draw on canvas
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);

// Initialize a new Matrix instance
Matrix matrix = new Matrix();

// Set rotation on matrix
matrix.setRotate(
45, // degrees
(float) srcBitmap.getWidth() / 2, // px
(float)srcBitmap.getHeight() / 2 // py
);

// Draw the bitmap at the center position of the canvas
// both vertically and horizontally
matrix.postTranslate(
(float) canvas.getWidth() / 2
- (float) srcBitmap.getWidth() / 2, // dx
(float) canvas.getHeight() / 2
- (float) srcBitmap.getHeight() / 2 // dy
);


// Finally, draw the bitmap on canvas as a rotated bitmap
canvas.drawBitmap(
srcBitmap, // Bitmap
matrix, // Matrix
paint // Paint
);

// Display the newly created bitmap on app interface
imageView.setImageBitmap(bitmap);
});
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>









android - How to rotate a Canvas








MainActivity.java



package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ImageView imageView = findViewById(R.id.imageView);


// Set a click listener for Button widget
button.setOnClickListener(view -> {
// Initialize a new Bitmap
Bitmap bitmap = Bitmap.createBitmap(
1200, // Width
600, // Height
Bitmap.Config.ARGB_8888 // Config
);

// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);

// Draw a solid color on the canvas as background
canvas.drawColor(Color.LTGRAY);

// Initialize a new Paint instance to draw on canvas
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#676767"));
paint.setAntiAlias(true);

// Set an offset value in pixels to draw rectangle on canvas
int offset = 200;

// Save the canvas state
canvas.saveLayer(
0, // left
0, // top
canvas.getWidth(),
canvas.getHeight(),
null // paint
);

// Rotate the canvas
canvas.rotate(
25, // degrees
(float) canvas.getWidth() / 2, // px, center x
(float) canvas.getHeight() / 2 // py, center y
);

// Draw the rounded corners rectangle object on the canvas
// The rectangle will be drawn as a 25 degrees rotated rectangle
canvas.drawRect(
offset, // left
offset, // top
canvas.getWidth() - offset, // right
canvas.getHeight() - offset, // bottom
paint // Paint
);

// Finally, restore the canvas state
canvas.restore();

// Display the newly created bitmap on app interface
imageView.setImageBitmap(bitmap);
});
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>








android - How to draw Rounded Rectangle on Canvas








MainActivity.java



package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ImageView imageView = findViewById(R.id.imageView);


// Set a click listener for Button widget
button.setOnClickListener(view -> {
// Initialize a new Bitmap
Bitmap bitmap = Bitmap.createBitmap(
1200, // Width
600, // Height
Bitmap.Config.ARGB_8888 // Config
);

// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);

// Draw a solid color on the canvas as background
canvas.drawColor(Color.LTGRAY);

// Initialize a new Paint instance to
// draw the rounded rectangle
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#676767"));
paint.setAntiAlias(true);

// Set an offset value in pixels to draw
// rounded rectangle on canvas
int offset = 75;

// Initialize a new RectF instance
RectF rectF = new RectF(
offset, // left
offset, // top
canvas.getWidth() - offset, // right
canvas.getHeight() - offset // bottom
);

// Define the corners radius of rounded rectangle
int cornersRadius = 50;

// Finally, draw the rounded corners
// rectangle object on the canvas
canvas.drawRoundRect(
rectF, // rect
cornersRadius, // rx
cornersRadius, // ry
paint // Paint
);

// Display the newly created bitmap on app interface
imageView.setImageBitmap(bitmap);
});
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Rounded Rectangle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>










android - How to draw a Bitmap on a Canvas







MainActivity.java



package com.cfsuman.androidtutorials;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
private Resources mResources;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the Resources
mResources = getResources();

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ImageView imageView = findViewById(R.id.imageView);


// Set a click listener for Button widget
button.setOnClickListener(view -> {
// Bitmap to draw on the canvas
Bitmap bitmap = BitmapFactory.decodeResource(
mResources,
R.drawable.flower
);

// Define an offset value between canvas and bitmap
int offset = 100;

// 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);

//Finally, Draw the source bitmap on the canvas
canvas.drawBitmap(
bitmap, // Bitmap
offset, // Left
offset, // Top
null // Paint
);

// Display the newly created bitmap on app interface
imageView.setImageBitmap(dstBitmap);
});
}
}




activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Bitmap"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>








android - How to draw a Line on Canvas







MainActivity.java



package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ImageView imageView = findViewById(R.id.imageView);


// Set a click listener for Button widget
button.setOnClickListener(view -> {
// Initialize a new Bitmap object
Bitmap bitmap = Bitmap.createBitmap(
1200, // Width
600, // Height
Bitmap.Config.ARGB_8888 // Config
);

// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);

// Draw a solid color on the canvas as background
canvas.drawColor(Color.LTGRAY);

// Initialize a new Paint instance to draw the line
Paint paint = new Paint();
// Line color
paint.setColor(Color.parseColor("#676767"));
paint.setStyle(Paint.Style.STROKE);
// Line width in pixels
paint.setStrokeWidth(12);
paint.setAntiAlias(true);

// Set a pixels value to offset the line from canvas edge
int offset = 50;

// Draw a line on canvas at the center position
canvas.drawLine(
offset, // startX
(float) canvas.getHeight() / 2, // startY
canvas.getWidth() - offset, // stopX
(float) canvas.getHeight() / 2, // stopY
paint // Paint
);

// Display the newly created bitmap on app interface
imageView.setImageBitmap(bitmap);
});
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Line"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>









android - How to draw a Rectangle on a Canvas







MainActivity.java



package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ImageView imageView = findViewById(R.id.imageView);


// Set a click listener for Button widget
button.setOnClickListener(view -> {
// Initialize a new Bitmap object
Bitmap bitmap = Bitmap.createBitmap(
1200, // Width
600, // Height
Bitmap.Config.ARGB_8888 // Config
);

// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);

// Draw a solid color to the canvas background
canvas.drawColor(Color.LTGRAY);

// Initialize a new Paint instance to draw the Rectangle
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#676767"));
paint.setAntiAlias(true);

// Set a pixels value to padding around the rectangle
int padding = 50;

// Initialize a new Rect object
Rect rectangle = new Rect(
padding, // Left
padding, // Top
canvas.getWidth() - padding, // Right
canvas.getHeight() - padding // Bottom
);

// Finally, draw the rectangle on the canvas
canvas.drawRect(rectangle,paint);

// Display the newly created bitmap on app interface
imageView.setImageBitmap(bitmap);
});
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Rectangle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>








android - How to draw a circle on a Canvas







MainActivity.java



package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ImageView imageView = findViewById(R.id.imageView);


// Set a click listener for Button widget
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Initialize a new Bitmap object
Bitmap bitmap = Bitmap.createBitmap(
1200, // Width
600, // Height
Bitmap.Config.ARGB_8888 // Config
);

// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);

// Draw a solid color to the canvas background
canvas.drawColor(Color.LTGRAY);

// Initialize a new Paint instance to draw the Circle
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setAntiAlias(true);

// Calculate the available radius of canvas
int radius = Math.min(
canvas.getWidth(),canvas.getHeight()/2);

// Set a pixels value to padding around the circle
int padding = 5;

// Finally, draw the circle on the canvas
canvas.drawCircle(
(float)canvas.getWidth() / 2, // cx
(float)canvas.getHeight() / 2, // cy
radius - padding, // Radius
paint // Paint
);

// Display the newly created bitmap on app interface
imageView.setImageBitmap(bitmap);
}
});
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Circle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>











android - Draw border and shadow around a bitmap






activity_main.xml



<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:padding="24dp"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Border"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/iv"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_marginTop="24dp"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>




MainActivity.java



package com.cfsuman.androidtutorials;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
private ImageView mImageView;
private Context mContext;
private Resources mResources;
private Bitmap mBitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the application context
mContext = getApplicationContext();

// Get the Resources
mResources = getResources();

// Get the widgets reference from XML layout
Button btn = findViewById(R.id.button);
mImageView = findViewById(R.id.iv);

// Get the bitmap resource id
final int bitmapResourceID =R.drawable.flower;

// Set an image to ImageView
mImageView.setImageBitmap(
BitmapFactory.decodeResource(
mResources, bitmapResourceID
)
);

btn.setOnClickListener(v -> {
// Get the bitmap from drawable resources
mBitmap = BitmapFactory.decodeResource(
mResources, bitmapResourceID
);

// Add a border around the bitmap
mBitmap = addBorderToBitmap(
mBitmap, 50,
Color.parseColor("#F0F8FF")
);

// Add a border around the bitmap as shadow
mBitmap = addBorderToBitmap(
mBitmap, 10,
Color.LTGRAY
);

// Set the ImageView image as drawable object
mImageView.setImageBitmap(mBitmap);
});
}


// Custom method to add a border around bitmap
protected Bitmap addBorderToBitmap(
Bitmap srcBitmap, int borderWidth, int borderColor){

// Initialize a new Bitmap to make it bordered bitmap
Bitmap dstBitmap = Bitmap.createBitmap(
srcBitmap.getWidth() + borderWidth*2, // Width
srcBitmap.getHeight() + borderWidth*2, // Height
Bitmap.Config.ARGB_8888 // Config
);

// Initialize a new Canvas instance
Canvas canvas = new Canvas(dstBitmap);

// Initialize a new Paint instance to draw border
Paint paint = new Paint();
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderWidth);
paint.setAntiAlias(true);

Rect rect = new Rect(
borderWidth / 2,
borderWidth / 2,
canvas.getWidth() - borderWidth / 2,
canvas.getHeight() - borderWidth / 2
);

// Draw a rectangle as a border/shadow on canvas
canvas.drawRect(rect,paint);

// Draw source bitmap to canvas
canvas.drawBitmap(
srcBitmap, borderWidth, borderWidth, null
);

srcBitmap.recycle();

// Return the bordered bitmap
return dstBitmap;
}
}








android - How to create rounded corners ImageView






activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
tools:context=".MainActivity"
android:background="#F8F8FF">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rounded It"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn" />

</androidx.constraintlayout.widget.ConstraintLayout>






MainActivity.java



package com.cfsuman.androidtutorials;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;


public class MainActivity extends Activity {
private Context mContext;
private Resources mResources;
private Button mBTN;
private ImageView mImageView;
private Bitmap mBitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the application context
mContext = getApplicationContext();

// Get the Resources
mResources = getResources();

// Get the widgets reference from XML layout
mImageView = (ImageView) findViewById(R.id.iv);
mBTN = (Button) findViewById(R.id.btn);

// Get the bitmap from drawable resources
mBitmap = BitmapFactory.decodeResource(
mResources,
R.drawable.flower
);

// Display the bitmap in ImageView
mImageView.setImageBitmap(mBitmap);

// Set a click listener for Button widget
mBTN.setOnClickListener(view -> {
// Define the ImageView corners radius
float cornerRadius = 80.0f;

// Initialize a new RoundedBitmapDrawable
RoundedBitmapDrawable roundedBitmapDrawable
= RoundedBitmapDrawableFactory.create(
mResources,
mBitmap
);

// Set the RoundedBitmapDrawable corners radius
roundedBitmapDrawable.setCornerRadius(cornerRadius);

roundedBitmapDrawable.setAntiAlias(true);

// Set the ImageView image as drawable object
mImageView.setImageDrawable(roundedBitmapDrawable);
});
}
}