Saturday, November 14, 2015

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