Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
Android中的ImageView只能显示矩形的图片,为了用户体验更多,Android实现圆角矩形,圆形或者椭圆等图形,一般通过自定义ImageView来实现,首先获取到图片的Bitmap,然后通过Paint和onDraw()进行圆形图片显示。
效果图:
代码:
- /**
- * 实现圆形、圆角,椭圆等自定义图片View。
- * @author zq
- *
- */
- public class ZQImageViewRoundOval extends ImageView {
- private Paint mPaint;
- private int mWidth;
- private int mHeight;
- private int mRadius;//圆半径
- private RectF mRect;//矩形凹行大小
- private int mRoundRadius;//圆角大小
- private BitmapShader mBitmapShader;//图形渲染
- private Matrix mMatrix;
- private int mType;//记录是圆形还是圆角矩形
- public static final int TYPE_CIRCLE = 0;//圆形
- public static final int TYPE_ROUND = 1;//圆角矩形
- public static final int TYPE_OVAL = 2;//椭圆形
- public static final int DEFAUT_ROUND_RADIUS = 10;//默认圆角大小
- public ZQImageViewRoundOval(Context context) {
- this(context, null);
- // TODOAuto-generated constructor stub
- }
- public ZQImageViewRoundOval(Context context, AttributeSet attrs) {
- this(context,attrs, 0);
- // TODOAuto-generated constructor stub
- }
- public ZQImageViewRoundOval(Context context, AttributeSet attrs,int defStyle){
- super(context,attrs, defStyle);
- initView();
- }
- private void initView() {
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mMatrix = new Matrix();
- mRoundRadius = DEFAUT_ROUND_RADIUS;
- }
- @Override
- protected void onMeasure(int widthMeasureSpec,intheightMeasureSpec) {
- // TODOAuto-generated method stub
- super.onMeasure(widthMeasureSpec,heightMeasureSpec);
- // 如果是绘制圆形,则强制宽高大小一致
- if (mType ==TYPE_CIRCLE) {
- mWidth = Math.min(getMeasuredWidth(),getMeasuredHeight());
- mRadius = mWidth / 2;
- setMeasuredDimension(mWidth, mWidth);
- }
- }
- @Override
- protected void onDraw(Canvas canvas) {
- if (null ==getDrawable()) {
- return;
- }
- setBitmapShader();
- if (mType ==TYPE_CIRCLE) {
- canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
- } else if (mType == TYPE_ROUND) {
- mPaint.setColor(Color.RED);
- canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint);
- }else if(mType == TYPE_OVAL){
- canvas.drawOval(mRect, mPaint);
- }
- }
- @Override
- protected void onSizeChanged(int w,int h, int oldw,int oldh) {
- // TODOAuto-generated method stub
- super.onSizeChanged(w,h, oldw, oldh);
- mRect = new RectF(0,0, getWidth(), getHeight());
- }
- /**
- * 设置BitmapShader
- */
- private void setBitmapShader() {
- Drawable drawable = getDrawable();
- if (null ==drawable) {
- return;
- }
- Bitmap bitmap = drawableToBitmap(drawable);
- // 将bitmap作为着色器来创建一个BitmapShader
- mBitmapShader = newBitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
- float scale =1.0f;
- if (mType ==TYPE_CIRCLE) {
- // 拿到bitmap宽或高的小值
- int bSize =Math.min(bitmap.getWidth(), bitmap.getHeight());
- scale = mWidth * 1.0f /bSize;
- } else if (mType == TYPE_ROUND ||mType == TYPE_OVAL) {
- // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
- scale = Math.max(getWidth() * 1.0f/ bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
- }
- // shader的变换矩阵,我们这里主要用于放大或者缩小
- mMatrix.setScale(scale,scale);
- // 设置变换矩阵
- mBitmapShader.setLocalMatrix(mMatrix);
- mPaint.setShader(mBitmapShader);
- }
- /**
- * drawable转bitmap
- *
- * @paramdrawable
- * @return
- */
- private Bitmap drawableToBitmap(Drawable drawable) {
- if (drawableinstanceof BitmapDrawable) {
- BitmapDrawable bitmapDrawable =(BitmapDrawable) drawable;
- returnbitmapDrawable.getBitmap();
- }
- int w =drawable.getIntrinsicWidth();
- int h =drawable.getIntrinsicHeight();
- Bitmap bitmap = Bitmap.createBitmap(w,h, Config.ARGB_8888);
- Canvas canvas = newCanvas(bitmap);
- drawable.setBounds(0, 0, w, h);
- drawable.draw(canvas);
- return bitmap;
- }
- /**
- * 单位dp转单位px
- */
- public int dpTodx(int dp){
- return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
- dp,getResources().getDisplayMetrics());
- }
- public int getType(){
- return mType;
- }
- /**
- * 设置图片类型:圆形、圆角矩形、椭圆形
- * @param mType
- */
- public void setType(int mType) {
- if(this.mType !=mType){
- this.mType = mType;
- invalidate();
- }
- }
- public int getRoundRadius() {
- return mRoundRadius;
- }
- /**
- * 设置圆角大小
- * @parammRoundRadius
- */
- public void setRoundRadius(int mRoundRadius) {
- if(this.mRoundRadius !=mRoundRadius){
- this.mRoundRadius =mRoundRadius;
- invalidate();
- }
- }
- }
- MainActivity类
- /****
- *
- * 自定义ImageView实现圆形图片,圆角矩形图片 椭圆图片
- *
- * @authorzq
- *
- */
- public class MainActivity extends Activity {
- private ZQImageViewRoundOvaliv_circle;//圆形图片
- private ZQImageViewRoundOvaliv_roundRect;//圆角矩形图片
- private ZQImageViewRoundOvaliv_oval;//椭圆图片
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_image);
- initViews();
- }
- /**
- * 初始化Views
- */
- private void initViews(){
- iv_circle =(ZQImageViewRoundOval)findViewById(R.id.cicle);
- iv_roundRect =(ZQImageViewRoundOval)findViewById(R.id.roundRect);
- iv_oval =(ZQImageViewRoundOval)findViewById(R.id.oval);
- iv_roundRect.setType(ZQImageViewRoundOval.TYPE_ROUND);
- iv_roundRect.setRoundRadius(6);//矩形凹行大小
- iv_oval.setType(ZQImageViewRoundOval.TYPE_OVAL);
- iv_oval.setRoundRadius(45);//圆角大小
- }
- }
源码下载:
Eclipse下载:http://download.csdn.net/detail/dickyqie/9621330
AndroidStudio下载:https://github.com/DickyQie/android-imageview
Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示的更多相关文章
- Android之拨号界面图片风格,无信息默认显示界面修改
Android之拨号界面图片风格,无信息默认显示界面修改 点开Dialer app,出现拨号,联系人,收藏三个选项卡,也就是三个Fragment,在三个界面都没有信息的时候会显示一个时钟,联系人,收藏 ...
- Android自定义ImageView圆形头像
效果图: 代码如下: RoundImageView.java import cn.comnav.evaluationsystem.R; import android.content.Context; ...
- Android -- 自定义ImageView(圆形头像)
1. 原图
- 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
首先呢,还是一贯作风,我们先来看看众多应用中的示例:(这种效果是很常见的,可以说应用的必须品.) 搜狐客户端 ...
- Android 自定义ImageView支持缩放,拖拽,方便复用
今天刚发了一篇关于ImageView的缩放和拖拽的博客,然后我想了下,将他自定义下,方便我们来复用这个imageView,效果我就不多说了,http://blog.csdn.net/xiaanming ...
- android中对Bitmap图片设置任意角为圆角
http://blog.csdn.net/l448288137/article/details/48276681 最近项目开发中使用到了圆角图片,网上找到的圆角图片控件大多比较死板,只可以全圆角.其中 ...
- Android 自定义Gallery浏览图片
之前写的<Android ImageSwitcher和Gallery的使用>一文中提到我在教室一下午为实现那个效果找各种资料.期间在网上找了一个个人觉得比较不错的效果,现在贴图上来: 其实 ...
- [Android] 修改ImageView的图片颜色
有两种方法: 方法1: ImageView imageView = (ImageView) findViewById(R.id.arrow_image); Drawable tipsArrow = i ...
- android 自定义组件-带图片的textView
1. 定义属性 <?xml version="1.0" encoding="utf-8"?> <resources> <decla ...
随机推荐
- Oracle学习系列5
Oracle学习系列5 ************************************************************************************ ,掌握 ...
- 使用as3控制动画的播放与暂停
1.需要两个按钮元件 2.在属性面板为两个按钮元件分别命名为pausebutton与playButton 3.代码 stop(); pausebutton.visible = false; playB ...
- [转]Dll注入经典方法完整版
Pnig0s1992:算是复习了,最经典的教科书式的Dll注入. 总结一下基本的注入过程,分注入和卸载 注入Dll: 1,OpenProcess获得要注入进程的句柄 2,VirtualAllocEx在 ...
- MySQL查询今天/昨天/本周、上周、本月、上个月份数据的sql代码
MySQL查询本周.上周.本月.上个月份数据的sql代码 作者: 字体:[增加 减小] 类型:转载 时间:2012-11-29我要评论 MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查 ...
- MYSQL EXPLAIN 很慢的原因
今天同事在查看一个SQL的执行计划的时候,EXPLAIN语句跑了2分钟.SQL命令类似: SELECT * FROM (SELECT USERID,COUNT(*) FROM TBNAME GROUP ...
- Cannot find executable for CFBundle 解决办法
出错日志为:2013-12-29 01:17:49.785 Displaying Alerts with UIAlertView[419:70b] Cannot find executable for ...
- [jQuery]html(),text(),val()方法的区别
1.HTML html():取得第一个匹配元素的html内容.这个函数不能用于XML文档.但可以用于XHTML文档 html(val):设置每一个匹配元素的html内容.这个函数不能用于XML文档.但 ...
- 自定义表并实现Identity登录(一)
注意,Microsoft.AspNet.Identity.Core.1.0.0和Microsoft.AspNet.Identity.Core.2.2.1差别太大,需考虑实际项目中用的是哪种,本文是基于 ...
- [Git].gitignore失效的原因
使用git管理源代码已经成为现在开源社区的一大选择. 开发的人都知道,在源代码管理中,我们需要监控和备份的是代码,而不是开发过程中生成的exe和dll文件.//即使在某些时候,我们需要某些dll,我们 ...
- android开发之单选按钮
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...