Mobile Apps 2023W Lecture 4: Difference between revisions
|  Created page with "==Resources==  * [https://developer.android.com/jetpack/compose/graphics/images/loading Loading images in Compose] * [https://developer.android.com/jetpack/compose/gestures#dragging Drag gestures in compose]" | No edit summary | ||
| (One intermediate revision by the same user not shown) | |||
| Line 3: | Line 3: | ||
| * [https://developer.android.com/jetpack/compose/graphics/images/loading Loading images in Compose] | * [https://developer.android.com/jetpack/compose/graphics/images/loading Loading images in Compose] | ||
| * [https://developer.android.com/jetpack/compose/gestures#dragging Drag gestures in compose] | * [https://developer.android.com/jetpack/compose/gestures#dragging Drag gestures in compose] | ||
| * [https://homeostasis.scs.carleton.ca/~soma/ma-2023w/images/happy.jpg Happy pop-up dog image] | |||
| ==Code== | |||
| <syntaxhighlight lang="kotlin" line> | |||
| package carleton.comp2601.jan20 | |||
| import android.os.Bundle | |||
| import androidx.activity.ComponentActivity | |||
| import androidx.activity.compose.setContent | |||
| import androidx.compose.foundation.Image | |||
| import androidx.compose.foundation.background | |||
| import androidx.compose.foundation.gestures.detectDragGestures | |||
| import androidx.compose.foundation.layout.* | |||
| import androidx.compose.material.MaterialTheme | |||
| import androidx.compose.material.Surface | |||
| import androidx.compose.material.Text | |||
| import androidx.compose.runtime.* | |||
| import androidx.compose.ui.Modifier | |||
| import androidx.compose.ui.graphics.Color | |||
| import androidx.compose.ui.input.pointer.consumeAllChanges | |||
| import androidx.compose.ui.input.pointer.pointerInput | |||
| import androidx.compose.ui.res.painterResource | |||
| import androidx.compose.ui.tooling.preview.Preview | |||
| import androidx.compose.ui.unit.IntOffset | |||
| import androidx.compose.ui.unit.dp | |||
| import carleton.comp2601.jan20.ui.theme.Jan20Theme | |||
| import kotlin.math.roundToInt | |||
| class MainActivity : ComponentActivity() { | |||
|     override fun onCreate(savedInstanceState: Bundle?) { | |||
|         super.onCreate(savedInstanceState) | |||
|         setContent { | |||
|             Jan20Theme { | |||
|                 // A surface container using the 'background' color from the theme | |||
|                 Surface( | |||
|                     modifier = Modifier.fillMaxSize(), | |||
|                     color = MaterialTheme.colors.background | |||
|                 ) { | |||
|                     MainScreen() | |||
|                 } | |||
|             } | |||
|         } | |||
|     } | |||
| } | |||
| @Composable | |||
| fun MainScreen() { | |||
|     Column { | |||
|         draggableImage() | |||
|         draggableBox() | |||
|     } | |||
| } | |||
| @Composable | |||
| fun draggableImage() { | |||
|     var x by remember { mutableStateOf(0f)} | |||
|     var y by remember { mutableStateOf(0f)} | |||
|     Image ( | |||
|         painter = painterResource(id = R.drawable.happy), | |||
|         contentDescription = "Happy Dog", | |||
|         Modifier | |||
|                 .offset { IntOffset(x.roundToInt(), y.roundToInt()) } | |||
|                 .pointerInput(Unit) { | |||
|                     detectDragGestures { change, dragAmount -> | |||
|                         change.consumeAllChanges() | |||
|                         x += dragAmount.x | |||
|                         y += dragAmount.y | |||
|                     } | |||
|                 } | |||
|     ) | |||
| } | |||
| @Composable | |||
| fun draggableBox() { | |||
|     Box(modifier = Modifier.fillMaxSize()) { | |||
|         var offsetX by remember { mutableStateOf(0f) } | |||
|         var offsetY by remember { mutableStateOf(0f) } | |||
|         Box( | |||
|             Modifier | |||
|                 .offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) } | |||
|                 .background(Color.Blue) | |||
|                 .size(50.dp) | |||
|                 .pointerInput(Unit) { | |||
|                     detectDragGestures { change, dragAmount -> | |||
|                         change.consumeAllChanges() | |||
|                         offsetX += dragAmount.x | |||
|                         offsetY += dragAmount.y | |||
|                     } | |||
|                 } | |||
|         ) | |||
|     } | |||
| } | |||
| @Preview(showBackground = true) | |||
| @Composable | |||
| fun DefaultPreview() { | |||
|     Jan20Theme { | |||
|         MainScreen() | |||
|     } | |||
| } | |||
| </syntaxhighlight> | |||
Latest revision as of 21:09, 20 January 2023
Resources
Code
package carleton.comp2601.jan20
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import carleton.comp2601.jan20.ui.theme.Jan20Theme
import kotlin.math.roundToInt
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Jan20Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    MainScreen()
                }
            }
        }
    }
}
@Composable
fun MainScreen() {
    Column {
        draggableImage()
        draggableBox()
    }
}
@Composable
fun draggableImage() {
    var x by remember { mutableStateOf(0f)}
    var y by remember { mutableStateOf(0f)}
    Image (
        painter = painterResource(id = R.drawable.happy),
        contentDescription = "Happy Dog",
        Modifier
                .offset { IntOffset(x.roundToInt(), y.roundToInt()) }
                .pointerInput(Unit) {
                    detectDragGestures { change, dragAmount ->
                        change.consumeAllChanges()
                        x += dragAmount.x
                        y += dragAmount.y
                    }
                }
    )
}
@Composable
fun draggableBox() {
    Box(modifier = Modifier.fillMaxSize()) {
        var offsetX by remember { mutableStateOf(0f) }
        var offsetY by remember { mutableStateOf(0f) }
        Box(
            Modifier
                .offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
                .background(Color.Blue)
                .size(50.dp)
                .pointerInput(Unit) {
                    detectDragGestures { change, dragAmount ->
                        change.consumeAllChanges()
                        offsetX += dragAmount.x
                        offsetY += dragAmount.y
                    }
                }
        )
    }
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    Jan20Theme {
        MainScreen()
    }
}