• 自定义实现:头像上传View


    记录demo中实现一个圆形头像的选择与上传

    先自定义实现头像上传View 

    1. /**
    2. * 带有自定义字体TextView。
    3. */
    4. class EditAvatarUploadView : AppCompatTextView {
    5. lateinit var paint:Paint
    6. constructor(context: Context) : this(context, null){
    7. iniPaint()
    8. }
    9. constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0){
    10. iniPaint()
    11. }
    12. constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
    13. iniPaint()
    14. }
    15. override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
    16. super.onLayout(changed, left, top, right, bottom)
    17. iniPaint()
    18. }
    19. private fun iniPaint() {
    20. paint = Paint()
    21. paint.setAntiAlias(true)
    22. paint.setColor(Color.LTGRAY)
    23. paint.setStyle(Paint.Style.STROKE)
    24. paint.textSize= 20F
    25. }
    26. override fun onDraw(canvas: Canvas?) {
    27. super.onDraw(canvas)
    28. //得到屏幕宽高
    29. var width = width
    30. var radius = width - 300 / 2
    31. var height = height
    32. canvas!!.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), radius.toFloat(), paint)
    33. //绘制一条直线(两点确定一线)
    34. /* paint.setColor(Color.RED)
    35. canvas.drawLine(5F, (height/2+50).toFloat(), width.toFloat()-5, (height/2+50).toFloat(), paint)*/
    36. drawMask(canvas)
    37. drawCam(canvas)
    38. drawText(canvas)
    39. }
    40. fun drawMask(canvas: Canvas) {
    41. val bitmap =
    42. BitmapFactory.decodeResource(context.getResources(), R.mipmap.login_upload_half_mask)
    43. val dst = Rect(6, 60, 293, 160)
    44. /***********绘制圆弧
    45. * 矩形的宽度width=C(right)-A(left),高度height=D(bottom)-B(top)*/
    46. canvas.translate(0F, (width/2-10).toFloat())
    47. val rectf_head = RectF(1f, 0f, 291f, 207f) //确定外切矩形范围
    48. rectf_head.offset(5f, 90f) //使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
    49. //开始角度(以时钟3点的方向为0°,逆时针为正方向)
    50. //(以时钟3点的方向为0°,逆时针为正方向)
    51. //canvas.drawArc(rectf_head, 0F, 180F, false, paint) //绘制圆弧,不含圆心
    52. canvas.drawBitmap(bitmap, null, dst, null);
    53. }
    54. fun drawCam(canvas: Canvas) {
    55. val bitmap =
    56. BitmapFactory.decodeResource(context.getResources(), R.mipmap.login_edit_camera_icon)
    57. val dst = Rect(10, 60, 50, 100)
    58. dst.offset(70, 20)
    59. canvas.drawBitmap(bitmap, null, dst, null);
    60. }
    61. fun drawText(canvas: Canvas) {
    62. val text = "点击上传"
    63. paint.setColor(Color.WHITE)
    64. //x表示文本原点的x坐标;y表示文本基线的y坐标。
    65. canvas.drawText(text, 130F, 110F, paint)
    66. }
    67. }

    看看效果: 

    BitmapShader实现圆形的渲染:

    BitmapShader是Shader的子类,BitmapShader可以根据设置的方式将图片铺满所选区域: 
    (1)CLAMP:拉伸,在x方向上是图片的最后一列像素重复平铺,而y方向是最后一行往下拉伸 
    (2)REPEAT: 重复,很容易理解,图片重复平铺过去 
    (3)MIRROR:镜像,就是将图片翻转

    先初始化Paint对象:

    1. mBitmapShader = BitmapShader(mBitmap!!, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
    2. paint.setAntiAlias(true)
    3. paint.shader = mBitmapShader
    4. paint.setColor(Color.LTGRAY)

    在onDraw方法中,用这个Paint画出来我们想要的圆形和边框:

    1. override fun onDraw(canvas: Canvas) {
    2. /*先绘制背景,再绘制图片,最后再绘制边框,*/
    3. if (drawable == null) {
    4. return
    5. }
    6. mDrawableRadius = (width - 300 / 2).toFloat()
    7. canvas.drawCircle(
    8. (width / 2).toFloat(),
    9. (height / 2).toFloat(),
    10. mDrawableRadius,
    11. paint
    12. )
    13. // 绘制边框
    14. if (mBorderWidth != 0) {
    15. canvas.drawCircle(
    16. (width / 2).toFloat(),
    17. (height / 2).toFloat(),
    18. mBorderRadius,
    19. mBorderPaint
    20. )
    21. }
    22. drawMask(canvas)
    23. drawCam(canvas)
    24. drawText(canvas)
    25. }

    测试发现多次替换图片时由于缓存原因,图片更新不及时 

    1. val optionsa = RequestOptions()
    2. /* optionsa.placeholder(R.mipmap.ic_launcher)
    3. optionsa.error(R.mipmap.ic_launcher_round) //异常显示图*/
    4. optionsa.diskCacheStrategy(DiskCacheStrategy.NONE) //禁用掉Glide的缓存功能
    5. optionsa.skipMemoryCache(true) //禁用掉Glide的内存缓存
    6. //file:/storage/emulated/0/Android/data/com.jiangxue.heartview/cache/avatar.png
    7. val loadSdCard = ImageUtil.loadSdCard(editAvatarView, pathList?.get(0))
    8. Glide.with(this).load(loadSdCard).apply(optionsa).into(editAvatarView)

    代码地址

  • 相关阅读:
    拒绝水文!八大排序(三)【适合初学者】快速排序
    Ubuntu安装redis和redis-php扩展
    【DDPM论文解读】Denoising Diffusion Probabilistic Models
    谷歌新AI火了!世界最长单词都能画:Pneumonoultramicroscopicsilicovolcanoconiosis
    简单写个JS插件替换网页上的文本
    Spring Boot JPA 存储库派生查询示例
    Redis
    电脑屏幕模糊?这5个方法教你恢复清晰屏幕!
    二手安捷伦E9323A射频传感器
    Unity技术手册-初识编辑器(上)
  • 原文地址:https://blog.csdn.net/oqzuser1q2w3e4r5t/article/details/133011900