首页 > 其他分享 >Android开发——添加图片

Android开发——添加图片

时间:2023-12-22 22:44:32浏览次数:24  
标签:compose androidx image 添加 ui import Android Modifier 图片

1、首先选择一张需要的图片,通过左侧的Resource Manage选择“+”并选择Import Drawables
file
选择一张图片
file
并调整以下两个内容
file
这两个内容的作用借用谷歌官方的Android开发教程的内容:
*Android 设备具有不同的屏幕尺寸(手机、平板电脑和电视等),而且这些屏幕也具有不同的像素尺寸。也就是说,有可能一部设备的屏幕为每平方英寸 160 个像素,而另一部设备的屏幕在相同的空间内可以容纳 480 个像素。如果不考虑像素密度的这些变化,系统可能会按比例缩放图片,这可能会导致图片模糊或占用大量内存空间,或者图片大小不当。

如果所调整的图片超出了 Android 系统可处理的图片大小,系统会抛出内存不足错误。对于照片和背景图片(如当前图片 androidparty.png),应将其放在 drawable-nodpi 文件夹中,这样会停止调整大小行为*

2、在项目中,所有资源都保存在/res目录下,具有以下例如的结构:
MyProject/
src/
MyActivity.kt
res/
drawable/
graphic.png
mipmap/
icon.png
values/
strings.xml

在项目中,R 类是 Android 自动生成的类,其中包含了项目中所有资源的 ID。所以我们可以使用R.drawable.picture_name的方式找到图片资源。
接下来构建组合函数GreetingImage()首先在函数中定义变量image它所对应的就是我们所需要的图片

val image = painterResource(R.drawable.androidparty)

接着构建组合内容image

Image(
            painter = image,
            contentDescription = null,
            contentScale = ContentScale.Crop,
            alpha = 0.6F
        )

这里的实参contentDescriptionTalk Back内容有关,这是一个为了使更多用户使用我们的程序所有的一些提示,这里我们并不需要所以设置为null
ContentScale中提供各种各样的参数来调整图片大小比例
alpha规定了图片透明度
最后使用Box布局并添加上之前的"GreetingText"函数

3、最终效果

file
file

完整代码

package com.example.happybirthday

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.happybirthday.ui.theme.HappyBirthdayTheme


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            HappyBirthdayTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    GreetingImage("Happy Birthday,Human")
                }
            }
        }
    }
}
@Composable
fun GreetingText(message: String, modifier: Modifier = Modifier) {
    Text(
        text = message,
        fontSize = 100.sp,
        lineHeight = 113.sp)
}

@Composable
fun GreetingImage(message: String, modifier: Modifier = Modifier) {
    val image = painterResource(R.drawable.androidparty)
    Box {
        Image(
            painter = image,
            contentDescription = null,
            contentScale = ContentScale.Crop,
            alpha = 0.5F
        )
        GreetingText(
            message = message,
            modifier = Modifier
                .fillMaxSize()
                .padding(8.dp)
        )
    }
}

@Preview(showBackground = false)
@Composable
fun GreetingPreview() {
    HappyBirthdayTheme {
        GreetingImage("Happy Birthday,Human")
    }
}

本文由博客一文多发平台 OpenWrite 发布!

标签:compose,androidx,image,添加,ui,import,Android,Modifier,图片
From: https://www.cnblogs.com/humanplug/p/17922496.html

相关文章

  • 关于Android图像Bitmap类你要知道的一切
    Bitmap介绍Bitmap是一种图像文件格式,它由像素阵列组成,每个像素都有自己的颜色信息。在计算机图形学中,Bitmap图像可以被描述为一个二维的矩阵,其中每个元素代表一个像素的颜色值。Android中的Bitmap是用来表示图像的类,它可以用来加载、显示和处理图像。你可以通过Bitmap类来创建一个......
  • Android新手程序员提升技术最快的3个方法,你知道吗?
    前言对于刚刚进入职场的1-3年的程序员来说,首要任务无疑是全身心地投入到技术开发工作中,用最专业的技术知识和熟练度来开展工作。这项任务需要你花费大量的时间和精力去学习、探索和实践。只有充分掌握了当前技术的使用方法和功能,以及行业内的趋势和动态,你才能够在这个领域中不断进......
  • Android平台RTSP流如何添加动态水印后转推RTMP或轻量级RTSP服务
    技术背景我们在对接外部开发者的时候,遇到这样的技术诉求,客户用于地下管道检测场景,需要把摄像头的数据拉取过来,然后叠加上实时位置、施工单位、施工人员等信息,然后对外输出新的RTSP流,并本地录制一份带动态水印叠加后的数据。整个过程,因为摄像头位置一直在变化,所以需要整体尽可能的低......
  • pandas to_excel 添加一个新的工作表到存在的excel文件中
    EverytimeyouwanttosaveaPandasDataFrametoanExcel,youmaycallthisfunction:importosdefsave_excel_sheet(df,filepath,sheetname,index=False):#Createfileifitdoesnotexistifnotos.path.exists(filepath):df.to_excel(......
  • android添加c语言的可执行程序
    在android源码的external目录下添加test目录。在test目录下新建test.c文件和Android.mk文件。test.c文件例如:#include<stdio.h>intmain(){printf("helloworld\n");return0;}Android.mk文件:LOCAL_PATH:=$(callmy-dir)include$(CLEAR_VARS)LO......
  • jQuery功能强大的图片查看器插件 viewer
    http://www.htmleaf.com/jQuery/Image-Effects/201509032517.html 如果想知道用户点击的是第几张图片,可使用HTML5的window.postMessage实现通信,修改viewer.js: 然后在html中接收事件: ......
  • python批量给文本文件txt内容添加特殊符号等内容:-----------------------------------
    python脚本内容如下:#导入需要使用的模块importosimporttkinter.filedialogfn=tkinter.filedialog.askopenfilename(title='选择了一个文件',filetypes=[('文本文件','.txt'),('所有文件','.*')])print("打开了文件:"+fn)line_count=0f=open......
  • Android应用开发长按拖拽-Flutter的LongPressDraggable控件回调函数onDraggableCancel
    onDraggableCanceled介绍LongPressDraggable的onDraggableCanceled回调在拖动被取消时触发。拖动可能会被取消,例如用户在拖动开始后移动了太快或在放置之前取消了拖动。onDraggableCanceled的使用以下是如何使用onDraggableCanceled的示例:LongPressDraggable<int>(//......
  • Android创建引导时的镂空View漏空view
     importandroid.graphics.RectFimportandroid.view.Viewimportandroid.view.ViewGroupobjectMyGuideUtil{/***@baseView在哪个view基础上进行镂空*/funguideMain(baseView:View){vallocation=IntArray(2)baseView.ge......
  • 图片镜像程序(输入父目录与阈值)
    importosfromPILimportImagedefmirror_images_in_subfolders(parent_directory,x_threshold):"""遍历指定目录,找到包含图片数量少于指定阈值的子文件夹,并镜像这些图片,同时保存新名称以避免重复。:paramparent_directory:要搜索的根目录。:pa......