Saturday, November 14, 2015

android - How to create rounded corners ImageView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    >
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rounded It"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Resources mResources;
    private RelativeLayout mRelativeLayout;
    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
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mImageView = (ImageView) findViewById(R.id.iv);
        mBTN = (Button) findViewById(R.id.btn);

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

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

        // Set a click listener for Button widget
        mBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Define the ImageView corners radius
                float cornerRadius = 50.0f;

                /*
                    RoundedBitmapDrawable
                        A Drawable that wraps a bitmap and can be drawn with rounded corners. You
                        can create a RoundedBitmapDrawable from a file path, an input stream, or
                        from a Bitmap object.
                */
                /*
                    RoundedBitmapDrawableFactory
                        Constructs RoundedBitmapDrawable objects, either from Bitmaps directly, or
                        from streams and files.

                    public static RoundedBitmapDrawable create (Resources res, Bitmap bitmap)
                        Returns a new drawable by creating it from a bitmap, setting initial target
                        density based on the display metrics of the resources.
                */
                // Initialize a new RoundedBitmapDrawable object to make ImageView rounded corners
                RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(
                        mResources,
                        mBitmap
                );

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

                /*
                    setAntiAlias(boolean aa)
                        Enables or disables anti-aliasing for this drawable.
                */
                roundedBitmapDrawable.setAntiAlias(true);

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