android Composable引入项目与简单案例源码 kotlin

开发预览

运行效果

build.gradle(:app)

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    id 'kotlin-kapt'
}

android {
    namespace 'com.example.lanidemokt'
    compileSdk 31

    defaultConfig {
        applicationId "com.example.lanidemokt"
        minSdk 24
        targetSdk 30
        versionCode 1
        versionName "1.0"


        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        buildConfigField "Boolean", "DEBUG", "true"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    lintOptions {
        abortOnError false
    }
    buildFeatures {
        viewBinding true
        dataBinding  true
        compose true
    }
   dataBinding {
        enabled = true
    }
    kapt {
        generateStubs = true
    }

dependencies {

   implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2"
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'androidx.appcompat:appcompat-resources:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    testImplementation 'junit:junit:4.12'
    implementation "androidx.viewpager2:viewpager2:1.0.0"
    kapt  "androidx.room:room-compiler:2.4.0"
    api 'com.google.code.gson:gson:2.8.6'
    api 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0'
    
//
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:1.1.1"

    implementation 'androidx.compose.material:material:1.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.0.1"
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.0.1"
    implementation "androidx.compose.ui:ui-tooling:1.0.0-rc01"
    implementation "androidx.compose.ui:ui-tooling-preview:1.0.0-rc01"
    implementation "androidx.compose.runtime:runtime:1.0.1"

}

activity.kt

package com.example.lanidemokt.cmp

import android.os.Bundle
import android.os.Message
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

class FirstComposeActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
//            Text("Hello world!")
            MessageCard(name = "LANI")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewCard(){
MessageCard(name = "ADDDD")
}
@Composable
fun MessageCard(name: String) {
    Text(text = "我是谁...,$name")
}


/*ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeTestTheme {
        Greeting("Android")
    }
}*/

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

build.gradle(项目根目录)

plugins {
    id 'com.android.application' version '7.4.0' apply false
    id 'com.android.library' version '7.4.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}

参考官方文档

Android Compose 教程  |  Jetpack Compose  |  Android Developers 

Kotlin中使用Jetpack Compose创建UI时,可以使用`Image`和`Text`组件来展示图片和文字。如果你想要将图片和文字变成黑白效果,失去颜色,你可以通过调整颜色矩阵(ColorMatrix)来实现。具体步骤如下: 1. 使用`Image`组件加载你想要显示的图片。 2. 利用`ColorMatrix`和`MatrixColorFilter`将图片转换成黑白效果。 3. 对于`Text`组件,同样可以应用`MatrixColorFilter`来实现黑白效果。 下面是一个简化的代码示例来展示这个过程: ```kotlin import android.graphics.ColorMatrix import android.graphics.ColorMatrixColorFilter import androidx.compose.foundation.Image import androidx.compose.foundation.Text import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.DefaultShader import androidx.compose.ui.graphics.drawscope.DrawStyle import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.graphics.painter.BitmapPainter import androidx.compose.ui.res.loadImageBitmap import androidx.compose.ui.res.useResource @Composable fun BlackAndWhiteImage() { val bitmap = useResource("path_to_your_image") { loadImageBitmap(it) } val colorMatrix = ColorMatrix().apply { setSaturation(0f) // 将饱和度设置为0,使图片变为灰度 } Column(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) { Image( painter = BitmapPainter(bitmap), contentDescription = "", colorFilter = ColorFilter.colorMatrix(colorMatrix, DrawStyle.Stroke(Stroke.DefaultMiter)) ) // 如果需要对文字也应用黑白效果,可以类似地使用ColorFilter Text( text = "黑白文字", colorFilter = ColorFilter.colorMatrix(colorMatrix, DefaultShader()) ) } } ``` 请注意,上述代码仅作为示例,实际上在Compose中实现颜色转换可能需要不同的方法,因为上述API可能并不适用于最新的Compose版本。确保查看最新的Compose文档来获取正确的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值