Android自己定义圆角ImageView
我们常常看到一些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的更多相关文章
- Android自己定义圆角ImageView 支持网络图片
先看下效果图 我们再来看一张CSDN的圆角图片 从布局能够看出csdn app 的头像也是圆角的Image,但能够看到.有明显的毛刺感.不知道是csdn 程序猿的疏忽还是 我手机的问题,本人手机(小米 ...
- Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角
转载请注明出处:王亟亟的大牛之路 平时要用一些非方方正正的button之类的小伙伴们是怎样实现的?RadioButton? ImageButton? 还是其它? 今天亟亟上的是ImageView来实现 ...
- Android 自己定义UI圆角button
Android实际开发中我们一般须要圆角的button,普通情况下我们能够让美工做出来对应的button图片.然后放上去就可以,另外我们能够在布局文件里直接设置,也能够达到一样的效果. 以下解说在布局 ...
- 自定义控件之 圆形 / 圆角 ImageView
一.问题在哪里? 问题来源于app开发中一个很常见的场景——用户头像要展示成圆的: 二.怎么搞? 机智的我,第一想法就是,切一张中间圆形透明.四周与底色相同.尺寸与头像相同的蒙板图片,盖在 ...
- Android UI--自定义ListView(实现下拉刷新+加载更多)
Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...
- Android自己定义控件实战——仿淘宝商品浏览界面
转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38656929 用手机淘宝浏览商品详情时,商品图片是放在后面的,在第一个Scr ...
- Android自己定义控件系列一:Android怎样实现老版优酷client三级环形菜单
转载请附上本文链接:http://blog.csdn.net/cyp331203/article/details/40423727 先来看看效果: 一眼看上去好像还挺炫的,感觉比較复杂...实际上并不 ...
- Android 自己定义ScrollView ListView 体验各种纵向滑动的需求
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38950509.本文出自[张鸿洋的博客] 1.概述 群里的一个哥们有个需求是这种: ...
- android 自己定义视频播放器之2/1
非常久没更新博客,相信大家年后都比較忙. 今天给大家带来了一款视频播放器,首先确认的得有几点. 1.首先得有个播放视频的view. 2.加点额外功能进去左边上下滑动调节亮度,右边上下滑动调节声量: 3 ...
随机推荐
- HDU 2476 区间DP String painter
题解 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...
- 【01】国内外git托管平台(总结by魔芋)
[01]国内git托管平台介绍 01, github:代码协作平台,协同开发. 代码托管平台. git:项目版本控制系统 02, 最好的托管方式: github 关闭或小众的托管方式: geakit( ...
- How To Configure VMware fencing using fence
本文主要简单介绍一下如何在RHEL 7 Pacemaker中配置一个fence_vmware_soap类型的STONITH设备(仅供测试学习). STONITH是Shoot-The-Other-Nod ...
- Debian 9 更新 sourrce.list(163源)
Debian 9 更新 sourrce.list(163源) 需求说明: 更新apt-get源 kyeup@kyeup-nas:~$ lsb_release -a No LSB modules are ...
- STM32F407 ADC 个人笔记
1. ADC概述(STM32F4xx系列) 3 个 ADC 可分别独立使用 也可使用双重/三重模式(提高采样率) 2 个通道组 规则通道:相当于正常运行的程序 注入通道:相当于中断(可以打断规则通道的 ...
- 大数据学习——azkaban工作流调度系统
azkaban的安装部署 在/root/apps 1目录下新建azkaban文件夹 上传安装包到azkaban 2解压 .tar.gz 3删掉安装包 [root@mini1 azkaban]# .ta ...
- 【Go】并发编程
Go语言宣扬用通讯的方式共享数据. Go语言以独特的并发编程模型傲视群雄,与并发编程关系最紧密的代码包就是sync包,意思是同步.同步的用途有两个,一个是避免多个线程在同一时刻操作同一个数据块,另一个 ...
- 【Luogu】P1312Mayan游戏(暴搜)
题目链接 由于是暴搜题,所以这篇博客只讲怎么优化剪枝,以及一些细节. 模拟消除思路:因为消除可以拆分成小的横条或竖条,而这些条的长度至少为三,所以一块可消除的区域至少会有一个中心点.这里的中心点可以不 ...
- Codevs 1043 ==洛谷 P1004 方格取数
题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...
- ftrace的使用
This article explains how to set up ftrace and be able to understand how to trace functions. It shou ...