国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析

2019-10-22 18:17:02
字體:
供稿:網(wǎng)友

本文實例講述了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);  }}

Android,圓角矩形,創(chuàng)建工具,RoundRect類,Android開發(fā)

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

Android,圓角矩形,創(chuàng)建工具,RoundRect類,Android開發(fā)

希望本文所述對大家Android程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 温州市| 图们市| 南和县| 舒兰市| 墨江| 大连市| 南川市| 四子王旗| 龙南县| 荥经县| 贺州市| 桑植县| 东源县| 昆明市| 荆州市| 曲松县| 正阳县| 玉树县| 合山市| 永和县| 营山县| 化隆| 三门峡市| 祥云县| 伊吾县| 扎鲁特旗| 中牟县| 张家界市| 札达县| 东方市| 禹城市| 杭州市| 黄骅市| 姚安县| 武义县| 滕州市| 遵义市| 六枝特区| 邯郸市| 阿拉善左旗| 西乡县|