Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add drawable to Slider #13521

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,23 @@ private void updateControl()
private void updateThumb(SeekBar seekBar, KrollDict d)
{
TiFileHelper tfh = null;
String thumbImage = TiConvert.toString(d, "thumbImage");
Object thumbImage = d.get("thumbImage");
if (thumbImage != null) {
if (tfh == null) {
tfh = new TiFileHelper(seekBar.getContext());
}
String url = proxy.resolveUrl(null, thumbImage);
Drawable thumb = tfh.loadDrawable(url, false);
if (thumb != null) {
thumbDrawable = new SoftReference<>(thumb);
seekBar.setThumb(thumb);
} else {
Log.e(TAG, "Unable to locate thumb image for progress bar: " + url);

if (thumbImage instanceof String) {
String url = proxy.resolveUrl(null, (String) thumbImage);
Drawable thumb = tfh.loadDrawable(url, false);
if (thumb != null) {
thumbDrawable = new SoftReference<>(thumb);
seekBar.setThumb(thumb);
} else {
Log.e(TAG, "Unable to locate thumb image for progress bar: " + url);
}
} else if (thumbImage instanceof Number) {
seekBar.setThumb(TiUIHelper.getResourceDrawable((Integer) thumbImage));
}
} else {
seekBar.setThumb(null);
Expand All @@ -173,54 +178,84 @@ private void updateThumb(SeekBar seekBar, KrollDict d)

private void updateTrackingImages(SeekBar seekBar, KrollDict d)
{
String leftImage = TiConvert.toString(d, "leftTrackImage");
String rightImage = TiConvert.toString(d, "rightTrackImage");
Object leftImage = d.get("leftTrackImage");
Object rightImage = d.get("rightTrackImage");

Drawable leftDrawable = null;
Drawable rightDrawable = null;
TiFileHelper tfh = new TiFileHelper(seekBar.getContext());
boolean isLeftImage = false;
boolean isRightImage = false;

if (leftImage != null) {
String leftUrl = proxy.resolveUrl(null, leftImage);
if (leftUrl != null) {
leftDrawable = tfh.loadDrawable(leftUrl, false, true);
if (leftDrawable == null) {
Log.e(TAG, "Unable to locate left image for progress bar: " + leftUrl);
if (leftImage instanceof String) {
String leftUrl = proxy.resolveUrl(null, (String) leftImage);
if (leftUrl != null) {
leftDrawable = tfh.loadDrawable(leftUrl, false, true);
if (leftDrawable == null) {
Log.e(TAG, "Unable to locate left image for progress bar: " + leftUrl);
} else {
isLeftImage = true;
}
}
} else if (leftImage instanceof Number) {
leftDrawable = TiUIHelper.getResourceDrawable((Integer) leftImage);
}
}

if (rightImage != null) {
String rightUrl = proxy.resolveUrl(null, rightImage);
if (rightUrl != null) {
rightDrawable = tfh.loadDrawable(rightUrl, false, true);
if (rightDrawable == null) {
Log.e(TAG, "Unable to locate right image for progress bar: " + rightUrl);
if (rightImage instanceof String) {
String rightUrl = proxy.resolveUrl(null, (String) rightImage);
if (rightUrl != null) {
rightDrawable = tfh.loadDrawable(rightUrl, false, true);
if (rightDrawable == null) {
Log.e(TAG, "Unable to locate right image for progress bar: " + rightUrl);
} else {
isRightImage = true;
}
}
} else if (rightImage instanceof Number) {
rightDrawable = TiUIHelper.getResourceDrawable((Integer) rightImage);
}
}

if (leftDrawable != null || rightDrawable != null) {
LayerDrawable ld = null;
if (rightDrawable == null) {
Drawable[] lda = { new ClipDrawable(leftDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) };
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.progress);
if (isLeftImage) {
Drawable[] lda = { new ClipDrawable(leftDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) };
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.progress);
seekBar.setProgressDrawable(ld);
} else {
seekBar.setProgressDrawable(leftDrawable);
}
} else if (leftDrawable == null) {
rightClipDrawable = new ClipDrawable(rightDrawable, Gravity.RIGHT, ClipDrawable.HORIZONTAL);
Drawable[] lda = { rightClipDrawable };
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.secondaryProgress);
if (isRightImage) {
rightClipDrawable = new ClipDrawable(rightDrawable, Gravity.RIGHT, ClipDrawable.HORIZONTAL);
Drawable[] lda = { rightClipDrawable };
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.secondaryProgress);
seekBar.setProgressDrawable(ld);
} else {
seekBar.setProgressDrawable(rightDrawable);
}
} else {
Drawable[] lda = {
rightDrawable,
new ClipDrawable(leftDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL)
};
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.background);
ld.setId(1, android.R.id.progress);
if (isLeftImage) {
Drawable[] lda = {
rightDrawable,
new ClipDrawable(leftDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL)
};
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.background);
ld.setId(1, android.R.id.progress);
seekBar.setProgressDrawable(ld);
} else {
seekBar.setProgressDrawable(leftDrawable);
seekBar.setBackground(rightDrawable);
}
}
seekBar.setProgressDrawable(ld);

} else {
Log.w(TAG, "Custom tracking images could not be loaded.");
}
Expand Down
34 changes: 34 additions & 0 deletions apidoc/Titanium/UI/Slider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ description: |
[rightTrackImage](Titanium.UI.Slider.rightTrackImage) properties be specified before the properties would be honored. Beginning with
Titanium SDK 4.0.0 this limitation has been removed. However it is recommended that either both or neither be specified.

Beginning with 12.3.0 you can use a drawble as a leftTrackImage/rightTrackImage or thumbImage:
``` js
Ti.UI.createSlider({
min:1,
max:100,
value:50,
thumbImage: Ti.App.Android.R.drawable.custom_thumb
});
```

/app/platform/android/res/drawable/custom_thumb.xml
``` xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#f00"/>
<size
android:width="35dp"
android:height="35dp"/>
</shape>
</item>
</layer-list>
```

To style different parts of the Slider you can set these properties in a custom Android theme:
``` xml
<item name="android:progressBackgroundTint">#f4511e</item>
Expand Down Expand Up @@ -238,6 +263,9 @@ properties:
summary: Image URL of the slider left track.
description: |
See introduction of the <Titanium.UI.Slider> component for implementation specific information on Android Platform.

Android: You can use a drawble as an image too. Like `Ti.App.Android.R.drawable.seekbar_style` (a file called
seekbar_style.xml in /app/platform/android/res/drawable/).
type: String
platforms: [android, iphone, ipad, macos]

Expand Down Expand Up @@ -333,6 +361,9 @@ properties:
summary: Image URL of the slider right track.
description: |
See introduction of the <Titanium.UI.Slider> component for implementation specific information on Android Platform.

Android: You can use a drawble as an image too. Like `Ti.App.Android.R.drawable.seekbar_style` (a file called
seekbar_style.xml in /app/platform/android/res/drawable/).
type: String
platforms: [android, iphone, ipad, macos]

Expand Down Expand Up @@ -396,6 +427,9 @@ properties:
The thumb image can be specified as a URL.

Support for using <Titanium.Blob> for this property is only available on iOS.

Android: You can use a drawble as an image too. Like `Ti.App.Android.R.drawable.custom_thumb` (a file called
custom_thumb.xml in /app/platform/android/res/drawable/).
type: [String, Titanium.Blob]
platforms: [android, iphone, ipad, macos]

Expand Down