Android Compose 七:常用组件 Image

1 基本使用

在这里插入图片描述

 Image(painter = painterResource(id = R.drawable.ic_wang_lufei), contentDescription = "" )  // 图片
       Spacer(modifier = Modifier.height(20.dp))
       Image(imageVector = ImageVector.vectorResource(id = R.drawable.ic_android_black_24dp), contentDescription = "")  //矢量图
       Spacer(modifier = Modifier.height(20.dp))
       Image(bitmap = ImageBitmap.imageResource(id = R.drawable.ic_wang_lufei), contentDescription = "") //bitmap 

效果
在这里插入图片描述

2 参数说明

以上三种创建方式,除了引用资源方式不同外,其他参数相同

fun Image(
    painter: Painter,
    contentDescription: String?,
    modifier: Modifier = Modifier,
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Fit,
    alpha: Float = DefaultAlpha,
    colorFilter: ColorFilter? = null
) 

fun Image(
    imageVector: ImageVector,
    contentDescription: String?,
    modifier: Modifier = Modifier,
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Fit,
    alpha: Float = DefaultAlpha,
    colorFilter: ColorFilter? = null
) 

fun Image(
    bitmap: ImageBitmap,
    contentDescription: String?,
    modifier: Modifier = Modifier,
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Fit,
    alpha: Float = DefaultAlpha,
    colorFilter: ColorFilter? = null,
    filterQuality: FilterQuality = DefaultFilterQuality
) 

点点点最后点到了 Layout 是不是就可以说是Image就是Layout 然后控制图片大小形状等信息

 Layout(
        {},
        modifier.then(semantics).clipToBounds().paint(
            painter,
            alignment = alignment,
            contentScale = contentScale,
            alpha = alpha,
            colorFilter = colorFilter
        )
    ) { _, constraints ->
        layout(constraints.minWidth, constraints.minHeight) {}
    }

2.1 资源加载参数

painter: Painter,
imageVector: ImageVector, //可以通过 ImageVector.vectorResource(id = ) 获取图片后做处理再显示
bitmap: ImageBitmap, //可以通过 ImageBitmap.imageResource(id = ) 获取图片后做处理再显示
实现bitmap等比缩放

 	Image(painter = painterResource(id = R.drawable.ic_wang_lufei), contentDescription = "王路飞1" )
    Spacer(modifier = Modifier.height(20.dp))
      
    var bitmap = ImageBitmap.imageResource(id = R.drawable.ic_wang_lufei)
    //获取图片宽高等比缩放
    val width = bitmap.width;
    val height = bitmap.height;

    Image(bitmap = bitmap,
        modifier = Modifier
            .width(pxToDp(px = width) / 2)
            .height(pxToDp(px = height)/2),
        contentDescription = "王路飞2")


效果
在这里插入图片描述

//ImageBitmap 转 Bitmap
       val asAndroidBitmap = bitmap.asAndroidBitmap()
       //Bitmap 转 ImageBitmap
       val  imageBitmap = asAndroidBitmap.asImageBitmap()
       Image(bitmap = imageBitmap,
           contentDescription = "王路飞2")

.asImageBitmap() 其实就是创建了一个ImageBitmap

fun Bitmap.asImageBitmap(): ImageBitmap = AndroidImageBitmap(this)

