我正在尝试为我的应用程序创建自定义seekbar
。
我想得到的是:
到目前为止我得到了什么:
我为Thumb
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#16A085"/>
<size
android:height="30dp"
android:width="30dp" />
</shape>
和吧:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/SecondaryProgress">
<shape android:shape="rectangle">
<solid android:color="#EBEDEF" />
<corners android:radius="35dp" />
</shape>
</item>
<item android:id="@android:id/progress">
<shape android:shape="rectangle">
<padding android:left="10dp" android:top="20dp" android:right="10dp" android:bottom="20dp" />
<solid android:color="#1ABC9C" />
<corners android:radius="15dp" />
</shape>
</item>
</layer-list>
我能做些什么来实现真正的&#34; seekbar?
感谢您的帮助。
答案 0 :(得分:1)
我找到了另一种实现自定义搜索栏的方法。 这是我目前使用的代码
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
public class FlatSeekBar extends android.widget.SeekBar
{
private int size = 20;
public FlatSeekBar(Context context)
{
super(context);
init(null, true);
}
public FlatSeekBar(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs, true);
}
public FlatSeekBar(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(attrs, true);
}
private void init(AttributeSet attrs, boolean applyAttributeTheme)
{
// setting thumb
PaintDrawable thumb = new PaintDrawable(Color.parseColor("#2C3E50"));
thumb.setCornerRadius(size * 9 / 8);
thumb.setIntrinsicWidth(size * 9 / 4);
thumb.setIntrinsicHeight(size * 9 / 4);
setThumb(thumb);
// progress
PaintDrawable progress = new PaintDrawable(Color.parseColor("#34495E"));
progress.setCornerRadius(size);
progress.setIntrinsicHeight(size);
progress.setIntrinsicWidth(size);
progress.setDither(true);
ClipDrawable progressClip = new ClipDrawable(progress, Gravity.LEFT, ClipDrawable.HORIZONTAL);
// secondary progress
PaintDrawable secondary = new PaintDrawable(Color.parseColor("#EBEDEF"));
secondary.setCornerRadius(size);
secondary.setIntrinsicHeight(size);
ClipDrawable secondaryProgressClip = new ClipDrawable(secondary, Gravity.LEFT, ClipDrawable.HORIZONTAL);
// background
PaintDrawable background = new PaintDrawable(Color.parseColor("#EBEDEF"));
background.setCornerRadius(size);
background.setIntrinsicHeight(size);
// applying drawable
LayerDrawable ld = (LayerDrawable) getProgressDrawable();
ld.setDrawableByLayerId(android.R.id.background, background);
ld.setDrawableByLayerId(android.R.id.progress, progressClip);
ld.setDrawableByLayerId(android.R.id.secondaryProgress, secondaryProgressClip);
}
}
只需编辑这四种颜色即可获得自己的颜色:
#2C3E50
#34495E
#EBEDEF
#EBEDEF
我希望它有所帮助;)
答案 1 :(得分:0)
如果您想使用自定义背景drawable而不是颜色尝试如下
LayerDrawable layerDrawable =(LayerDrawable)mProgressBar.getProgressDrawable();
Drawable progress;
progress = new ClipDrawable(new BitmapDrawable(mContext.getResources(), bitmap),
Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
layerDrawable.setDrawableByLayerId(android.R.id.progress, progress);
Drawable background;
background =
new BitmapDrawable(mContext.getResources(), bitmap);
layerDrawable.setDrawableByLayerId(android.R.id.background, background);
ProgressBar.setProgressDrawable(layerDrawable);
mProgressBar.setBackground(background);
mProgressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(
itemView.getContext(),R.color.colorAccent),
PorterDuff.Mode.SRC_IN);