这个类可以实现圆角,或者是圆形图片的操作。

CircleImageManager.java
package com.kale.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; public class CircleImageManager { /**
* @param context
* 对象
* @param resId
* 图片资源的id
* @return 一个圆形的Drawable对象
*/
public static Drawable getCircleDrawable(Context context, int resId) {
Bitmap bitmap = getCircleBitmap(context, resId);
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
return drawable;
} /**
* @param context
* @param resId
* @return 转换好的bitmap对象
*/
public static Bitmap getCircleBitmap(Context context, int resId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
resId);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
left = 0;
top = 0;
right = width;
bottom = width;
height = width; dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2; left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height; dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
} Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output); final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst); paint.setAntiAlias(true);// 设置画笔无锯齿 canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas // 以下有两种方法画圆,drawRounRect和drawCircle
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
// canvas.drawCircle(roundPx, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle return output;
} /**
* 圆角的处理
* 参考:http://www.open-open.com/lib/view/open1331042856468.html
* 穿入bitmap和圆角的角度(如:50)
* @param bitmap
*/
public static Bitmap getRoundCornerBitmap(Bitmap bitmap ,int roundPixels) {
// 创建一个和原始图片一样大小位图
Bitmap roundConcerImage = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
// 创建带有位图roundConcerImage的画布
Canvas canvas = new Canvas(roundConcerImage);
// 创建画笔
Paint paint = new Paint();
// 创建一个和原始图片一样大小的矩形
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectF = new RectF(rect);
// 去锯齿
paint.setAntiAlias(true);
// 画一个和原始图片一样大小的圆角矩形
canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint);
// 设置相交模式
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
// 把图片画到矩形去
canvas.drawBitmap(bitmap, null, rect, paint);
return roundConcerImage; } /**
* @param context
* @param resId
* @param roundPixels
* @return 圆角的Drawable对象
*/
public static Drawable getRoundCornerDrawable(Context context, int resId ,int roundPixels) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
bitmap = getRoundCornerBitmap(bitmap, roundPixels);
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
return drawable; }
}

CircleImageManager——圆形 / 圆角图片的工具类的更多相关文章

  1. 安卓图片载入之使用universalimageloader载入圆形圆角图片

    前言 话说这universalimageloader载入图片对搞过2年安卓程序都是用烂了再熟悉只是了.就是安卓新手也是百度就会有一大堆东西出来,今天为什么这里还要讲使用universalimagelo ...

  2. 图片处理工具类 - ImageUtils.java

    纯JAVA实现的图片处理工具类,提供图片的裁剪.压缩.获取尺寸.制作圆角等方法. 源码如下:(点击下载 -ImageUtils.java .FolderUtils.java .commons-io-2 ...

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

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

  4. Android 圆形/圆角图片的方法

    Android 圆形/圆角图片的方法 眼下网上有非常多圆角图片的实例,Github上也有一些成熟的项目.之前做项目,为了稳定高效都是选用Github上的项目直接用.但这样的结束也是Android开发必 ...

  5. Java操作图片的工具类

    操作图片的工具类: import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.a ...

  6. java图片处理工具类

    直接上代码: package com.zxd.tool; /** * Created by zhang on 14-3-1. * 图片的常用操作类 */ import java.awt.AlphaCo ...

  7. Java的图片处理工具类

    import Java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  8. 【转】java缩放图片、java裁剪图片代码工具类

    一首先看下效果 二工具类 三测试类 在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统 ...

  9. JavaUtil_03_图片处理工具类

    一.源码 功能:缩放图像.切割图像.图像类型转换.彩色转黑白.文字水印.图片水印等 package com.ray.dingtalk.util; import java.awt.AlphaCompos ...

随机推荐

  1. Mysql报Cannot load from mysql.proc. The table is probably corrupted

    1548-Cannot load from mysql.proc. The table is probably corrupted http://bugs.mysql.com/bug.php?id=5 ...

  2. springcloud hystrix 部分参数整理

    hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command Properties Executio ...

  3. 017 jquery中对样式的操作

    1.样式操作 2.css-dom操作 3.程序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  4. matplotlib显示中文异常处理

    matplotlib显示中文 [做个记录,方便以后使用] [一般导入方式] import matplotlib.pyplot as plt [效果图] [方式一]FontProperties impo ...

  5. Android 常见SD卡操作

    目录 Android 常见SD卡操作 Android 常见SD卡操作 参考 https://blog.csdn.net/mad1989/article/details/37568667. [0.] E ...

  6. PHP菜刀工具WebHandler

    PHP菜刀工具WebHandler   在Web渗透测试中,后台代码如果包含系统命令执行功能,并以用户提交的数据作为参数,就带来潜在的安全隐患.Kali Linux提供一款PHP菜单工具WebHand ...

  7. BZOJ.1901.Dynamic Rankings(整体二分)

    题目链接 BZOJ 洛谷 (以下是口胡) 对于多组的询问.修改,我们可以发现: 假设有对p1,p2,p3...的询问,在这之前有对p0的修改(比如+1),且p0<=p1,p2,p3...,那么我 ...

  8. Node.js用6行代码1个JS文件搭建一个HTTP静态服务器

    Node.js是一个基于Chrome的JavaScript运行时的用户以轻松构建快速.可扩展的网络应用平台. Node.js使用事件驱动.非阻塞I/ O模型,使它轻量级.高效和完美的适用于运行在分布式 ...

  9. python使用递归实现一个分形图形

    代码如下: import turtle def main(): t = turtle.Turtle() t.hideturtle() t.speed(10) level = 12 fract(t,-8 ...

  10. resteasy经验谈

    resteasy 是java体系中比较成熟的rest框架,也是jax-rs规范的实现之一,dubbox的REST服务框架,就是采用的resteasy实现,近日在实际项目中遇到了几个问题,记录于此:   ...