Giter VIP home page Giter VIP logo

android_toolkitty's People

Contributors

dizzykitty3 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

hongjiecn

android_toolkitty's Issues

Lucky spinningWheel!

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.*
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApp()
        }
    }
}

@Composable
fun MyApp() {
    val items = listOf("项目1", "项目2", "项目3", "项目4", "项目5") // 你的选项列表
    var selectedItem by remember { mutableStateOf<String?>(null) }
    var showDialog by remember { mutableStateOf(false) }

    SpinningWheel(
        items = items,
        modifier = Modifier
            .fillMaxSize()
            .padding(32.dp),
        onItemSelected = { item ->
            selectedItem = item
            showDialog = true
        }
    )

    if (showDialog) {
        AlertDialog(
            onDismissRequest = { showDialog = false },
            title = { Text(text = "恭喜你!") },
            text = { Text(text = "你选中了:${selectedItem}") },
            confirmButton = {
                Button(onClick = { showDialog = false }) {
                    Text("确定")
                }
            }
        )
    }
}

@Composable
fun SpinningWheel(
    items: List<String>,
    modifier: Modifier = Modifier,
    onItemSelected: (String) -> Unit
) {
    var angle by remember { mutableStateOf(0f) }
    var isSpinning by remember { mutableStateOf(false) }
    val infiniteTransition = rememberInfiniteTransition()
    val angleState = infiniteTransition.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 2000, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        )
    )

    // 点击按钮触发旋转
    Button(onClick = {
        isSpinning = !isSpinning
    }) {
        Text(if (isSpinning) "停止" else "旋转")
    }

    // 转盘画布
    Canvas(modifier = modifier) {
        val wheelSize = size.minDimension
        val center = Offset(size.width / 2, size.height / 2)
        val anglePerItem = 360f / items.size
        rotate(if (isSpinning) angleState.value else angle) {
            items.forEachIndexed { index, item ->
                // 根据索引绘制扇形和文本
                val startAngle = anglePerItem * index
                drawArc(
                    color = if (index % 2 == 0) Color.Green else Color.Blue,
                    startAngle = startAngle,
                    sweepAngle = anglePerItem,
                    useCenter = true,
                    topLeft = Offset(center.x - wheelSize / 2, center.y - wheelSize / 2),
                    size = androidx.compose.ui.geometry.Size(wheelSize, wheelSize)
                )
                // 在这里添加绘制文本的代码
            }
        }
    }

    // 当点击停止按钮时
    LaunchedEffect(isSpinning) {
        if (!isSpinning && angleState.isRunning) {
            infiniteTransition.stop()
            angle = angleState.value % 360
            val selectedIndex = (angle / anglePerItem).toInt() % items.size
            onItemSelected(items[selectedIndex])
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApp()
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.