您好,登錄后才能下訂單哦!
如何在Android應(yīng)用中利用SeekBa添加一個(gè)進(jìn)度條功能?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
step1、先來看一看PopupWindow的布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/seek_dialog_bg" > <!-- 展現(xiàn)當(dāng)前播放進(jìn)度時(shí)間的文本框--> <TextView android:id="@+id/dialogSeekTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginTop="12dip" android:text="@string/unknow_seek_time" android:textColor="@color/black" android:textSize="12sp" /> </RelativeLayout>
step2、自定義一個(gè)SeekBar
import com.canplay.video.R; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.PopupWindow; import android.widget.SeekBar; import android.widget.TextView; /** * 自定義進(jìn)度拖動(dòng)條控件 */ public class MySeekBar extends SeekBar { /** * 定義一個(gè)展現(xiàn)時(shí)間的PopupWindow */ private PopupWindow mPopupWindow; private View mView; /** * 顯示時(shí)間的TextView */ private TextView dialogSeekTime; /** * 用來表示該組件在整個(gè)屏幕內(nèi)的絕對坐標(biāo),其中 mPosition[0] 代表X坐標(biāo),mPosition[1] 代表Y坐標(biāo)。 */ private int[] mPosition; /** * SeekBar上的Thumb的寬度,即那個(gè)托動(dòng)的小黃點(diǎn)的寬度 */ private final int mThumbWidth = 25; public MySeekBar(Context context) { this(context, null); } public MySeekBar(Context context, AttributeSet attrs) { super(context, attrs); mView = LayoutInflater.from(context).inflate(R.layout.seek_popu, null); dialogSeekTime = (TextView) mView.findViewById(R.id.dialogSeekTime); mPopupWindow = new PopupWindow(mView, mView.getWidth(), mView.getHeight(), true); mPosition = new int[2]; } /** * 獲取控件的寬度 * * @param v * @return 控件的寬度 */ private int getViewWidth(View v) { int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); v.measure(w, h); return v.getMeasuredWidth(); } /** * 獲取控件的高度 * * @param v * @return 控件的高度 */ private int getViewHeight(View v) { int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); v.measure(w, h); return v.getMeasuredHeight(); } /** * 隱藏進(jìn)度拖動(dòng)條的PopupWindow */ public void hideSeekDialog() { if (mPopupWindow != null && mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } } /** * 顯示進(jìn)度拖動(dòng)條的PopupWindow * * @param str * 時(shí)間值 */ public void showSeekDialog(String str) { dialogSeekTime.setText(str); int progress = this.getProgress(); // 計(jì)算每個(gè)進(jìn)度值所占的寬度 int thumb_x = (int) (progress * (1.0f * (this.getWidth() - 22) / this.getMax())); //22是兩邊的空白部分寬度 // 更新后的PopupWindow的Y坐標(biāo) int middle = this.getHeight() / 2 + 120; if (mPopupWindow != null) { try { /* * 獲取在整個(gè)屏幕內(nèi)的絕對坐標(biāo),注意這個(gè)值是要從屏幕頂端算起,也就是包括了通知欄的高度。 * 其中 mPosition[0] 代表X坐標(biāo),mPosition[1]代表Y坐標(biāo)。 */ this.getLocationOnScreen(mPosition); // 相對某個(gè)控件的位置(正左下方),在X、Y方向各有偏移 mPopupWindow.showAsDropDown(this, (int) mPosition[0], mPosition[1]); /* * 更新后的PopupWindow的X坐標(biāo) * 首先要把當(dāng)前坐標(biāo)值減去PopWindow的寬度的一半,再加上Thumb的寬度一半。 * 這樣才能使PopWindow的中心點(diǎn)和Thumb的中心點(diǎn)的X坐標(biāo)相等 */ int x = thumb_x + mPosition[0] - getViewWidth(mView) / 2 + mThumbWidth / 2; // 更新popup窗口的位置 mPopupWindow.update(x, middle, getViewWidth(mView), getViewHeight(mView)); } catch (Exception e) { } } } }
step3、將自定義的拖動(dòng)條加入到布局文件中,下面是部分代碼
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/black" > ...... <!-- 進(jìn)度拖動(dòng)條 --> <RelativeLayout android:id="@+id/seek_bar_container" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/control_btn_container" android:background="@drawable/seek_bg" > <com.canplay.video.view.MySeekBar android:id="@+id/seek_progress" android:layout_width="600dip" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <TextView android:id="@+id/currentTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/seek_progress" android:paddingLeft="20dip" android:text="@string/unknow_time" /> </RelativeLayout> ............... </RelativeLayout>
step4、在主文件中對拖動(dòng)條進(jìn)行托動(dòng)監(jiān)聽
mSeekBar = (MySeekBar) findViewById(R.id.seek_progress); mSeekBar.setOnSeekBarChangeListener(mSeekBarListener); /** * 進(jìn)度拖動(dòng)條監(jiān)聽器 */ private OnSeekBarChangeListener mSeekBarListener = new OnSeekBarChangeListener() { // 通知進(jìn)度已經(jīng)被修改 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (isTouchSeeked) { mSeekBar.showSeekDialog(makeTimeString(progress));//動(dòng)態(tài)展示當(dāng)前播放時(shí)間 } else { mSeekBar.hideSeekDialog(); } } // 通知用戶已經(jīng)開始一個(gè)觸摸拖動(dòng)手勢 public void onStartTrackingTouch(SeekBar seekBar) { showControlView(3600000); isTouchSeeked = true; } // 通知用戶觸摸手勢已經(jīng)結(jié)束 public void onStopTrackingTouch(SeekBar seekBar) { Message msg = Message.obtain(); msg.what = PROGRESS_SEEKTO; msg.arg1 = seekBar.getProgress(); mHandler.removeMessages(PROGRESS_SEEKTO); mHandler.sendMessageAtTime(msg, 1000);// 1秒之后開始發(fā)送更新進(jìn)度的消息 isTouchSeeked = false; showControlView(sDefaultTimeout); } };
其中將進(jìn)度值轉(zhuǎn)換為時(shí)間的方法makeTimeString(int secs)如下所示:
/** * 格式化的Builder */ private StringBuilder sFormatBuilder = new StringBuilder(); /** * 格式化的Formatter */ private Formatter sFormatter = new Formatter(sFormatBuilder, Locale.getDefault()); /** * 格式化的相關(guān)屬性 */ private final Object[] sTimeArgs = new Object[3]; /** * 轉(zhuǎn)換進(jìn)度值為時(shí)間 * * @param secs * @return */ private String makeTimeString(int secs) { /** * %[argument_index$][flags][width]conversion 可選的 * argument_index 是一個(gè)十進(jìn)制整數(shù),用于表明參數(shù)在參數(shù)列表中的位置。第一個(gè)參數(shù)由 "1$" * 引用,第二個(gè)參數(shù)由 "2$" 引用,依此類推。 可選 flags * 是修改輸出格式的字符集。有效標(biāo)志集取決于轉(zhuǎn)換類型。 可選 width * 是一個(gè)非負(fù)十進(jìn)制整數(shù),表明要向輸出中寫入的最少字符數(shù)。 可選 precision * 是一個(gè)非負(fù)十進(jìn)制整數(shù),通常用來限制字符數(shù)。特定行為取決于轉(zhuǎn)換類型。 所需 conversion * 是一個(gè)表明應(yīng)該如何格式化參數(shù)的字符。給定參數(shù)的有效轉(zhuǎn)換集取決于參數(shù)的數(shù)據(jù)類型。 */ String durationformat = getString(R.string.durationformat);// <xliff:g // id="format">%1$02d:%2$02d:%3$02d</xliff:g> sFormatBuilder.setLength(0); secs = secs / 1000; Object[] timeArgs = sTimeArgs; timeArgs[0] = secs / 3600; // 秒 timeArgs[1] = (secs % 3600) / 60; // 分 timeArgs[2] = (secs % 3600 % 60) % 60; // 時(shí) return sFormatter.format(durationformat, timeArgs).toString().trim(); }
關(guān)于如何在Android應(yīng)用中利用SeekBa添加一個(gè)進(jìn)度條功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。