Android Compose 七:常用组件 Image

news2024/9/22 23:20:03

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)

           )
       }

效果
在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python中的SSH、SFTP和FTP操作详解

大家好,在网络编程中,安全地连接到远程服务器并执行操作是一项常见任务。Python 提供了多种库来实现这一目标,其中 Paramiko 是一个功能强大的工具,可以轻松地在 Python 中执行 SSH、SFTP 和 FTP 操作。本文将介绍如何使用 Parami…

动态规划-二维费用的背包问题

文章目录 1. 一和零(474)2. 盈利计划(879) 1. 一和零(474) 题目描述: 状态表示: 我们之前的01背包问题以及完全背包问题都是一维的,因为我们只有一个要求或者说是限制那…

IT技术 | 电脑蓝屏修复记录DRIVER_IRQL_NOT_LESS_OR_EQUAL

我的台式机是iMac 2015年的,硬盘是机械的,时间久了运行越来越慢。后来对苹果系统失去了兴趣,想换回windows,且想换固态硬盘,就使用winToGo 搞了双系统,在USB外接移动固态硬盘上安装了win10系统。 最近&…

安卓六种页面加载优化方案对比总结

根据工作经验,笔者提炼了六种页面加载优化方式,按照业务与非业务,将六种加载方式分为两类: 业务类 控制业务与UI的执行顺序、控制多业务之间的执行顺序 ①预加载:是指在进入页面之前,提前获得页面所需得数据…

2024经济管理、社会科学与教育国际会议(ICEMSSE 2024)

2024经济管理、社会科学与教育国际会议(ICEMSSE 2024) 会议简介 2024年国际经济管理、社会科学和教育会议(ICEMSSE 2024)专注于经济、社会发展和教育。会议旨在为专家、学者和社会人士提供一个交流平台。通过讨论科学研究成果和前沿技术,我…

浅谈配置元件之CSV 数据文件设置

浅谈配置元件之CSV 数据文件设置 为了增强测试的真实性和多样性,JMeter 提供了多种数据参数化的方式,其中 CSV 数据文件设置(CSV Data Set Config)是一种常用且强大的功能,它允许测试脚本从外部CSV文件中动态读取数据…

基于springboot+vue的“漫画之家”系统

开发语言:Java框架:springbootJDK版本:JDK1.8服务器:tomcat7数据库:mysql 5.7(一定要5.7版本)数据库工具:Navicat11开发软件:eclipse/myeclipse/ideaMaven包:…

Samtec mPower®电源连接器:高能、可靠、灵活、小巧

【摘要/前言】 电源连接器是互连解决方案中不可或缺的一个组成部分。虽然相较于比较爱“竞速”的信号连接器,电源连接器的技术迭代不是那么频繁,但是它是连接电源和用电设备的重要纽带,想要确保设备正常运行,就少不了它的身影。 …

EPSON RX8111CE+松下高性能电池的组合应用

RTC是一种实时时钟,用于记录和跟踪时间,具有独立供电和时钟功能。在某些应用场景中,为了保证RTC在断电或者其他异常情况下依然能够正常工作,需要备份电池方案来提供稳定的供电。本文将介绍EPSON爱普生nA级RTC RX8111CE松下Panason…

ESP32-C6构建Matter Wi-Fi和Thread终端设备的利器,启明云端乐鑫代理商

随着物联网技术的飞速发展,越来越多的智能设备开始融入我们的日常生活。在智能家居和智能家电领域,ESP32系列产品以其高性能的微控制器(MCU)和无线通信解决方案而深入人心。 作为乐鑫代理商,启明云端为广大开发者提供…

Python代码:十八、生成数字列表

1、描述 牛牛在牛客网系统录入了一连串数字,数字之间依靠逗号隔开,你能帮助他将这些数字存储在列表中吗,列表元素以int的形式。 输入描述: 输入一行整数,数字之间以空格间隔。 输出描述: 输出这些数字…

【5G NB-IoT NTN】基于IoT NTN实现卫星语音通信的关键技术研究

近年来,随着商业航天的兴起,卫星制造和发射成本大幅降低,星载天线技术快速发展,为卫星通信产业注入了新的活力。同时,随着通信芯片、终端能力的提升,基于星地网络融合的公众手持终端直连卫星逐步成为现实。…

超融合架构下,虚拟机高可用机制如何构建?

作者:SmartX 产品部 钟锦锌 虚拟机高可用(High Availability,简称 HA)是虚拟化/超融合平台最常用、关键的功能之一,可在服务器发生故障时通过重建业务虚拟机以降低故障对业务带来的影响。因此,为了充分保障…

什么是驾驶舱?这3种驾驶舱领导最爱看

在当今快速变化的商业环境中,企业决策者需要实时、准确地掌握企业运营的各个方面,以便迅速做出明智的决策。数据驾驶舱,作为一种先进的管理工具,正逐渐成为企业管理层的必备利器。本文将深入探讨数据驾驶舱的概念、类型、角色多维…

DAOS: A Scale-Out High Performance Storage Stack for Storage Class Memory——论文泛读

Supercomputing Frontiers 2020 Paper 分布式元数据论文阅读笔记整理 问题 企业、政府和学术界出现的数据密集型应用程序将现有的I/O模型扩展到了极限。现代I/O工作负载的特点是元数据与未对齐和碎片化数据的结合比例越来越高。传统的存储堆栈为这些工作负载提供了较差的性能…

从原理上解决 uniapp (含第三方插件)打包 iOS APP 失败的问题

最近一段时间,我的团队基于uniapp开发的平台型APP因平台资金合规的要求,需要对接中金支付,uniapp的插件市场有一个别人做好的中金支付插件,但前端开发同事在引用这个 插件时,出现了 iOS APP 打包不成功的情况&#xff…

通付盾Web3专题 | SharkTeam:Web3常见钓鱼方式分析与安全防范建议

引言 Web3钓鱼是一种针对Web3用户的常见攻击手段,通过各种方式窃取用户的授权、签名,或诱导用户进行误操作,目的是盗窃用户钱包中的加密资产。 近年来,Web3钓鱼事件不断出现,且发展出钓鱼即服务的黑色产业链&#xf…

python+pymysql对数据库进行增、删、改、查操作

一、概述 接口测试中,应用到数据库操作的场景: 1.校验测试数据 接口发送请求后明确会对数据库中的某个字段进行修改(编辑,更新、删除操作),但,响应结果中无该字段数据时。 例如:删…

QML-1- qml简介及项目创建

文章目录 1. QML 简介2. 项目创建3. 目录结构4. CMakeLists.txt 简单介绍5. 运行demo 1. QML 简介 根据官网介绍,qml 为qt一个模块,使用Qml语言开发应用程序和库提供了一个框架。它定义并实现了语言和引擎基础结构,并提供了一个API&#xff…