internal class AndroidImageBitmap(internal val bitmap: Bitmap) : ImageBitmap {...}
 //ImageBitmap 转 Bitmap
       val asAndroidBitmap = bitmap.asAndroidBitmap()
       //Bitmap 转 ImageBitmap
       val  imageBitmap = asAndroidBitmap.asImageBitmap()
       val bitmap1 = BitmapFactory.decodeResource(Resources.getSystem(),R.drawable.ic_wang_lufei1)
       Image(bitmap = bitmap1.asImageBitmap(),
           contentDescription = "王路飞2")`

效果
在这里插入图片描述

2.2 alignment 图片对齐方式

当框大于图片大小时

Image(painter = painterResource(id = R.drawable.ic_wang_lufei), contentDescription = "王路飞1" )
       Spacer(modifier = Modifier.height(20.dp))
       Image(
           painter = painterResource(id = R.drawable.ic_wang_lufei),
           contentDescription = "王路飞1",
           modifier = Modifier.fillMaxWidth(),
           alignment = Alignment.Center
       )

效果 居中 显示效果 受contentScale影响
在这里插入图片描述

2.3 contentScale 预览效果

默认 contentScale: ContentScale = ContentScale.Fit,
在这里插入图片描述
contentScale = ContentScale.Crop 宽或高撑满布局 另外一方向居中显示

 Image(
           painter = painterResource(id = R.drawable.ic_wang_lufei),
           contentDescription = "王路飞1",
           modifier = Modifier.fillMaxWidth(),
           alignment = Alignment.Center,
           contentScale = ContentScale.Crop
       )

在这里插入图片描述
modifier = Modifier.fillMaxSize(),
在这里插入图片描述

contentScale = ContentScale.FillBounds
在这里插入图片描述
contentScale = ContentScale.FillHeight 高度撑满,宽度自适应
在这里插入图片描述
contentScale = ContentScale.Inside 显示图片大小
在这里插入图片描述
contentScale = ContentScale.FillWidth 宽度撑满高度自适应
在这里插入图片描述

contentScale = ContentScale.None
在这里插入图片描述

2.4 alpha = 0.5f, 透明度

Image(
           painter = painterResource(id = R.drawable.lufei3),
           contentDescription = "王路飞1",
           modifier = Modifier.width(300.dp).height(100.dp),
           alignment = Alignment.Center,
           contentScale = ContentScale.Crop,
           alpha = 0.5f,
       )

在这里插入图片描述

2.5 colorFilter = ColorFilter.lighting(multiply = Color.Red,add = Color.Black) 颜色过滤器

Image(
           painter = painterResource(id = R.drawable.lufei3),
           contentDescription = "王路飞1",
           modifier = Modifier.width(300.dp).height(100.dp),
           alignment = Alignment.Center,
           contentScale = ContentScale.Crop,
           colorFilter = ColorFilter.lighting(multiply = Color.Red,add = Color.Black)
       )

在这里插入图片描述

        /**
         * Create a [ColorFilter] that can be used to simulate simple lighting effects.
         * A lighting ColorFilter is defined by two parameters, one used to multiply the source
         * color and one used to add to the source color
         *
         * @param multiply Color that will be added to the source color when the color
         *          filter is applied
         * @param add Color used to multiply the source color when the color filter is applied.
         */
        @Stable
        fun lighting(multiply: Color, add: Color): ColorFilter =
            actualLightingColorFilter(multiply, add)

翻译
在这里插入图片描述
另外两个滤波

        /**
         * Creates a color filter that applies the blend mode given as the second
         * argument. The source color is the one given as the first argument, and the
         * destination color is the one from the layer being composited.
         *
         * The output of this filter is then composited into the background according
         * to the [Paint.blendMode], using the output of this filter as the source
         * and the background as the destination.
         *
         * @param color Color used to blend source content
         * @param blendMode BlendMode used when compositing the tint color to the destination
         */
        @Stable
        fun tint(color: Color, blendMode: BlendMode = BlendMode.SrcIn): ColorFilter =
            actualTintColorFilter(color, blendMode)

        /**
         * Create a [ColorFilter] that transforms colors through a 4x5 color matrix. This filter can
         * be used to change the saturation of pixels, convert from YUV to RGB, etc.
         *
         * @param colorMatrix ColorMatrix used to transform pixel values when drawn
         */
        @Stable
        fun colorMatrix(colorMatrix: ColorMatrix): ColorFilter =
            actualColorMatrixColorFilter(colorMatrix)

默认的滤波器

@kotlin.jvm.JvmInline
value class ColorMatrix(
    val values: FloatArray = floatArrayOf(
        1f, 0f, 0f, 0f, 0f,
        0f, 1f, 0f, 0f, 0f,
        0f, 0f, 1f, 0f, 0f,
        0f, 0f, 0f, 1f, 0f
    )
) 

咱们稍微改个值

 Image(
           painter = painterResource(id = R.drawable.lufei3),
           contentDescription = "王路飞1",
           modifier = Modifier.width(300.dp).height(100.dp),
           alignment = Alignment.Center,
           contentScale = ContentScale.Crop,
//           colorFilter = ColorFilter.lighting(multiply = Color.Red,add = Color.Black)
            colorFilter = ColorFilter.colorMatrix(ColorMatrix(floatArrayOf(
            1f, 0f, 0f, 0f, 0f,
                0f, 0f, 0f, 0f, 0f,
                0f, 0f, 1f, 0f, 0f,
                0f, 0f, 0f, 1f, 1f
            )))
       )

效果
在这里插入图片描述
实际使用中需要什么效果,咱也不知道,咱也不敢吭,试吧就

3 常用的图片效果

圆角矩形
圆形图片

  Image(
           painter = painterResource(id = R.drawable.ic_wang_lufei1),
           contentDescription = "",
           modifier = Modifier.clip(RoundedCornerShape(20.dp))
       )
       Spacer(modifier = Modifier.height(20.dp))
       Image(
           painter = painterResource(id = R.drawable.ic_wang_lufei1),
           contentDescription = "",
           modifier = Modifier.clip(CircleShape)
       )

在这里插入图片描述

加边框的效果

 Spacer(modifier = Modifier.height(20.dp))
       Image(
           painter = painterResource(id = R.drawable.ic_wang_lufei1),
           contentDescription = "",
           modifier = Modifier.border(2.dp, color = Color.Red)
       )
       Spacer(modifier = Modifier.height(20.dp))
       Image(
           painter = painterResource(id = R.drawable.ic_wang_lufei1),
           contentDescription = "",
           modifier = Modifier.clip(CircleShape) .border(2.dp, color = Color.Red, shape = CircleShape)
       )

效果
在这里插入图片描述

增加边框时 shape = CircleShape 需要与image的shap一致,否则效果如下
在这里插入图片描述

4实现阴影效果
 Image(
           painter = painterResource(id = R.drawable.ic_wang_lufei1),
           contentDescription = "",
           modifier = Modifier.clip(CircleShape) .border(2.dp, color = Color.Red, shape = CircleShape)
               .shadow(30.dp, shape = CircleShape,true,Color.Blue,Color.Yellow)
       )

给image 设置shadow好像无效果
在这里插入图片描述
前边Button里说过
最后调用了Box
在这里插入图片描述
于是

   Box(
           modifier = Modifier.shadow(30.dp, shape = CircleShape,true,Color.Blue,Color.Red),
       ){
           Image(
               painter = painterResource(id = R.drawable.ic_wang_lufei1),
               contentDescription = "",
               modifier = Modifier
                   .clip(CircleShape)
                   .border(2.dp, color = Color.Red, shape = CircleShape)

           )
       }

效果
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/656550.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Nature 正刊!瑞典于默奥大学研究团队在研究全球河流和溪流的甲烷排放中取得新进展

甲烷(CH4)是一种强有力的温室气体,自工业革命以来,其在大气中的浓度增加了两倍。有证据表明,全球变暖增加了淡水生态系统的 CH4 排放,为全球气候提供了积极的反馈。然而,对于河流和溪流来说,甲烷排放的控制…

618有什么宠物空气净化器推荐?希喂FreAir Lite宠物空气净化器真实体验

一、宠物空气净化器的必要性 掉毛季又来了,猫咪的毛发满天飞,怎么办?我家里的猫咪一到换毛季就掉满地的毛发,尤其喜欢在家里奔跑打闹,结果整个房间都是毛。为了减少家里空气中的浮毛,你都做过哪些努力呢&a…

电脑出现:excel词典(xllex.dll)文件丢失或损坏的错误提示怎么办?有效的将丢失的xllex.dll修复

当遇到 Excel 提示“词典 (xllex.dll) 文件丢失或损坏”的问题时,通常意味着该动态链接库文件(Dynamic Link Library,DLL),它与拼写检查功能相关联的,无法被正确找到或者合适地使用。那么有什么办法可以解决…

空间转录组数据的意义

10X空间转录组Visium学习笔记(三)跑通Visium全流程记录 | 码农家园 (codenong.com) 这两个的区别是:一个是像素的位置信息,一个是阵列的位置信息

第97天:权限提升-Web 权限权限划分源码后台中间件第三方数据库等

前置知识 具体有哪些权限需要我们了解掌握的 后台权限,网站权限,数据库权限,接口权限,系统权限,域控权限等 以上常见权限获取方法简要归类说明 后台权限:SQL 注入,数据库备份泄露,默认或弱口…

设计模型-系统架构师(三)

1、按照《中华人民共和国著作权法》的权利保护期,署名权和修改权 是永久保护的。 发表权、使用权和获取报酬权 保护期限 作者终生和死后的50年。 2、(重要)面向对象的分析模型主要由 顶层架构图、用例与用例图和()构成…

CTF网络安全大赛简单的web抓包题目:HEADache

题目来源于&#xff1a;bugku 题目难度&#xff1a;简单 题目 描  述: > Wanna learn about some types of headache? > Lets dig right into it! 下面是题目源代码&#xff1a; <!DOCTYPE html> <html> <head><meta charset"utf-8"&…

基于C#开发web网页管理系统模板流程-主界面管理员入库和出库功能完善

前言 紧接上篇->基于C#开发web网页管理系统模板流程-主界面管理员录入和编辑功能完善-CSDN博客 本篇将完善主界面的管理员入库和出库功能&#xff0c;同样的&#xff0c;管理员入库和出库的设计套路适用于动态表的录入和编辑 首先还是介绍一下本项目将要实现的功能 &#xf…

对象解构与迭代器的猫腻?

前言 变量的解构赋值是前端开发中经常用到的一个技巧&#xff0c;比如&#xff1a; // 对象解构 const obj { a: 1, b: 2 }; const { a, b } obj; console.log(a, b)数组解构 const arr [1, 2, 3]; const [a, b] arr; console.log(a, b)工作中我们最经常用的就是类似上面…

【Python安全攻防】【网络安全】一、常见被动信息搜集手段

一、IP查询 原理&#xff1a;通过目标URL查询目标的IP地址。 所需库&#xff1a;socket Python代码示例&#xff1a; import socketip socket.gethostbyname(www.163.com) print(ip)上述代码中&#xff0c;使用gethostbyname函数。该函数位于Python内置的socket库中&#xf…

②单细胞学习-组间及样本细胞比例分析

目录 数据读入 每个样本各细胞比例 两个组间细胞比例 亚组间细胞比例差异分析&#xff08;循环&#xff09; 单个细胞类型亚新间比例差异 ①单细胞学习-数据读取、降维和分群-CSDN博客 比较各个样本间的各类细胞比例或者亚组之间的细胞比例差异 ①数据读入 #各样本细胞…

ubuntu-24.04系统静态Mac和IP配置

操作系统版本&#xff08;桌面版&#xff09;&#xff1a;ubuntu-24.04-desktop-amd64.iso 原因说明&#xff1a;因网络的IP地址和Mac是预分配的&#xff0c;所以ubuntu系统需要修改网卡的mac地址和IP才能访问&#xff0c;网络查了半天资料都没成功&#xff0c;后再界面提示&a…

vue2 案例入门

vue2 案例入门 1 vue环境2 案例2.1 1.v-text v-html2.2 v-bind2.3 v-model2.4 v-on2.5 v-for2.6 v-if和v-show2.7 v-else和v-else-if2.8 计算属性和侦听器2.9 过滤器2.10 组件化2.11 生命周期2.12 使用vue脚手架2.13 引入ElementUI2.13.1 npm方式安装2.13.2 main.js导入element…

BIGO前端CICD平台

本文首发于&#xff1a;https://github.com/bigo-frontend/blog/ 欢迎关注、转载。 我是谁 BIGO前端CICD平台&#xff0c;是一个服务于前端团队的全研发周期管理平台&#xff0c;已经是我们团队日常都要使用的工具了。 该平台实现了一键创建项目、发布编排、新建迭代、checkl…

互联网应用主流框架整合之数据库事务管理

在互联网系统中同时运行着成千上百个线程是十分常见的事情&#xff0c;尤其当一个热门出现时&#xff0c;用户几乎同时打开手机、电脑、平板灯设备进行访问&#xff0c;这样就会导致数据库处在一个多事务访问的环境中&#xff0c;从而引发数据丢失或者数据不一致的现象&#xf…

Java GC问题排查的一些个人总结和问题复盘

个人博客 Java GC问题排查的一些个人总结和问题复盘 | iwts’s blog 是否存在GC问题判断指标 有的比较明显&#xff0c;比如发布上线后内存直接就起飞了&#xff0c;这种也是比较好排查的&#xff0c;也是最多的。如果单纯从优化角度&#xff0c;看当前应用是否需要优化&…

Mowgli用于配对多组学整合

对同一组细胞的多个分子层进行分析逐渐流行。越来越需要能够联合分析这些数据的多视图学习方法。Mowgli是一种支持配对多组学数据的整合方法。值得注意的是&#xff0c;Mowgli将非负矩阵分解和最优传输相结合&#xff0c;同时提高了非负矩阵分解的聚类性能和可解释性。作者将Mo…

解锁数据的力量:Navicat 17 新特性和亮点

解锁数据的力量&#xff1a;Navicat 17 新特性和亮点 大家好&#xff0c;我是猫头虎。今天我要为大家介绍 Navicat 17 的新特性和亮点。Navicat 是一款专业的数据库管理工具&#xff0c;支持多种数据库类型&#xff0c;包括 MySQL、Oracle、SQL Server、PostgreSQL、MariaDB、…

5月28号总结

刷题记录 1.A. Phone Desktop 输入&#xff1a; 11 1 1 7 2 12 4 0 3 1 0 8 1 0 0 2 0 15 0 8 2 0 9 输出&#xff1a; 1 1 2 2 1 1 0 1 1 2 5 题意&#xff1a;题目给我们1x1和2x2的图标个数&#xff0c;让我们求最少需要多少个5x3的屏幕。 思路&#xff1a;当只看2x2的图…

新建一个esri_sde_gists的服务

需求 新建一个esri_sde_gists的服务 步骤&#xff1a; 需要拷贝ora11gexe目标为新的目录&#xff0c;例如ora11gexe_gists 运行drivers找到etc下面的services文件&#xff0c;添加端口5152&#xff1a; 检查sde的library并创建&#xff1a; CREATE or REPLACE LIBRARY ST_S…