1 /* 2 * Copyright (C) 2023 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.preview.ui.view 17 18 import android.content.Context 19 import android.util.AttributeSet 20 import com.android.wallpaper.util.WallpaperCropUtils 21 import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView 22 23 /** 24 * A [SubsamplingScaleImageView] for wallpaper preview that scales and centers the surface to 25 * simulate the actual wallpaper surface's default system zoom. 26 */ 27 class SystemScaledSubsamplingScaleImageView(context: Context, attrs: AttributeSet? = null) : 28 SubsamplingScaleImageView(context, attrs) { onMeasurenull29 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 30 super.onMeasure(widthMeasureSpec, heightMeasureSpec) 31 32 val scale = WallpaperCropUtils.getSystemWallpaperMaximumScale(context) 33 setMeasuredDimension((measuredWidth * scale).toInt(), (measuredHeight * scale).toInt()) 34 } 35 onLayoutnull36 override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { 37 // Calculate the size of wallpaper surface based on the system zoom 38 // and scale & center the wallpaper preview to respect the zoom. 39 val scale = WallpaperCropUtils.getSystemWallpaperMaximumScale(context) 40 41 val scaledWidth = (measuredWidth * scale).toInt() 42 val scaledHeight = (measuredHeight * scale).toInt() 43 val xCentered = (measuredWidth - scaledWidth) / 2 44 val yCentered = (measuredHeight - scaledHeight) / 2 45 46 x = xCentered.toFloat() 47 y = yCentered.toFloat() 48 } 49 } 50