码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Android描边外框stroke边线、rotate旋转、circle圆形图的简洁通用方案,基于Glide与ShapeableImageView,Kotlin


    Android描边外框stroke边线、rotate旋转、circle圆形图的简洁通用方案,基于Glide与ShapeableImageView,Kotlin

    利用ShapeableImageView专门处理圆形和外框边线的特性,通过Glide加载图片装载到ShapeableImageView。注意,因为要描边,在xml定义ShapeableImageView时候,padding值与stroke值要保持一直,否则,圆图会在某些边缘地方被切边。
    旋转的话,可以在上层Kotlin代码设置rotation(动态设置,灵活),旋转ShapeableImageView;也可以在xml里面写死rotation值(静态配置,不灵活)。
    ShapeableImageView通过配置shapeAppearance改造成圆形图。

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. android:orientation="vertical">
    7. <com.google.android.material.imageview.ShapeableImageView
    8. android:id="@+id/image1"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:layout_gravity="center"
    12. android:background="@drawable/ic_launcher_background"
    13. android:padding="30px"
    14. android:src="@drawable/ic_launcher_foreground"
    15. app:shapeAppearance="@style/rounded_style"
    16. app:strokeColor="@android:color/holo_red_dark"
    17. app:strokeWidth="30px" />
    18. <com.google.android.material.imageview.ShapeableImageView
    19. android:id="@+id/image2"
    20. android:layout_width="wrap_content"
    21. android:layout_height="wrap_content"
    22. android:layout_gravity="center"
    23. android:background="@drawable/ic_launcher_background"
    24. android:padding="30px"
    25. android:src="@drawable/ic_launcher_foreground"
    26. app:shapeAppearance="@style/rounded_style"
    27. app:strokeColor="@android:color/holo_red_dark"
    28. app:strokeWidth="30px" />
    29. <com.google.android.material.imageview.ShapeableImageView
    30. android:id="@+id/image3"
    31. android:layout_width="wrap_content"
    32. android:layout_height="wrap_content"
    33. android:layout_gravity="center"
    34. android:background="@drawable/ic_launcher_background"
    35. android:padding="30px"
    36. android:src="@mipmap/pic1"
    37. app:shapeAppearance="@style/rounded_style"
    38. app:strokeColor="@android:color/holo_red_dark"
    39. app:strokeWidth="30px" />
    40. <com.google.android.material.imageview.ShapeableImageView
    41. android:id="@+id/image4"
    42. android:layout_width="wrap_content"
    43. android:layout_height="wrap_content"
    44. android:layout_gravity="center"
    45. android:background="@drawable/ic_launcher_background"
    46. android:padding="30px"
    47. android:rotation="-30"
    48. android:src="@mipmap/pic1"
    49. app:shapeAppearance="@style/rounded_style"
    50. app:strokeColor="@android:color/holo_red_dark"
    51. app:strokeWidth="30px" />
    52. <com.google.android.material.imageview.ShapeableImageView
    53. android:id="@+id/image5"
    54. android:layout_width="wrap_content"
    55. android:layout_height="wrap_content"
    56. android:layout_gravity="center"
    57. android:background="@drawable/ic_launcher_background"
    58. android:padding="30px"
    59. android:rotation="-30"
    60. android:scaleType="centerCrop"
    61. android:src="@mipmap/pic1"
    62. app:shapeAppearance="@style/rounded_style"
    63. app:strokeColor="@android:color/holo_red_dark"
    64. app:strokeWidth="30px" />
    65. LinearLayout>

    styles.xml:

    1. "1.0" encoding="utf-8"?>
    2. <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    3. <style name="rounded_style">
    4. <item name="cornerFamily">roundeditem>
    5. <item name="cornerSize">50%item>
    6. style>
    7. resources>

    1. import android.os.Bundle
    2. import androidx.appcompat.app.AppCompatActivity
    3. import com.bumptech.glide.load.resource.bitmap.CenterCrop
    4. import com.google.android.material.imageview.ShapeableImageView
    5. class MainActivity : AppCompatActivity() {
    6. companion object {
    7. const val DEGREE = -60
    8. const val SIZE = 500
    9. }
    10. override fun onCreate(savedInstanceState: Bundle?) {
    11. super.onCreate(savedInstanceState)
    12. setContentView(R.layout.activity_main)
    13. val iv1 = findViewById(R.id.image1)
    14. GlideApp.with(this)
    15. .load(R.mipmap.pic1)
    16. .transform(CenterCrop())
    17. .error(android.R.drawable.stat_notify_error)
    18. .override(SIZE)
    19. .into(iv1)
    20. val iv2 = findViewById(R.id.image2)
    21. iv2.rotation = DEGREE.toFloat()
    22. GlideApp.with(this)
    23. .load(R.mipmap.pic1)
    24. .transform(CenterCrop())
    25. .error(android.R.drawable.stat_notify_error)
    26. .override(SIZE)
    27. .into(iv2)
    28. }
    29. }

    Android Glide加载transform CenterCrop, CircleCrop ShapeableImageView圆形图并描边,Kotlin-CSDN博客文章浏览阅读446次。Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案RoundedBitmapDrawable是Android在support v4的扩展包中新增的实现圆角图形的关键类,借助RoundedBitmapDrawable的帮助,可以轻松的以Android标准方式实现圆角图形图象。现在结合他人的代码加以修改,给出一个以原始图形中心为原点,修剪图片为头像的工具类,此类可以直接在布局文件中加载使用,比。所实现的在Kotlin动态代码中绘制的描边效果。https://blog.csdn.net/zhangphil/article/details/134297059

  • 相关阅读:
    Dcoker学习笔记(一)
    【GEE笔记4】GEE的数据下载和上传(Google Drive和Google Assets)
    VMware中安装centos无网络,配置教程
    老友记第一季21集背诵句
    前端工作小结81-状态管理里面取值
    Android四大组件之- Service的创建与应用
    CTFHub技能树web之XSS
    机器学习模型评价指标
    picoctf_2018_got_shell
    浅谈电气防火保护器在地下商场的应用
  • 原文地址:https://blog.csdn.net/zhangphil/article/details/134314380
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号