设为首页 加入收藏

TOP

自定义控件之 圆形 / 圆角 ImageView(三)
2017-10-12 17:57:46 】 浏览:3536
Tags:定义 控件 圆形 圆角 ImageView
;borderColor"
/> 8 <attr name="borderWidth" /> 9 </declare-styleable> 10 </resources> View attrs_imageviewplus.xml

 然后在ImageViewPlus的构造函数中去读取这些自定义属性:

 1     private static final int DEFAULT_BORDER_COLOR = Color.TRANSPARENT;
 2     private static final int DEFAULT_BORDER_WIDTH = 0;
 3     
 4     public ImageViewPlus(Context context, AttributeSet attrs) {
 5         super(context, attrs);
 6         //取xml文件中设定的参数
 7         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ImageViewPlus);
 8         mBorderColor = ta.getColor(R.styleable.ImageViewPlus_borderColor, DEFAULT_BORDER_COLOR);
 9         mBorderWidth = ta.getDimensionPixelSize(R.styleable.ImageViewPlus_borderWidth, dip2px(DEFAULT_BORDER_WIDTH));
10         ta.recycle();
11     }
View Code

 在xml布局中使用自定义属性:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     xmlns:snser="http://schemas.android.com/apk/res/cc.snser.imageviewplus"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:background="@drawable/wallpaper"
 7     android:orientation="vertical"
 8     tools:context="${relativePackage}.${activityClass}" >
 9     
10     <cc.snser.imageviewplus.ImageViewPlus
11         android:id="@+id/imgplus"
12         android:layout_width="200dp"
13         android:layout_height="300dp"
14         android:layout_marginBottom="50dp"
15         android:layout_centerHorizontal="true"
16         android:layout_alignParentBottom="true"
17         android:src="@drawable/img_square"
18         snser:borderColor="#FF0080FF"
19         snser:borderWidth="15dp" />
20     
21 </RelativeLayout>
View Code

[转载请保留本文地址:http://www.cnblogs.com/snser/p/5159126.html] 

六、更多玩法 —— 圆角ImageView

搞定了圆形ImageView以及对应的边框,那如何实现下面这种圆角的ImageView呢?

 

其实原理上一样,把 canvas.drawCircle 对应改成 canvas.drawRoundRect 就OK了,直接贴代码吧:

  1 public class ImageViewPlus extends ImageView{
  2     /**
  3      * android.widget.ImageView
  4      */
  5     public static final int TYPE_NONE = 0;
  6     /**
  7      * 圆形
  8      */
  9     public static final int TYPE_CIRCLE = 1;
 10     /**
 11      * 圆角矩形
 12      */
 13     public static final int TYPE_ROUNDED_RECT = 2;    
 14     
 15     private static final int DEFAULT_TYPE = TYPE_NONE;
 16     private static final int DEFAULT_BORDER_COLOR = Color.TRANSPARENT;
 17     private static final int DEFAULT_BORDER_WIDTH = 0;
 18     private static final int DEFAULT_RECT_ROUND_RADIUS = 0;
 19     
 20     private int mType;
 21     private int mBorderColor;
 22     private int mBorderWidth;
 23     private int mRectRoundRadius;
 24     
 25     private Paint mPaintBitmap = new Paint(Paint.ANTI_ALIAS_FLAG);
 26     private Paint mPaintBorder = new Paint(Paint.ANTI_ALIAS_FLAG);
 27     
 28     private RectF mRectBorder = new RectF();
 29     private RectF mRectBitmap = new RectF();
 30     
 31     private Bitmap mRawBitmap;
 32     private BitmapShader mShader;
 33     private Matrix mMatrix = new Matrix();
 34     
 35     public ImageViewPlus(Context context, AttributeSet attrs) {
 36         super(context, attrs);
 37         //取xml文件中设定的参数
 38         TypedArray ta = context.obtainStyledAttributes(attrs, R.
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【转】Android Studio下加入百度.. 下一篇注册时获取验证码常用的倒计时工具

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目