1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.wallpaper.picker.customization.ui.view.transformer
17 
18 import android.graphics.Point
19 import android.view.View
20 import androidx.viewpager2.widget.ViewPager2
21 import com.android.wallpaper.R
22 import com.android.wallpaper.util.RtlUtils
23 import kotlin.math.abs
24 
25 /**
26  * This class implements the translations on a view pagers adjacent pages (adjacent to the currently
27  * focused page) to make the page peek out from the end of the screen.
28  */
29 class PreviewPagerPageTransformer(private val screenSizePx: Point) : ViewPager2.PageTransformer {
transformPagenull30     override fun transformPage(page: View, position: Float) {
31         val cardPreview = page.requireViewById<View>(R.id.preview_card)
32 
33         val nextItemVisibleOffsetPx =
34             page.resources.getDimension(R.dimen.wallpaper_control_button_group_divider_space)
35 
36         // device width in pixels minus the page width will give margin
37         val availableMargin = screenSizePx.x - cardPreview.width - nextItemVisibleOffsetPx
38         page.translationX =
39             if (RtlUtils.isRtl(page.context)) {
40                 availableMargin * position
41             } else {
42                 -availableMargin * position
43             }
44 
45         page.alpha = 0.25f + (1 - abs(position))
46     }
47 }
48