Android开发之圆角矩形创建工具RoundRect类定义与用法分析

所属分类: 软件编程 / Android 阅读数: 77
收藏 0 赞 0 分享

本文实例讲述了Android开发之圆角矩形创建工具RoundRect类。分享给大家供大家参考,具体如下:

用于把普通图片转换为圆角图像的工具类RoundRect类(复制即可使用):

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;
  /**
   * 用于初始化圆角矩形基本参数
   *
   * @param width    图片宽度
   * @param height    图片高度
   * @param cornerRadius 圆角半径
   */
  public RoundRect(int width, int height, float cornerRadius) {
    this.width = width;
    this.height = height;
    this.cornerRadius = cornerRadius;
  }
  /**
   * 用于把普通图片转换为圆角矩形图像
   *
   * @param path 图片路径
   * @return output 转换后的圆角矩形图像
   */
  Bitmap toRoundRect(String path) {
    //创建位图对象
    Bitmap photo = lessenUriImage(path);
    return Transformation(photo);
  }
  /**
   * 用于把普通图片转换为圆角矩形图像
   *
   * @param imageID 图片资源ID
   * @param context 上下文对象
   * @return output 转换后的圆角矩形图像
   */
  Bitmap toRoundRect(Context context, int imageID) {
    //创建位图对象
    Bitmap photo = BitmapFactory.decodeResource(context.getResources(), imageID);
    return Transformation(photo);
  }
  /**
   * 用于把Uri图片转换为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; //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
    int be = (int) (options.outHeight / (float) 320);
    if (be <= 0) be = 1;
    options.inSampleSize = be; //重新读入图片,注意此时已经把 options.inJustDecodeBounds 设回 false 了
    bitmap = BitmapFactory.decodeFile(path, options);
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    System.out.println(w + " " + h); //after zoom
    return bitmap;
  }
  /**
   * 用于把Bitmap图像转换为圆角图像
   *
   * @param photo 需要转换的Bitmap对象
   * @return 转换成圆角的Bitmap对象
   */
  private Bitmap Transformation(Bitmap photo) {
    //根据源文件新建一个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);
    // 产生一个红色的圆角矩形
    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;
  }
}

测试效果:

创建矩形图标:

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

创建圆形头像:

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相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多