dokar3 / pinch-zoom-grid

Compose pinch-to-zoom wrapper for LazyVerticalGrid and LazyHorizontalGrid.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pinch Zoom Grid

Maven Central GitHub Workflow Status (with event)

A wrapper for LazyVerticalGrid() and LazyHorizontalGrid() to switch between multiple grid cells using pinch gestures and animations.

Demo

Vertical Horizontal Invoke
vertical.1.mp4
horizontal.1.mp4
invoke.1.mp4

Usages

Dependency

implementation("io.github.dokar3:pinchzoomgrid:LATEST_VERSION")

Basic

val cellsList = remember {
    listOf(
        GridCells.Fixed(4), // ↑ Zoom out
        GridCells.Fixed(3), // |
        GridCells.Fixed(2), // ↓ Zoom in
    )
}
val state = rememberPinchZoomGridState(
    cellsList = cellsList,
    initialCellsIndex = 1,
)

PinchZoomGridLayout(state = state) { // PinchZoomGridScope
    LazyVerticalGrid(
        // Use gridCells from the PinchZoomGridScope
        columns = gridCells,
        // Use gridState from the PinchZoomGridScope
        state = gridState,
        // Use fillMaxSize() to avoid transition clipping
        modifier = Modifier.fillMaxSize(),
    ) {
        item(span = { GridItemSpan(maxLineSpan) }) {
            PhotoHeader(
                text = "Today",
                // Apply to layouts that require transitions
                modifier = Modifier.pinchItem(key = "header")
            )
        }

        items(count = 10) { index ->
            ImageItem(
                index = it,
                // Make sure keys are unique
                modifier = Modifier.pinchItem(key = index),
            )
        }
    }
}

Item transitions

It automatically works with non-fixed size layouts, for example:

AsyncImage(
    model = url,
    contentDescription = null,
    modifier = Modifier
        .pinchItem(key = index)
        .fillMaxWidth()
        .aspectRatio(1f)
        .background(placeholder),
)

If you have some layouts have fixed size in the item, such as Text(), scale transitions need to be disabled:

 Column(modifier = modifier) {
    AsyncImage(
        model = url,
        contentDescription = null,
        modifier = Modifier
            // Enable all transitions
            .pinchItem(key = index)
            .fillMaxWidth()
            .aspectRatio(1f)
            .background(placeholder),
    )
    Text(
        text = "$index",
        modifier = Modifier.pinchItem(
            // Use a different key
            key = "$index-text",
            // Enable only the translate transition
            transitions = PinchItemTransitions.Translate,
        )
    )
}

Offscreen item transitions

It's also possible to customize offscreen item transitions. The default value is Alpha + Translate.

AsyncImage(
    model = url,
    contentDescription = null,
    modifier = Modifier
        .pinchItem(key = index, offscreenTransitions = PinchItemTransitions.Translate)
        .fillMaxWidth()
        .aspectRatio(1f)
        .background(placeholder),
)

License

Copyright 2023 dokar3

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Compose pinch-to-zoom wrapper for LazyVerticalGrid and LazyHorizontalGrid.

License:Apache License 2.0


Languages

Language:Kotlin 100.0%