1 package com.google.appinventor.components.runtime;
4 import android.content.res.ColorStateList;
5 import android.graphics.PorterDuff;
6 import android.graphics.PorterDuff.Mode;
7 import android.graphics.drawable.Drawable;
8 import android.graphics.drawable.LayerDrawable;
9 import android.graphics.drawable.StateListDrawable;
10 import android.os.Build.VERSION;
11 import android.os.Build.VERSION_CODES;
12 import android.util.Log;
13 import android.view.MotionEvent;
14 import android.view.View;
15 import android.widget.SeekBar;
51 @DesignerComponent(version = YaVersion.SLIDER_COMPONENT_VERSION,
52 description =
"A Slider is a progress bar that adds a draggable thumb. You can touch " +
53 "the thumb and drag left or right to set the slider thumb position. " +
54 "As the Slider thumb is dragged, it will trigger the PositionChanged event, " +
55 "reporting the position of the Slider thumb. The reported position of the " +
56 "Slider thumb can be used to dynamically update another component " +
57 "attribute, such as the font size of a TextBox or the radius of a Ball.",
58 category = ComponentCategory.USERINTERFACE)
61 private final static String LOG_TAG =
"Slider";
62 private static final boolean DEBUG =
false;
64 private final SeekBar seekbar;
67 private float minValue;
68 private float maxValue;
70 private float thumbPosition;
71 private boolean thumbEnabled;
74 private int rightColor;
75 private int leftColor;
91 private class SeekBarHelper {
92 public void getThumb(
int alpha) {
93 seekbar.getThumb().mutate().setAlpha(alpha);
109 seekbar.setSplitTrack(
false);
112 leftColor = initialLeftColor;
113 rightColor = initialRightColor;
125 seekbar.setOnSeekBarChangeListener(
this);
135 setSeekbarPosition();
138 Log.d(LOG_TAG,
"Slider initial min, max, thumb values are: " +
150 private void setSliderColors() {
151 if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
152 seekbar.setProgressTintList(ColorStateList.valueOf(leftColor));
153 if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1 ||
154 !(seekbar.getProgressDrawable() instanceof StateListDrawable)) {
155 seekbar.setProgressBackgroundTintList(ColorStateList.valueOf(rightColor));
156 seekbar.setProgressBackgroundTintMode(Mode.MULTIPLY);
163 StateListDrawable drawable = (StateListDrawable) seekbar.getProgressDrawable();
164 if (drawable.getCurrent() instanceof LayerDrawable) {
165 LayerDrawable layerDrawable = (LayerDrawable) drawable.getCurrent();
166 Drawable background = layerDrawable.findDrawableByLayerId(R.id.background);
167 background.setTintList(ColorStateList.valueOf(rightColor));
168 background.setTintMode(Mode.MULTIPLY);
172 LayerDrawable fullBar = (LayerDrawable) seekbar.getProgressDrawable();
173 fullBar.setColorFilter(rightColor,PorterDuff.Mode.SRC);
174 fullBar.findDrawableByLayerId(R.id.progress).setColorFilter(leftColor, PorterDuff.Mode.SRC);
181 private void setSeekbarPosition() {
182 float seekbarPosition = ((thumbPosition - minValue) / (maxValue - minValue)) * 100;
185 Log.d(LOG_TAG,
"Trying to recalculate seekbar position "
186 + minValue +
"/" + maxValue +
"/" + thumbPosition +
"/" + seekbarPosition);
190 seekbar.setProgress((
int) seekbarPosition);
198 @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN,
199 defaultValue =
"True")
200 @SimpleProperty(description =
"Sets whether or not to display the slider thumb.",
203 thumbEnabled = enabled;
204 int alpha = thumbEnabled ? 255 : 0;
206 new SeekBarHelper().getThumb(alpha);
210 seekbar.setOnTouchListener(
new View.OnTouchListener() {
212 public boolean onTouch(View view, MotionEvent motionEvent) {
213 return !thumbEnabled;
224 description =
"Returns whether or not the slider thumb is being be shown",
241 @
SimpleProperty(description =
"Sets the position of the slider thumb. " +
242 "If this value is greater than MaxValue, then it will be set to same value as MaxValue. " +
243 "If this value is less than MinValue, then it will be set to same value as MinValue.",
247 thumbPosition = Math.max(Math.min(position, maxValue), minValue);
249 Log.d(LOG_TAG,
"ThumbPosition is set to: " + thumbPosition);}
250 setSeekbarPosition();
261 description =
"Returns the position of slider thumb", userVisible =
true)
263 return thumbPosition;
276 @
SimpleProperty(description =
"Sets the minimum value of slider. Changing the minimum value " +
277 "also resets Thumbposition to be halfway between the (new) minimum and the maximum. " +
278 "If the new minimum is greater than the current maximum, then minimum and maximum will " +
279 "both be set to this value. Setting MinValue resets the thumb position to halfway " +
280 "between MinValue and MaxValue and signals the PositionChanged event.",
285 maxValue = Math.max(value, maxValue);
288 Log.d(LOG_TAG,
"Min value is set to: " + value);
301 description =
"Returns the value of slider min value.", userVisible =
true)
314 @
SimpleProperty(description =
"Sets the maximum value of slider. Changing the maximum value " +
315 "also resets Thumbposition to be halfway between the minimum and the (new) maximum. " +
316 "If the new maximum is less than the current minimum, then minimum and maximum will both " +
317 "be set to this value. Setting MaxValue resets the thumb position to halfway " +
318 "between MinValue and MaxValue and signals the PositionChanged event.",
322 minValue = Math.min(value, minValue);
325 Log.d (LOG_TAG,
"Max value is set to: " + value);
337 description =
"Returns the slider max value.", userVisible =
true)
352 description =
"The color of slider to the left of the thumb.",
368 defaultValue = initialLeftColorString)
384 description =
"The color of slider to the right of the thumb.",
400 defaultValue = initialRightColorString)
418 thumbPosition = ((maxValue - minValue) * (
float) progress / 100)
422 Log.d(LOG_TAG,
"onProgressChanged progress value [0-100]: " + progress
423 +
", reporting to user as: " + thumbPosition);