Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析
本文實(shí)例講述了Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類。分享給大家供大家參考,具體如下:
用于把普通圖片轉(zhuǎn)換為圓角圖像的工具類RoundRect類(復(fù)制即可使用):
RoundRect.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
public class RoundRect {
private int width;
private int height;
private float cornerRadius;
/**
* 用于初始化圓角矩形基本參數(shù)
*
* @param width 圖片寬度
* @param height 圖片高度
* @param cornerRadius 圓角半徑
*/
public RoundRect(int width, int height, float cornerRadius) {
this.width = width;
this.height = height;
this.cornerRadius = cornerRadius;
}
/**
* 用于把普通圖片轉(zhuǎn)換為圓角矩形圖像
*
* @param path 圖片路徑
* @return output 轉(zhuǎn)換后的圓角矩形圖像
*/
Bitmap toRoundRect(String path) {
//創(chuàng)建位圖對(duì)象
Bitmap photo = lessenUriImage(path);
return Transformation(photo);
}
/**
* 用于把普通圖片轉(zhuǎn)換為圓角矩形圖像
*
* @param imageID 圖片資源ID
* @param context 上下文對(duì)象
* @return output 轉(zhuǎn)換后的圓角矩形圖像
*/
Bitmap toRoundRect(Context context, int imageID) {
//創(chuàng)建位圖對(duì)象
Bitmap photo = BitmapFactory.decodeResource(context.getResources(), imageID);
return Transformation(photo);
}
/**
* 用于把Uri圖片轉(zhuǎn)換為Bitmap對(duì)象
*
* @param path 圖片URI地址
* @return 生成的Bitmap對(duì)象
*/
public final static Bitmap lessenUriImage(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, options); //此時(shí)返回 bm 為空
options.inJustDecodeBounds = false; //縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
int be = (int) (options.outHeight / (float) 320);
if (be <= 0) be = 1;
options.inSampleSize = be; //重新讀入圖片,注意此時(shí)已經(jīng)把 options.inJustDecodeBounds 設(shè)回 false 了
bitmap = BitmapFactory.decodeFile(path, options);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
System.out.println(w + " " + h); //after zoom
return bitmap;
}
/**
* 用于把Bitmap圖像轉(zhuǎn)換為圓角圖像
*
* @param photo 需要轉(zhuǎn)換的Bitmap對(duì)象
* @return 轉(zhuǎn)換成圓角的Bitmap對(duì)象
*/
private Bitmap Transformation(Bitmap photo) {
//根據(jù)源文件新建一個(gè)darwable對(duì)象
Drawable imageDrawable = new BitmapDrawable(photo);
// 新建一個(gè)新的輸出圖片
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
// 新建一個(gè)矩形
RectF outerRect = new RectF(0, 0, width, height);
// 產(chǎn)生一個(gè)紅色的圓角矩形
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint);
// 將源圖片繪制到這個(gè)圓角矩形上
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
imageDrawable.setBounds(0, 0, width, height);
canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
imageDrawable.draw(canvas);
canvas.restore();
return output;
}
}
測(cè)試效果:
創(chuàng)建矩形圖標(biāo):
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
ImageView image = (ImageView)findViewById(R.id.image);
RoundRect roundRect = new RoundRect(500,500,100);
Bitmap photo = roundRect.toRoundRect(this,R.drawable.kms);
image.setImageBitmap(photo);
}
}

創(chuàng)建圓形頭像:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
ImageView image = (ImageView)findViewById(R.id.image);
RoundRect roundRect = new RoundRect(500,500,300);
Bitmap photo = roundRect.toRoundRect(this,R.drawable.indark);
image.setImageBitmap(photo);
}
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)圓角矩形和圓形ImageView的方式
- android實(shí)現(xiàn)圓角矩形背景的方法
- Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
- Android編程之canvas繪制各種圖形(點(diǎn),直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
- Android實(shí)現(xiàn)空心圓角矩形按鈕的實(shí)例代碼
- Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法
- Android編程實(shí)現(xiàn)帶漸變效果的圓角矩形示例
- Android開發(fā)基于Drawable實(shí)現(xiàn)圓角矩形的方法
- Android實(shí)現(xiàn)自定義ImageView的圓角矩形圖片效果
相關(guān)文章
android判斷相機(jī)圖片朝向的簡(jiǎn)單方法
下面小編就為大家?guī)硪黄猘ndroid判斷相機(jī)圖片朝向的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Android如何實(shí)現(xiàn)鎖屏狀態(tài)下彈窗
在鎖屏狀態(tài)下彈窗的效果我們平時(shí)并不少見,如QQ、微信和鬧鐘等,但是Android開發(fā)者要怎么實(shí)現(xiàn)這一功能呢?下面一起來看看。2016-08-08
Android實(shí)現(xiàn)三角形氣泡效果方式匯總
這篇文章主要介紹了Android實(shí)現(xiàn)三角形氣泡效果方式匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Android開發(fā)中應(yīng)用程序分享功能實(shí)例
這篇文章主要介紹了Android開發(fā)中應(yīng)用程序分享功能,結(jié)合實(shí)例形式分析了基于Intent實(shí)現(xiàn)Android程序分享功能的技巧,需要的朋友可以參考下2016-02-02
Android使用 PopupWindow 實(shí)現(xiàn)底部彈窗功能
這篇文章主要介紹了Android使用 PopupWindow 實(shí)現(xiàn)底部彈窗功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

