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

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

RoundRect.class

测试效果:

创建矩形图标:

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圆角矩形创建工具RoundRect类的更多相关文章

  1. Android 自定义的圆角矩形ImageView 工具类

    上图看效果 自定义圆角矩形ImageView工具类 package com.wechaotou.utils; import android.content.Context; import androi ...

  2. Android圆角矩形

    1.在drawable中创建shape_round文件 <?xml version="1.0" encoding="utf-8"?> <sha ...

  3. Android圆角矩形的实现

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android=&quo ...

  4. Android中实现圆角矩形及半透明效果。

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在做Android开发时,我们为了美观,有时候需要使用圆角矩形,或半透明之类的效果,在网页设计中很容易实现.但在Android开发中 ...

  5. [BOT] 一种android中实现“圆角矩形”的方法

    内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...

  6. Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形) .

    1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, ...

  7. CircleImageManager——圆形 / 圆角图片的工具类

    这个类可以实现圆角,或者是圆形图片的操作. CircleImageManager.java package com.kale.utils; import android.content.Context ...

  8. Android开发之自定义圆角矩形图片ImageView的实现

    android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...

  9. Android开发之自定义圆角矩形图片ImageView的实现 - Jamy Cai

    android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...

随机推荐

  1. git 推送

    echo "# shops" >> README.md git init git add README.md git commit -m "first com ...

  2. extjs 兼容性问题解决方案

    首先明确一点,extjs是没有所谓的兼容性的问题的.那为什么总是出现不同浏览器兼容性的问题呢?而且很多人把这作为了extjs一个缺点. 解决方法,看看写的代码是不是多了  英文逗号 , 或 中文的逗号 ...

  3. openstack虚拟机启动过程

    核心项目3个 1.控制台 服务名:Dashboard 项目名:Horizon 功能:web方式管理云平台,建云主机,分配网络,配安全组,加云盘 2.计算 服务名:计算 项目名:Nova 功能:负责响应 ...

  4. ecshop 订单-》订单状态

    /** * 取得状态列表 * @param string $type 类型:all | order | shipping | payment */ function get_status_list($ ...

  5. Docker Compose to CoreOS

    taken from https://docs.docker.com/compose/install/ the only thing is that /usr is read only, but /o ...

  6. SQL Server2008窗口计算

    (一) 窗口的定义:指为用户指定的一组行,也称着"分区".如下图所示的窗口分区.每一个班级看作是一个数据窗口,一共有三个窗口 (二)窗口计算的相关方法 1)over()用法  格式 ...

  7. iis7+ 禁止IP访问设置方法

    第一步:打开 管理工具-Internet 信息服务(IIS)管理器,打开网站,选中某个站点 第二步:双击IIS中的IP地址和域限制 第三步:在右栏操作,添加拒绝条目

  8. linux安装-版本选择-终极决定

    选用64位或32位的版本,注意看硬件: 内存大于4G的用64位, 小于4G的用32位 同时, 64位的版本在软件源, 软件的兼容性等问题. ----------------------------- ...

  9. PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)

    原文: PHP5: mysqli 插入, 查询, 更新和删除  Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...

  10. django 初级(一) 配置与周边

    一.下载安装 从 https://www.djangoproject.com/download/ 下载最新的 django 版本,写本文时最新版本为 django 1.7,官方说只需要 python6 ...