Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析
本文實例講述了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)建位圖對象 Bitmap photo = lessenUriImage(path); return Transformation(photo); } /** * 用于把普通圖片轉(zhuǎn)換為圓角矩形圖像 * * @param imageID 圖片資源ID * @param context 上下文對象 * @return output 轉(zhuǎn)換后的圓角矩形圖像 */ Bitmap toRoundRect(Context context, int imageID) { //創(chuàng)建位圖對象 Bitmap photo = BitmapFactory.decodeResource(context.getResources(), imageID); return Transformation(photo); } /** * 用于把Uri圖片轉(zhuǎn)換為Bitmap對象 * * @param path 圖片URI地址 * @return 生成的Bitmap對象 */ public final static Bitmap lessenUriImage(String path) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(path, options); //此時返回 bm 為空 options.inJustDecodeBounds = false; //縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進(jìn)行計算即可 int be = (int) (options.outHeight / (float) 320); if (be <= 0) be = 1; options.inSampleSize = be; //重新讀入圖片,注意此時已經(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對象 * @return 轉(zhuǎn)換成圓角的Bitmap對象 */ private Bitmap Transformation(Bitmap photo) { //根據(jù)源文件新建一個darwable對象 Drawable imageDrawable = new BitmapDrawable(photo); // 新建一個新的輸出圖片 Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); // 新建一個矩形 RectF outerRect = new RectF(0, 0, width, height); // 產(chǎn)生一個紅色的圓角矩形 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED); canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint); // 將源圖片繪制到這個圓角矩形上 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; } }
測試效果:
創(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資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android實現(xiàn)圓角矩形和圓形ImageView的方式
- android實現(xiàn)圓角矩形背景的方法
- Android自定義ViewGroup實現(xiàn)帶箭頭的圓角矩形菜單
- Android編程之canvas繪制各種圖形(點(diǎn),直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
- Android實現(xiàn)空心圓角矩形按鈕的實例代碼
- Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法
- Android編程實現(xiàn)帶漸變效果的圓角矩形示例
- Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法
- Android實現(xiàn)自定義ImageView的圓角矩形圖片效果
相關(guān)文章
Android開發(fā)中應(yīng)用程序分享功能實例
這篇文章主要介紹了Android開發(fā)中應(yīng)用程序分享功能,結(jié)合實例形式分析了基于Intent實現(xiàn)Android程序分享功能的技巧,需要的朋友可以參考下2016-02-02Android使用 PopupWindow 實現(xiàn)底部彈窗功能
這篇文章主要介紹了Android使用 PopupWindow 實現(xiàn)底部彈窗功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12