Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Jetpack Compose Canvas入門

Jetpack Compose Canvas入門

rmakiyama

June 23, 2021
Tweet

More Decks by rmakiyama

Other Decks in Technology

Transcript

  1. ԁΛඳ͍ͯΈΔ🙆 class DrawCircleView @JvmOverloads constructor( context: Context, attrs: AttributeSet? =

    null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val paint = Paint().apply { // ͲͷΑ͏ʹඳը͢Δ͔ color = Color.BLUE } override fun onDraw(canvas: Canvas) { val radius = width.toFloat() / 2 // ԿΛඳը͢Δ͔ canvas.drawCircle( (width / 2).toFloat(), // center x (height / 2).toFloat(), // center y radius, // radius paint // paint ) } }
  2. ԁΛඳ͍ͯΈΔ🙆 class DrawCircleView @JvmOverloads constructor( context: Context, attrs: AttributeSet? =

    null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val paint = Paint().apply { // ͲͷΑ͏ʹඳը͢Δ͔ color = Color.BLUE } override fun onDraw(canvas: Canvas) { val radius = width.toFloat() / 2 // ԿΛඳը͢Δ͔ canvas.drawCircle( (width / 2).toFloat(), // center x (height / 2).toFloat(), // center y radius, // radius paint // paint ) } }
  3. ԁΛඳ͍ͯΈΔ🙆 class DrawCircleView @JvmOverloads constructor( context: Context, attrs: AttributeSet? =

    null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val paint = Paint().apply { // ͲͷΑ͏ʹඳը͢Δ͔ color = Color.BLUE } override fun onDraw(canvas: Canvas) { val radius = width.toFloat() / 2 // ԿΛඳը͢Δ͔ canvas.drawCircle( (width / 2).toFloat(), // center x (height / 2).toFloat(), // center y radius, // radius paint // paint ) } }
  4. ԁΛඳ͍ͯΈΔ🙆 class DrawCircleView @JvmOverloads constructor( context: Context, attrs: AttributeSet? =

    null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val paint = Paint().apply { // ͲͷΑ͏ʹඳը͢Δ͔ color = Color.BLUE } override fun onDraw(canvas: Canvas) { val radius = width.toFloat() / 2 // ԿΛඳը͢Δ͔ canvas.drawCircle( (width / 2).toFloat(), // center x (height / 2).toFloat(), // center y radius, // radius paint // paint ) } }
  5. ԁΛඳ͍ͯΈΔ🙆 @Composable fun CircleView() { Canvas(modifier = Modifier.fillMaxSize()) { /

    / this: DrawScope drawCircle(Color.Blue) } } ͨͬͨ͜Ε͚ͩ👏
  6. ͨͱ͑͹ESBX$JSDMFϝιου fun drawCircle( color: Color, radius: Float = size.minDimension /

    2.0f, center: Offset = this.center, /* @FloatRange(from = 0.0, to = 1.0) */ alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode )
  7. ·ͣ͸ͭԁΛඳ͘ @Composable fun DotLoading( modifier: Modifier = Modifier, dotSize: Dp

    = 24.dp, dotColor: Color = MaterialTheme.colors.primary, ) { val dotSpace: Dp = dotSize * 0.5f val canvasSize = dotSize * 2 + dotSpace Canvas(modifier = modifier.size(canvasSize)) { val radius = dotSize.toPx() / 2 repeat(4) { index -> rotate(90f * (index - 1)) { drawCircle( color = dotColor, radius = radius, center = Offset(radius, radius) ) } } } }
  8. Ξχϝʔγϣϯͷ࣮૷ @Composable fun DotLoading(…) { val dotSpace: Dp = dotSize

    * 0.5f val offset = with(LocalDensity.current) { (dotSize + dotSpace).toPx() } val infiniteTransition = rememberInfiniteTransition() val xOffset by infiniteTransition.animateFloat( 0f, offset, infiniteRepeatable( animation = tween(durationMillis = 500) ) ) val canvasSize = dotSize * 2 + dotSpace Canvas(modifier = modifier.size(canvasSize)) { val radius = dotSize.toPx() / 2 repeat(4) { index -> rotate(90f * (index - 1)) { drawCircle( color = dotColor, radius = radius, center = Offset(radius + xOffset, radius) ) } } } }
  9. Ξχϝʔγϣϯͷ࣮૷ @Composable fun DotLoading(…) { val dotSpace: Dp = dotSize

    * 0.5f val offset = with(LocalDensity.current) { (dotSize + dotSpace).toPx() } val infiniteTransition = rememberInfiniteTransition() val xOffset by infiniteTransition.animateFloat( 0f, offset, infiniteRepeatable( animation = tween(durationMillis = 500) ) ) val canvasSize = dotSize * 2 + dotSpace Canvas(modifier = modifier.size(canvasSize)) { val radius = dotSize.toPx() / 2 repeat(4) { index -> rotate(90f * (index - 1)) { drawCircle( color = dotColor, radius = radius, center = Offset(radius + xOffset, radius) ) } } } }