记录贴

现在制作圆形头像的第三方工具已经很多了,本帖只为记录自定义view学习过程。

1.主体代码部分

public class CirclePhotoView extends View {private int max;//最大进度private int roundColor;//圈颜色private int roundProgressColor;//进度颜色private int textColor;//文字颜色private int backgroundColor;//背景颜色private float textSize;//文字大小private float roundWidth;//圈宽度private boolean textShow;//是否显示文字private int progress;//当前进度private Paint mPaintCircle;      //画圆形图像的笔private Paint mPaintBorder;          //画圆形边界的笔private BitmapShader mBitmapShader;      //图像着色器,可以用来画圆private Matrix mMatrix;          //图片变换处理器-用来缩放图片以适应view控件的大小private int mWidth;        //获得控件宽度private int mHeight;             //获得控件高度private float mRadius;             //中心园的半径public static final int STROKE = 0;public static final int FILL = 1;private Bitmap bitmap;private float mBitmapHeight;private float mBitmapWidth;private  Bitmap afterBitmap ;public CirclePhotoView(Context context) {super(context);}public CirclePhotoView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);max = typedArray.getInteger(R.styleable.CustomProgressBar_max, 100);roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.RED);roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor, Color.BLUE);textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor, Color.GREEN);textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize, 55);roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth, 10);textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow, true);backgroundColor = typedArray.getColor(R.styleable.CustomProgressBar_backgroundColor, Color.GRAY);typedArray.recycle();initPaint();}private void initPaint() {//初始化图片变换处理器mMatrix = new Matrix();//圆形头像画笔设置mPaintCircle = new Paint();mPaintCircle.setColor(roundColor);mPaintCircle.setStyle(Paint.Style.FILL_AND_STROKE);mPaintCircle.setStrokeWidth(roundWidth);mPaintCircle.setAntiAlias(true);//边框设置mPaintBorder = new Paint();mPaintBorder.setAntiAlias(true);mPaintBorder.setStyle(Paint.Style.STROKE);mPaintBorder.setStrokeWidth(roundWidth);mPaintBorder.setColor(roundColor);}@Overrideprotected void onDraw(Canvas canvas) {canvas.drawColor(backgroundColor);//设置圆形图片背景色,和整体背景保持一致为好。mWidth = getWidth() / 2;mHeight = getHeight() / 2;mRadius = Math.min(mWidth, mHeight) - roundWidth;Drawable drawable = getBackground();if (drawable == null) {super.onDraw(canvas);} else {bitmap = ((BitmapDrawable) drawable).getBitmap();if(bitmap==null){return;}mBitmapHeight = bitmap.getHeight();mBitmapWidth = bitmap.getWidth();mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);float scale = Math.max(bitmap.getWidth(),bitmap.getHeight()) / Math.min(bitmap.getWidth(),bitmap.getHeight());mMatrix.setScale(scale,scale);mBitmapShader.setLocalMatrix(mMatrix);mPaintCircle.setShader(mBitmapShader);canvas.drawCircle(mWidth, mHeight, mRadius, mPaintCircle);canvas.drawCircle(mWidth, mHeight, mRadius + roundWidth / 2, mPaintBorder);/* 也可以绘制圆形ShapeDrawable shapeDrawble = new ShapeDrawable(new OvalShape());shapeDrawble.getPaint().setShader(mBitmapShader);shapeDrawble.setBounds(0,0,getWidth(),getHeight());shapeDrawble.draw(canvas);*/}}
}

2.自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CustomProgressBar"><attr name="roundProgressColor" format="color"></attr><attr name="roundColor" format="color"></attr><attr name="roundWidth" format="dimension"></attr><attr name="textSize" format="dimension"></attr><attr name="textColor" format="color"></attr><attr name="max" format="integer"></attr><attr name="textShow" format="boolean"></attr><attr name="backgroundColor" format="color"></attr></declare-styleable></resources>

3.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:lpq="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#cccccc"android:orientation="vertical"><ImageViewandroid:layout_marginTop="20dp"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@mipmap/s"/><com.example.lpq.myapplication.customview.CirclePhotoViewandroid:layout_marginTop="20dp"android:layout_centerVertical="true"android:layout_centerHorizontal="true"android:layout_width="100dp"android:layout_height="100dp"android:layout_gravity="center"android:background="@mipmap/s"lpq:textColor="@color/colorPrimary"lpq:roundColor = "@color/white"lpq:roundWidth ="1dp"lpq:backgroundColor="@color/gray_1"lpq:roundProgressColor ="@color/colorPrimaryDark"/></LinearLayout>

