我需要自定义一个进度条,在预定的时间(比如30秒),在进度条上有一个点。每个视频的时长都不同,所以我该如何在特定的时间点上放置一个点?
我需要自定义一个进度条,在预定的时间(比如30秒),在进度条上有一个点。每个视频的时长都不同,所以我该如何在特定的时间点上放置一个点?
以下是一些可能的解决方案:
SeekBar
上方。这是一个常见的 Android 布局任务,不再赘述;扩展 SeekBar
,像下面这样做(参考关于自定义视图的好解释):
/**
* Seek bar with dots on it on specific time / percent
*/
public class DottedSeekBar extends SeekBar {
/** Int values which corresponds to dots */
private int[] mDotsPositions = null;
/** Drawable for dot */
private Bitmap mDotBitmap = null;
public DottedSeekBar(final Context context) {
super(context);
init(null);
}
public DottedSeekBar(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public DottedSeekBar(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
/**
* Initializes Seek bar extended attributes from xml
*
* @param attributeSet {@link AttributeSet}
*/
private void init(final AttributeSet attributeSet) {
final TypedArray attrsArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.DottedSeekBar, 0, 0);
final int dotsArrayResource = attrsArray.getResourceId(R.styleable.DottedSeekBar_dots_positions, 0);
if (0 != dotsArrayResource) {
mDotsPositions = getResources().getIntArray(dotsArrayResource);
}
final int dotDrawableId = attrsArray.getResourceId(R.styleable.DottedSeekBar_dots_drawable, 0);
if (0 != dotDrawableId) {
mDotBitmap = BitmapFactory.decodeResource(getResources(), dotDrawableId);
}
}
/**
* @param dots to be displayed on this SeekBar
*/
public void setDots(final int[] dots) {
mDotsPositions = dots;
invalidate();
}
/**
* @param dotsResource resource id to be used for dots drawing
*/
public void setDotsDrawable(final int dotsResource) {
mDotBitmap = BitmapFactory.decodeResource(getResources(), dotsResource);
invalidate();
}
@Override
protected synchronized void onDraw(final Canvas canvas) {
super.onDraw(canvas);
final int width = getMeasuredWidth();
final int step = width / getMax();
if (null != mDotsPositions && 0 != mDotsPositions.length && null != mDotBitmap) {
// draw dots if we have ones
for (int position : mDotsPositions) {
canvas.drawBitmap(mDotBitmap, position * step, 0, null);
}
}
}
}
不要忘记在res/values/attrs.xml
中自定义属性:
<resources>
<declare-styleable name="DottedSeekBar">
<attr name="dots_positions" format="reference"/>
<attr name="dots_drawable" format="reference"/>
</declare-styleable>
</resources>
使用以下代码:
setContentView(R.layout.main);
final DottedSeekBar bar = (DottedSeekBar) findViewById(R.id.seekBar);
bar.setDots(new int[] {25, 50, 75});
bar.setDotsDrawable(R.drawable.dot);
使用main.xml布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.TestApp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.TestApp.DottedSeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar" />
</LinearLayout>
或只是单独的main.xml文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.TestApp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.TestApp.DottedSeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
custom:dots_positions="@array/dots"
custom:dots_drawable="@drawable/dot" />
</LinearLayout>
您可以获得以下图像:
查看此示例以获取更多想法;
关于在特定“时间”上放置点的问题:SeekBar
不涉及时间,因此由您提供任何与时间相关的逻辑。
上面的答案完全正确,但onDraw方法可以稍微改进一下。
@Override
protected synchronized void onDraw(final Canvas canvas) {
super.onDraw(canvas);
final float width=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
final float step=width/(float)(getMax());
if (null != mDotsPositions && 0 != mDotsPositions.length && null != mDotBitmap) {
// draw dots if we have ones
for (int position : mDotsPositions) {
canvas.drawBitmap(mDotBitmap, position * step, 0, null);
}
}
}