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 
17 package com.android.DeviceAsWebcam.view;
18 
19 import static com.android.DeviceAsWebcam.view.ZoomController.convertZoomRatioToString;
20 
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.util.AttributeSet;
24 import android.view.Gravity;
25 import android.widget.FrameLayout;
26 import android.widget.FrameLayout.LayoutParams;
27 import android.widget.SeekBar;
28 
29 import androidx.appcompat.widget.AppCompatTextView;
30 
31 import com.android.DeviceAsWebcam.R;
32 
33 /** A knob that aims to provide the sliding gesture control on the SeekBar. */
34 public class ZoomKnob extends AppCompatTextView {
35     private final Resources mResources;
36     /**
37      * The max zoom progress value.
38      */
39     private int mMaxZoomProgress;
40     /**
41      * The SeekBar this Knob is attached to.
42      */
43     private SeekBar mSeekBar;
44 
ZoomKnob(Context context, AttributeSet attrs)45     public ZoomKnob(Context context, AttributeSet attrs) {
46         super(context, attrs);
47         mResources = context.getResources();
48     }
49 
initialize(SeekBar zoomSeekBar, int maxZoomProgress)50     void initialize(SeekBar zoomSeekBar, int maxZoomProgress) {
51         mSeekBar = zoomSeekBar;
52         mMaxZoomProgress = maxZoomProgress;
53         float textSizePx = mResources.getDimensionPixelSize(R.dimen.zoom_knob_text_size);
54         float textSizeSp = textSizePx / mResources.getDisplayMetrics().scaledDensity;
55 
56         setElevation(mResources.getDimensionPixelSize(R.dimen.zoom_thumb_elevation));
57         setGravity(Gravity.CENTER);
58         setTextAlignment(TEXT_ALIGNMENT_CENTER);
59         setTextSize(textSizeSp);
60 
61         mSeekBar.setSplitTrack(false);
62     }
63 
64     /**
65      * Update the zoom ratio text which shows on the knob.
66      *
67      * @param zoomProgress the progress on the SeekBar.
68      * @param zoomRatio    the zoom ratio.
69      */
updateZoomProgress(int zoomProgress, float zoomRatio)70     public void updateZoomProgress(int zoomProgress, float zoomRatio) {
71         int maxMargin = mSeekBar.getWidth() - getWidth();
72         FrameLayout.LayoutParams lp = (LayoutParams) getLayoutParams();
73         int margin = (int) (((float) zoomProgress / mMaxZoomProgress) * maxMargin);
74         lp.leftMargin = margin;
75         lp.rightMargin = 0;
76         setLayoutParams(lp);
77         setText(convertZoomRatioToString(zoomRatio));
78     }
79 
80     /** Elevate the knob above SeekBar. */
setElevated(boolean elevated)81     public void setElevated(boolean elevated) {
82         FrameLayout.LayoutParams lp = (LayoutParams) getLayoutParams();
83         if (elevated) {
84             lp.bottomMargin = mResources.getDimensionPixelSize(R.dimen.zoom_knob_lift)
85                     + (mResources.getDimensionPixelSize(R.dimen.zoom_icon_size) / 2);
86         } else {
87             lp.bottomMargin = 0;
88         }
89         setLayoutParams(lp);
90     }
91 }
92 
93