【Jetpack Compose】回転するアニメーションの作り方

これ、おもしろい!!




👉 回転おじさん - Facebook

ちょっと画像をお借りして、

やってみましたが、、、


@Composable
fun Humans() {
  var currentRotation by remember { mutableFloatStateOf(0f) }
  val rotation = remember { Animatable(currentRotation) }

  LaunchedEffect(Unit) {
    rotation.animateTo(
      targetValue = currentRotation + 360f,
      animationSpec = infiniteRepeatable(
        animation = tween(3000, easing = LinearEasing),
        repeatMode = RepeatMode.Restart
      )
    ) {
      currentRotation = rotation.value
    }
  }
  Image(
    modifier = Modifier
      .fillMaxSize()
      .rotate(rotation.value)
      .aspectRatio(1.0f),
    painter = painterResource(R.drawable.humans),
    contentDescription = null
  )
}

ダメでした。。。

なんで、おじさんが走ってくれないのでしょうかー。


Jetpack Compose で Activity を取得する

なんとなくまだ古くさい感じはありますが。


val context = LocalContext.current


fun Context.getActivityOrNull(): Activity? {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }    
    return null
}


fun Context.findActivity(): Activity {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }
    throw IllegalStateException("Permissions should be called in the context of an Activity")
}

👉 android - How to get activity in compose - Stack Overflow


【Jetpack Compose】メールアプリを最も簡単に開く方法

指定したURLでWEBブラウザを開くならこう書けます。


@Composable
fun OpenLinkButton(
  urlHandler: UriHandler = LocalUriHandler.current
) {
  Button(
    onClick = {
      urlHandler.openUri("https://android.benigumo.com/")
    }
  ) {
    Text("Open Web link")
  }
}

では、

指定した宛先メールアドレスでメールアプリを開く場合。


@Composable
fun OpenMailAppButton(
  urlHandler: UriHandler = LocalUriHandler.current
) {
  Button(
    onClick = {
      urlHandler.openUri("mailto:[email protected]")
    }
  ) {
    Text("Open Mail App")
  }
}

旧来の Intent は Jetpack Compose では使わないことが多くなってきました!

👉 顔文字 (かおもじ) パック - Google Play のアプリ