我们常常看到一些app中能够显示圆角图片。比方qq的联系人图标等等,实现圆角图片一种办法是直接使用圆角图片资源,当然假设没有圆角图片资源。我们也能够自己通过程序实现的,以下介绍一个自己定义圆角ImageView的方法:

package com.yulongfei.imageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView; public class RoundAngleImageView extends ImageView {
private int roundWidth = 13;
private int roundHeight = 13; public RoundAngleImageView(Context context) {
super(context);
init(context, null);
} public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
} public RoundAngleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
} private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.RoundAngleImageView);
roundWidth = a.getDimensionPixelSize(
R.styleable.RoundAngleImageView_roundWidth, roundWidth);
roundHeight = a.getDimensionPixelSize(
R.styleable.RoundAngleImageView_roundHeight, roundHeight);
a.recycle();
} else {
float density = context.getResources().getDisplayMetrics().density;
roundWidth = (int)(roundWidth * density);
roundHeight = (int)(roundHeight * density);
}
} /** 重写draw() */
@Override
public void draw(Canvas canvas) { //实例化一个和ImageView一样大小的bitmap
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Config.ARGB_8888); //实例化一个canvas,这个canvas相应的内存为上面的bitmap
Canvas canvas2 = new Canvas(bitmap);
if (bitmap.isRecycled()) {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Config.ARGB_8888);
canvas2 = new Canvas(bitmap);
} //将imageView自己绘制到canvas2上,这个导致bitmap里面存放了imageView
super.draw(canvas2); //利用canvas画一个圆角矩形,这个会改动bitmap的数据
drawRoundAngle(canvas2); //将裁剪好的bitmap绘制到系统当前canvas上,这样裁剪好的imageview就能显示到屏幕上
Paint paint = new Paint();
paint.setXfermode(null);
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
} public void setRoundWidth(int roundWidth, int roundHeight) {
this.roundWidth = roundWidth;
this.roundHeight = roundHeight;
} private void drawRoundAngle(Canvas canvas)
{
Paint maskPaint = new Paint();
maskPaint.setAntiAlias(true);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
Path maskPath = new Path();
maskPath.addRoundRect(new RectF(0.0F, 0.0F, getWidth(), getHeight()), roundWidth, roundHeight, Path.Direction.CW); //这是设置了填充模式。很关键
maskPath.setFillType(Path.FillType.INVERSE_WINDING);
canvas.drawPath(maskPath, maskPaint);
}
}

Android自己定义圆角ImageView的更多相关文章

  1. Android自己定义圆角ImageView 支持网络图片

    先看下效果图 我们再来看一张CSDN的圆角图片 从布局能够看出csdn app 的头像也是圆角的Image,但能够看到.有明显的毛刺感.不知道是csdn 程序猿的疏忽还是 我手机的问题,本人手机(小米 ...

  2. Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角

    转载请注明出处:王亟亟的大牛之路 平时要用一些非方方正正的button之类的小伙伴们是怎样实现的?RadioButton? ImageButton? 还是其它? 今天亟亟上的是ImageView来实现 ...

  3. Android 自己定义UI圆角button

    Android实际开发中我们一般须要圆角的button,普通情况下我们能够让美工做出来对应的button图片.然后放上去就可以,另外我们能够在布局文件里直接设置,也能够达到一样的效果. 以下解说在布局 ...

  4. 自定义控件之 圆形 / 圆角 ImageView

    一.问题在哪里? 问题来源于app开发中一个很常见的场景——用户头像要展示成圆的:       二.怎么搞? 机智的我,第一想法就是,切一张中间圆形透明.四周与底色相同.尺寸与头像相同的蒙板图片,盖在 ...

  5. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  6. Android自己定义控件实战——仿淘宝商品浏览界面

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38656929 用手机淘宝浏览商品详情时,商品图片是放在后面的,在第一个Scr ...

  7. Android自己定义控件系列一:Android怎样实现老版优酷client三级环形菜单

    转载请附上本文链接:http://blog.csdn.net/cyp331203/article/details/40423727 先来看看效果: 一眼看上去好像还挺炫的,感觉比較复杂...实际上并不 ...

  8. Android 自己定义ScrollView ListView 体验各种纵向滑动的需求

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38950509.本文出自[张鸿洋的博客] 1.概述 群里的一个哥们有个需求是这种: ...

  9. android 自己定义视频播放器之2/1

    非常久没更新博客,相信大家年后都比較忙. 今天给大家带来了一款视频播放器,首先确认的得有几点. 1.首先得有个播放视频的view. 2.加点额外功能进去左边上下滑动调节亮度,右边上下滑动调节声量: 3 ...

随机推荐

  1. linux 搭建apache 服务器

    1.查看apache服务器 /etc/init.d/httpd status 若没有,则使用yum  -y install httpd  安装软件 2.设置开机启动 chkconfig httpd o ...

  2. find_element——By 元素定位

    • find_element(By.ID,”loginName”)• find_element(By.NAME,”SubjectName”)• find_element(By.CLASS_NAME,” ...

  3. Access denied for user ''@'localhost' to database 'mysql'

    ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'   在centos下安装好了mysql,用r ...

  4. 【Luogu】P2051中国象棋(DP)

    题目链接 去看STDCALL的题解吧 #include<cstdio> #include<cctype> #define mod 9999973 inline long lon ...

  5. [USACO5.3]Big Barn (动态规划)

    题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N x N 的方格.输入数据中包括有树的 ...

  6. JS return false 与 return true

    在大多数情况下,为事件处理函数返回false,可以防止默认的事件行为.例如,默认情况下点击一个<a>元素,页面会跳转到该元素href属性指定的页. Return False 就相当于终止符 ...

  7. 洛谷 [P3150] pb的游戏

    博弈论基础 本题可以视作P2148 E&D 的前置技能 本题直接判断奇偶性来求解, 证明就是2148 的证明 不贴代码

  8. Tengine的concat模块与js、css合并

    首先,先走出一个误区 ,下面是tengine-cn邮件列表里的一篇邮件原文:“看了这个例子就了解了,这个所谓的合并请求只是把所有的CSS或JAVASCRIPT请求合并,必须是同一个文件类型的.我开始想 ...

  9. shell的select脚本的简单入门

    shell的select脚本的简单入门 语法:select var in ...;do break;doneecho $var 示例: #/bin/bash echo "what is yo ...

  10. MVC模式(三层架构模式)

    (Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller). MVC模式最早由Try ...