[TimePicker] Changed clock face size to be dynamic

PiperOrigin-RevId: 352024053
This commit is contained in:
marianomartin 2021-01-15 12:27:35 -05:00 committed by ikim24
parent 655caeddba
commit bf0d11b4e7
10 changed files with 42 additions and 32 deletions

View File

@ -38,6 +38,7 @@ import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionIn
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionItemInfoCompat;
import androidx.appcompat.content.res.AppCompatResources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
@ -74,6 +75,9 @@ class ClockFaceView extends RadialViewGroup implements OnRotateListener {
private final int[] gradientColors;
private final float[] gradientPositions = new float[] {0f, 0.9f, 1f};
private final int clockHandPadding;
private final int minimumHeight;
private final int minimumWidth;
private final int clockSize;
private String[] values;
@ -167,6 +171,10 @@ class ClockFaceView extends RadialViewGroup implements OnRotateListener {
String[] initialValues = new String[INITIAL_CAPACITY];
Arrays.fill(initialValues, VALUE_PLACEHOLDER);
setValues(initialValues, /* contentDescription= */ 0);
minimumHeight = res.getDimensionPixelSize(R.dimen.material_time_picker_minimum_screen_height);
minimumWidth = res.getDimensionPixelSize(R.dimen.material_time_picker_minimum_screen_width);
clockSize = res.getDimensionPixelSize(R.dimen.material_clock_size);
}
/**
@ -278,4 +286,25 @@ class ClockFaceView extends RadialViewGroup implements OnRotateListener {
findIntersectingTextView();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Resources r = getResources();
DisplayMetrics displayMetrics = r.getDisplayMetrics();
float height = displayMetrics.heightPixels;
float width = displayMetrics.widthPixels;
// If the screen is smaller than our defined values. Scale the clock face
// proportionally to the smaller size
int size = (int) (clockSize / max3(minimumHeight / height, minimumWidth / width, 1f));
int spec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
setMeasuredDimension(size, size);
super.onMeasure(spec, spec);
}
private static float max3(float a, float b, float c) {
return max(max(a, b), c);
}
}

View File

@ -52,8 +52,8 @@
<com.google.android.material.timepicker.ClockFaceView
android:id="@+id/material_clock_face"
android:layout_width="@dimen/material_clock_size"
android:layout_height="@dimen/material_clock_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/clock_face_margin_start"
app:layout_constraintStart_toEndOf="@+id/material_clock_display"
app:layout_constraintTop_toTopOf="parent" />

View File

@ -30,8 +30,8 @@
<com.google.android.material.timepicker.ClockFaceView
android:id="@+id/material_clock_face"
android:layout_width="@dimen/material_clock_size"
android:layout_height="@dimen/material_clock_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:layout_gravity="center"
app:layout_constraintStart_toStartOf="parent"

View File

@ -16,6 +16,5 @@
-->
<resources>
<dimen name="material_clock_size">210dp</dimen>
<dimen name="material_timepicker_dialog_buttons_margin_top">8dp</dimen>
</resources>

View File

@ -16,6 +16,5 @@
-->
<resources>
<dimen name="material_clock_size">230dp</dimen>
<dimen name="material_timepicker_dialog_buttons_margin_top">24dp</dimen>
</resources>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2020 The Android Open Source Project
Copyright 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -14,7 +14,9 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<dimen name="material_clock_size">256dp</dimen>
<dimen name="material_time_picker_minimum_screen_height">450dp</dimen>
<dimen name="material_time_picker_minimum_screen_width">600dp</dimen>
</resources>

View File

@ -16,7 +16,6 @@
-->
<resources>
<dimen name="material_clock_size">170dp</dimen>
<dimen name="clock_face_margin_start">24dp</dimen>
<dimen name="material_clock_face_margin_top">36dp</dimen>
</resources>

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2020 The Android Open Source Project
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.
-->
<resources>
<dimen name="material_clock_size">200dp</dimen>
</resources>

View File

@ -16,6 +16,5 @@
-->
<resources>
<dimen name="material_clock_size">240dp</dimen>
<dimen name="clock_face_margin_start">64dp</dimen>
</resources>

View File

@ -21,7 +21,6 @@
<dimen name="material_clock_period_toggle_width">52dp</dimen>
<dimen name="material_clock_period_toggle_margin_left">12dp</dimen>
<dimen name="material_clock_face_margin_top">44dp</dimen>
<dimen name="material_clock_size">256dp</dimen>
<dimen name="clock_face_margin_start">64dp</dimen>
<dimen name="material_timepicker_dialog_buttons_margin_top">24dp</dimen>
<dimen name="material_clock_hand_stroke_width">2dp</dimen>
@ -35,4 +34,8 @@
<dimen name="material_cursor_inset_top">-12dp</dimen>
<dimen name="material_cursor_width">2dp</dimen>
<dimen name="material_clock_size">256dp</dimen>
<dimen name="material_time_picker_minimum_screen_height">560dp</dimen>
<dimen name="material_time_picker_minimum_screen_width">340dp</dimen>
</resources>