Android自定义View之圆形头像相关推荐

  1. Android 自定义View实现圆形头像(适用于任意布局)

    先看效果图: 先来说下我的思路:首先我需要在自定义View中动态获取头像id,那么就需要在attrs文件中,写一个关于该View类的自定义属性.这里仿照ImageView,取名为src,类型为refe ...

  2. android 清空canvas部分内容_Android自定义View实现圆形头像效果

    在我们的APP中通常会遇到,展示圆形头像的需求,一般通过Glide就能实现,但是让我们做一个圆形头像,如果让我们自定义实现这种效果,该怎样做呢? 好,接下来本文通过三种方式来实现这种效果! 注意:这是 ...

  3. android view 渐变动画,Android自定义view渐变圆形动画

    本文实例为大家分享了Android自定义view渐变圆形动画的具体代码,供大家参考,具体内容如下 直接上效果图 自定义属性 attrs.xml文件 创建一个类 ProgressRing继承自 view ...

  4. Android自定义view,圆形的TextView,并通过xml设置属性,AttributeSet中取值

    Android自定义view设置xml属性 一个圆形的自定义TextView,通过xml来设置背景颜色的属性 values/attrs <declare-styleable name=" ...

  5. android 环绕布局,Android自定义View实现圆形环绕效果

    之前项目中需要实现一个四周环绕中心圆形头像的效果,感觉还是自定义比较方便,于是就自己封装了一个控件去实现.先贴张图显示最终效果. 首先自定义一个View继承自LinearLayout,通过动态添加ch ...

  6. Android自定义滑动进度条,Android自定义View实现圆形水波进度条

    每次听到某大牛谈论自定义View,顿时敬佩之心,如滔滔江水连绵不绝,心想我什么时候能有如此境界,好了,心动不如行动,于是我开始了自定义View之路,虽然过程有坎坷,但是结果我还是挺满意的.我知道大牛还 ...

  7. 自定义View,圆形头像

    1. 效果图 2. xml中 <com.etoury.etoury.ui.view.CircleImgandroid:id="@+id/user_info_head_img" ...

  8. Android自定义view之圆形进度条

    本节介绍自定义view-圆形进度条 思路: 根据前面介绍的自定义view内容可拓展得之: 1:新建类继承自View 2:添加自定义view属性 3:重写onDraw(Canvas canvas) 4: ...

  9. android 缺半圆形头像,Android 自定义Glide实现圆形头像效果(圆角,正常,黑白,圆形效果)...

    /** * Glide 图片加载工具类 */ /** * diskCacheStrategy参数补充 * * DiskCacheStrategy.NONE 表示不缓存任何内容. * * DiskCac ...

最新文章

  1. 利用正则来判断一个数字的范围
  2. 找到指定的新类型字符
  3. python备份发包脚本_Python备份脚本,python
  4. 计算机编程术语理解,计算机编程常用术语中英对照
  5. 少儿编程150讲轻松学Scratch(十二)-Scratch编程算法练习-选择排序
  6. Android的sdk、api及工程目录说明
  7. nginx 和 nodejs配置使用搭建网站
  8. 系统待办事项设计_B端产品工作台设计详解
  9. 常用零部件表面粗糙度标注及表面处理技术
  10. 自动化测试全流程总结
  11. java bouncycastle_BouncyCastle
  12. 高光谱遥感数据光谱特征的提取与应用---高光谱基础知识科普论文
  13. 【耀杨闯荡华儿街】(面试官)曹阿门:给我讲讲多线程;耀杨:md心态崩了~
  14. NMOS的栅极充电过程
  15. myeclipse新建项目部署到tomcat中,点击finish键没反应
  16. 步进电机T型和S型速度曲线
  17. java excel 设置列为日期,POI - 如何将单元格值设置为日期并应用默认Excel日期格式?...
  18. 如何实现一个精简版的redux-saga
  19. 兰州理工大学计算机专业课,兰州理工大学计算机专业复试科目
  20. PhysX初步学习内容记录

热门文章

  1. GDC翻译:Far Cry 5 的程序化世界生成(第三部分:7-生态工具(Biome Tool))
  2. matlab怎么车牌识别,利用matlab来做车牌识别
  3. 细微之处见真章之JSON格式美化
  4. python中文件的打开与关闭_python中的文件打开与关闭操作命令介绍
  5. GAT1400:视图库对象
  6. Chrome保存网页为mhtml格式
  7. amd显卡风扇调节_让超频更容易 WattMan全解析(2)
  8. 人才消费价值回归 学历虚高将会远离职场
  9. 基于共词分析的中国近代史实体关系图构建(毕业设计:数据处理)
  10. 【杂题】超级公牛冠军赛(最大生成树)