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

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.ShapeableImageView
        android: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.ShapeableImageView
        android: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.ShapeableImageView
        android: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.ShapeableImageView
        android: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.ShapeableImageView
        android: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.ShapeableImageView


class MainActivity : AppCompatActivity() {
    companion object {
        const val DEGREE = -60
        const 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.kler.cn/news/135534.html

相关文章:

  • 【双指针】快乐数
  • Wireshark TS | 应用传输缓慢问题
  • 【运维篇】Redis 性能测试工具实践
  • 米家竞品分析
  • OceanBase 4.2.1 LTS 发版 | 一体化数据库首个长期支持版本
  • 数据结构与算法之美学习笔记:22 | 哈希算法(下):哈希算法在分布式系统中有哪些应用?
  • 面向开发者的Android
  • CXL崛起:2024启航,2025年开启新时代
  • 前端食堂技术周刊第 105 期:TS 5.3 RC、Vite 5.0、W3C 新任 CEO、有害的 Pinia 模式、2024 更快的 Web
  • Kotlin学习之函数
  • 【HarmonyOS开发】配置开发工具DevEco Studio
  • 基于SSM的高校毕业选题管理系统设计与实现
  • kubernetes部署jenkins
  • flink源码分析之功能组件(一)-metrics
  • openGauss学习笔记-128 openGauss 数据库管理-设置透明数据加密(TDE)
  • Hibernate 函数 ,子查询 和原生SQL查询
  • YOLOV8部署Android Studio安卓平台NCNN
  • Java Web——JS中的BOM
  • CISP模拟试题(三)
  • 前置语音群呼与语音机器人群呼哪个更好
  • python-opencv 培训课程笔记(1)
  • React整理总结(四)
  • LeetCode 面试题 16.25. LRU 缓存
  • js 对象数组删除某一个特定的对象
  • 达索系统3DEXPERIENCE云端设计新体验
  • CSS-表格属性(1)
  • docker数据卷详细讲解及数据卷常用命令
  • 【计算机视觉】24-Object Detection
  • Django 路由配置(二)
  • ESP32-BLE基础知识