|
| 1 | +/* |
| 2 | + * Copyright 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 io.material.catalog.progressindicator; |
| 17 | + |
| 18 | +import io.material.catalog.R; |
| 19 | + |
| 20 | +import android.view.View; |
| 21 | +import androidx.annotation.LayoutRes; |
| 22 | +import androidx.annotation.NonNull; |
| 23 | +import com.google.android.material.materialswitch.MaterialSwitch; |
| 24 | +import com.google.android.material.progressindicator.CircularProgressIndicator; |
| 25 | +import com.google.android.material.progressindicator.LinearProgressIndicator; |
| 26 | +import com.google.android.material.slider.Slider; |
| 27 | + |
| 28 | +/** |
| 29 | + * This is the fragment to demo wave effects in {@link LinearProgressIndicator} and {@link |
| 30 | + * CircularProgressIndicator}. |
| 31 | + */ |
| 32 | +public class ProgressIndicatorWaveDemoFragment extends ProgressIndicatorDemoFragment { |
| 33 | + |
| 34 | + @NonNull private LinearProgressIndicator linearIndicator; |
| 35 | + @NonNull private CircularProgressIndicator circularIndicator; |
| 36 | + |
| 37 | + @Override |
| 38 | + public void initDemoContents(@NonNull View view) { |
| 39 | + linearIndicator = view.findViewById(R.id.linear_indicator); |
| 40 | + circularIndicator = view.findViewById(R.id.circular_indicator); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void initDemoControls(@NonNull View view) { |
| 45 | + Slider progressSlider = view.findViewById(R.id.progress_slider); |
| 46 | + MaterialSwitch determinateSwitch = view.findViewById(R.id.determinate_mode_switch); |
| 47 | + |
| 48 | + progressSlider.addOnChangeListener( |
| 49 | + (slider, value, fromUser) -> { |
| 50 | + if (!linearIndicator.isIndeterminate()) { |
| 51 | + linearIndicator.setProgressCompat((int) value, true); |
| 52 | + } |
| 53 | + if (!circularIndicator.isIndeterminate()) { |
| 54 | + circularIndicator.setProgressCompat((int) value, true); |
| 55 | + } |
| 56 | + }); |
| 57 | + determinateSwitch.setOnCheckedChangeListener( |
| 58 | + (v, isChecked) -> { |
| 59 | + if (isChecked) { |
| 60 | + float progress = progressSlider.getValue(); |
| 61 | + linearIndicator.setProgressCompat((int) progress, true); |
| 62 | + circularIndicator.setProgressCompat((int) progress, true); |
| 63 | + } else { |
| 64 | + linearIndicator.setProgressCompat(0, false); |
| 65 | + circularIndicator.setProgressCompat(0, false); |
| 66 | + linearIndicator.setIndeterminate(true); |
| 67 | + circularIndicator.setIndeterminate(true); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + float pixelsInDp = view.getResources().getDisplayMetrics().density; |
| 72 | + |
| 73 | + Slider amplitudeSlider = view.findViewById(R.id.amplitude_slider); |
| 74 | + amplitudeSlider.addOnChangeListener( |
| 75 | + (slider, value, fromUser) -> { |
| 76 | + int newAmplitude = (int) (value * pixelsInDp); |
| 77 | + if (linearIndicator.getAmplitude() != newAmplitude) { |
| 78 | + linearIndicator.setAmplitude(newAmplitude); |
| 79 | + } |
| 80 | + if (circularIndicator.getAmplitude() != newAmplitude) { |
| 81 | + circularIndicator.setAmplitude((int) (value * pixelsInDp)); |
| 82 | + } |
| 83 | + }); |
| 84 | + Slider waveLengthSlider = view.findViewById(R.id.wavelength_slider); |
| 85 | + waveLengthSlider.addOnChangeListener( |
| 86 | + (slider, value, fromUser) -> { |
| 87 | + int newWaveLength = (int) (value * pixelsInDp); |
| 88 | + if (linearIndicator.getWavelength() != newWaveLength) { |
| 89 | + linearIndicator.setWavelength(newWaveLength); |
| 90 | + } |
| 91 | + if (circularIndicator.getWavelength() != newWaveLength) { |
| 92 | + circularIndicator.setWavelength(newWaveLength); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + Slider circularSizeSlider = view.findViewById(R.id.circularSizeSlider); |
| 97 | + circularSizeSlider.addOnChangeListener( |
| 98 | + (slider, value, fromUser) -> { |
| 99 | + int newCornerRadius = (int) (value * pixelsInDp); |
| 100 | + if (circularIndicator.getIndicatorSize() != newCornerRadius) { |
| 101 | + circularIndicator.setIndicatorSize(newCornerRadius); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + @LayoutRes |
| 108 | + public int getProgressIndicatorContentLayout() { |
| 109 | + return R.layout.cat_progress_indicator_main_content; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + @LayoutRes |
| 114 | + public int getProgressIndicatorDemoControlLayout() { |
| 115 | + return R.layout.cat_progress_indicator_wave_controls; |
| 116 | + } |
| 117 | +} |
0 commit comments