当前位置: 首页 > news >正文

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改造成圆形图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.google.android.material.imageview.ShapeableImageViewandroid:id="@+id/image1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@drawable/ic_launcher_background"android:padding="30px"android:src="@drawable/ic_launcher_foreground"app:shapeAppearance="@style/rounded_style"app:strokeColor="@android:color/holo_red_dark"app:strokeWidth="30px" /><com.google.android.material.imageview.ShapeableImageViewandroid:id="@+id/image2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@drawable/ic_launcher_background"android:padding="30px"android:src="@drawable/ic_launcher_foreground"app:shapeAppearance="@style/rounded_style"app:strokeColor="@android:color/holo_red_dark"app:strokeWidth="30px" /><com.google.android.material.imageview.ShapeableImageViewandroid:id="@+id/image3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@drawable/ic_launcher_background"android:padding="30px"android:src="@mipmap/pic1"app:shapeAppearance="@style/rounded_style"app:strokeColor="@android:color/holo_red_dark"app:strokeWidth="30px" /><com.google.android.material.imageview.ShapeableImageViewandroid:id="@+id/image4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@drawable/ic_launcher_background"android:padding="30px"android:rotation="-30"android:src="@mipmap/pic1"app:shapeAppearance="@style/rounded_style"app:strokeColor="@android:color/holo_red_dark"app:strokeWidth="30px" /><com.google.android.material.imageview.ShapeableImageViewandroid:id="@+id/image5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@drawable/ic_launcher_background"android:padding="30px"android:rotation="-30"android:scaleType="centerCrop"android:src="@mipmap/pic1"app:shapeAppearance="@style/rounded_style"app:strokeColor="@android:color/holo_red_dark"app:strokeWidth="30px" /></LinearLayout>

styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"><style name="rounded_style"><item name="cornerFamily">rounded</item><item name="cornerSize">50%</item></style>
</resources>

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.google.android.material.imageview.ShapeableImageViewclass MainActivity : AppCompatActivity() {companion object {const val DEGREE = -60const val SIZE = 500}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val iv1 = findViewById<ShapeableImageView>(R.id.image1)GlideApp.with(this).load(R.mipmap.pic1).transform(CenterCrop()).error(android.R.drawable.stat_notify_error).override(SIZE).into(iv1)val iv2 = findViewById<ShapeableImageView>(R.id.image2)iv2.rotation = DEGREE.toFloat()GlideApp.with(this).load(R.mipmap.pic1).transform(CenterCrop()).error(android.R.drawable.stat_notify_error).override(SIZE).into(iv2)}
}

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

http://www.lryc.cn/news/237044.html

相关文章:

  • virtualbox7 虚拟机与宿主机互传文件、共享u盘
  • 浅谈多回路电表在荷兰光伏系统配电项目中的应用
  • VBA之Word应用:文档(Document)的书签
  • 四旋翼无人机的飞行原理--【其利天下分享】
  • webpack的安全保障是怎么做的?
  • Python3.10的一些新特性与使用场景
  • VS2022 配置 OpenCV并开始第一个程序
  • 图像处理01 小波变换
  • 构建自定义ChatGPT,微软推出Copilot Studio
  • 什么是Mock?为什么要使用Mock呢?
  • elementui表格自定义指令控制显示哪些列可以拖动
  • Motion Plan之搜素算法笔记
  • 新中新身份证阅读器驱动下载sdk DKQ-A16D
  • 世界坐标系,相机坐标系,像素坐标系转换 详细说明(附代码)
  • 计算机毕业设计 基于SpringBoot的企业内部网络管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • CISP模拟试题(三)
  • 前端调取摄像头并实现拍照功能
  • android —— 阴影效果和跑马灯效果Textview
  • 多态语法详解
  • Python大数据之linux学习总结——day11_ZooKeeper
  • C语言——函数的嵌套调用
  • 4种经典的限流算法与集群限流
  • 网工内推 | 国企、港企网工,年底双薪,NA以上认证即可
  • 【华为HCIP | 华为数通工程师】刷题日记1116(一个字惨)
  • ​软考-高级-系统架构设计师教程(清华第2版)【第7章 系统架构设计基础知识(263~285)-思维导图】​
  • ⑩⑥ 【MySQL】详解 触发器TRIGGER,协助 确保数据的完整性,日志记录,数据校验等操作。
  • 数据结构与算法编程题3
  • Go基础面经大全(持续补充中)
  • uniapp heckbox-group实现多选
  • 读懂:“消费报销”模式新零售打法,适用连锁门店加盟的营销方案