Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;
import android.graphics.Bitmap;

import android.graphics.BitmapShader;
import android.graphics.Canvas;

import android.graphics.Paint;
import android.graphics.RectF;

import android.util.TypedValue;

public class Main {
    public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int radiusDP) {
        Bitmap bmp;

        bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

        float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radiusDP,
                context.getResources().getDisplayMetrics());
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(shader);

        RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawRoundRect(rect, radius, radius, paint);

        return bmp;
    }

    public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int leftTop, int rightTop,
            int leftBottm, int rightBottom) {
        Bitmap bmp;

        bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

        float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftTop,
                context.getResources().getDisplayMetrics());
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(shader);

        RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawRoundRect(rect, radius, radius, paint);
        return bmp;
    }
}