※效果



※代码

/**
* 转换图片成圆形
*
* @param bitmap
* 传入Bitmap对象
* @return
*/
public Bitmap toRoundBitmap(Bitmap bitmap) {
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;
} /**
* 圆角图片
* @param bitmap
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output); final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12; paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint); return output; } /***
* 设置图片倒影
* @param originalBitmap
* @return
*/
private Bitmap createReflectedImage(Bitmap originalBitmap) {
// 图片与倒影间隔距离
final int reflectionGap = 4; // 图片的宽度
int width = originalBitmap.getWidth();
// 图片的高度
int height = originalBitmap.getHeight(); Matrix matrix = new Matrix();
// 图片缩放,x轴变为原来的1倍,y轴为-1倍,实现图片的反转
matrix.preScale(1, -1);
// 创建反转后的图片Bitmap对象,图片高是原图的一半。
Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0,
height / 2, width, height / 2, matrix, false);
// 创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。
Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height
+ height / 2 + reflectionGap), Config.ARGB_8888); // 构造函数传入Bitmap对象,为了在图片上绘图
Canvas canvas = new Canvas(withReflectionBitmap);
// 画原始图片
canvas.drawBitmap(originalBitmap, 0, 0, null); // 画间隔矩形
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); // 画倒影图片
canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null); // 实现倒影效果
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, originalBitmap.getHeight(),
0, withReflectionBitmap.getHeight(), 0x70ffffff, 0x00ffffff,
TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 覆盖效果
canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint); return withReflectionBitmap;
}

※Demo下载

圆角和圆形ImageView的更多相关文章

  1. Android 自定义View修炼-实现自定义圆形、圆角和椭圆ImageView(使用Xfermode图形渲染方法)

    一:简介: 在上一篇<Android实现圆形.圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)>博文中,采用BitmapShader方法实现自定义的圆形.圆角等自定 ...

  2. Android CircleImageView圆形ImageView

     Android CircleImageView圆形ImageView CircleImageView是github上一个第三方开源的实现圆形ImageView的项目.其在github上的项目主页 ...

  3. Glide加载图片到自定义的圆形ImageView中不显示

    当使用自定义的圆形ImageView时,发现使用Glide加载并设置默认初始图片时,自定义的ImageView一直显示默认图片,无法更新到加载的图片. 使用下面代码可以解决这个问题 Glide.wit ...

  4. 自定义圆形imageview

    import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader ...

  5. Android 完美实现图片圆角和圆形(对实现进行分析)

    本来想在网上找个圆角的例子看一看,不尽人意啊,基本都是官方的Demo的那张原理图,稍后会贴出.于是自己自定义了个View,实现图片的圆角以及圆形效果.效果图: 第一个是原图,第二个是圆形效果,第三第四 ...

  6. Android学习笔记-绘制圆形ImageView实例

    现在很多的APP都很喜欢圆形的头像,这里就简单的写个圆形的ImageView~ 第三方圆形ImageView控件: RoundedImageView CircleImageView 实现代码: 自定义 ...

  7. [转]android 自定义圆形imageview控件

      android布局 首先,定义定义圆形Imageview类: import android.content.Context; import android.graphics.Bitmap; imp ...

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

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

  9. shape 填充 圆角矩形 圆形 环形

    属性 使用中可能出现的问题: 如果在某些手机中使用 shape 出现黑色填充背景,设置<solid android:color="@color/transparent"/&g ...

随机推荐

  1. 如何设置gen_server在退出时执行相关操作

    如果gen_server在监控树中不需要stop函数,gen_server会由其supervisor根据shutdown策略自动终止掉.如果要在进程终止之前执行清理,shutdown策略必须设定一个t ...

  2. CentOS6.5 下在Nginx中添加SSL证书以支持HTTPS协议访问

    参考文献: 1. NginxV1.8.0安装与配置 2. CentOS下在Nginx中添加SSL证书以支持HTTPS协议访问 3. nginx配置ssl证书的方法 4.nginx强制使用https访问 ...

  3. Win7 x64安装Paramiko出问题

    今天上午windows下配置paramiko环境时出现问题,随手记录下来.   先说一下我的环境: win7 x64 旗舰版.Python3.5.0.pip8.1.0 pip install para ...

  4. iOS开发之计算动态cell的高度并缓存

    项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...

  5. swift-数组array

    // Playground - noun: a place where people can play import UIKit //--------------------------------- ...

  6. HFS - 简单的将个人电脑变服务器!

    网络硬盘   HTTP File Server(HFS)是我目前所知道的最简便的P2P文件分享方式,只一个大小为559KB的单文件绿色软件(hfs.exe)就可以在瞬间不经过任何系统设置将一台普通的联 ...

  7. Java--CyclicBarrier使用简介

    CyclicBarrier介绍 (一)一 个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时 ...

  8. linux下的软件包安装

    linux下安装软件包有两种方法:源文件编译安装(source)和 rpm 安装. 1.源文件包安装的通用方法. 一般安装源代码的程序你得要看它的README,一般在它的目录下都有的. 01.配置: ...

  9. BNU Box of Bricks

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1596 这个题一开始以为要求最少移动次数,把我吓到了,原来只要求最少移动几个方块就行了..这一下就变简 ...

  10. docker 学习手冊-中文版下载

    这个PDF算是学习docker的一个小总结,全部文章摘自我在csdn的博客专栏: http://blog.csdn.net/column/details/docker.html 第一章到第八章摘自do ...