Android ProgressBar實(shí)現(xiàn)進(jìn)度條效果
本文實(shí)例為大家分享了Android ProgressBar實(shí)現(xiàn)進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
1.XML布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? ? <TextView ? ? ? ? android:textSize="20sp" ? ? ? ? android:layout_marginTop="30dp" ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? android:text="設(shè)置當(dāng)前進(jìn)度固定不可拖動(dòng)" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" /> ? ? ? <LinearLayout ? ? ? ? android:id="@+id/full" ? ? ? ? android:layout_centerInParent="true" ? ? ? ? android:orientation="vertical" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="60dp"> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/progesss_value1" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:background="#ddd" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:paddingBottom="8dp" ? ? ? ? ? ? android:paddingLeft="4dp" ? ? ? ? ? ? android:paddingRight="4dp" ? ? ? ? ? ? android:paddingTop="2dp" ? ? ? ? ? ? android:textColor="@android:color/white" ? ? ? ? ? ? android:textSize="12sp" ? ? ? ? ? ? android:text="20%" /> ? ? ? ? <ProgressBar ? ? ? ? ? ? android:layout_gravity="center_horizontal" ? ? ? ? ? ? android:id="@+id/progesss1" ? ? ? ? ? ? style="@style/Widget.AppCompat.ProgressBar.Horizontal" ? ? ? ? ? ? android:layout_width="330dp" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:background="@drawable/myprogressbar" ? ? ? ? ? ? android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal" ? ? ? ? ? ? android:indeterminateOnly="false" ? ? ? ? ? ? android:max="100" ? ? ? ? ? ? android:maxHeight="50dp" ? ? ? ? ? ? android:minHeight="16dp" ? ? ? ? ? ? android:progress="20" ? ? ? ? ? ? android:progressDrawable="@drawable/myprogressbar" /> ? ? </LinearLayout> ? </RelativeLayout>
2.myprogressbar布局
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <item android:id="@android:id/background"> ? ? ? ? <shape> ? ? ? ? ? ? <corners android:radius="10dip" /> ? ? ? ? ? ? <gradient ? ? ? ? ? ? ? ? android:angle="45" ? ? ? ? ? ? ? ? android:endColor="#EAEAEA" ? ? ? ? ? ? ? ? android:startColor="#EAEAEA" /> ? ? ? ? </shape> ? ? </item> ? ? ? <item android:id="@android:id/progress"> ? ? ? ? <clip> ? ? ? ? ? ? <shape> ? ? ? ? ? ? ? ? <corners android:radius="10dip" /> ? ? ? ? ? ? ? ? <gradient ? ? ? ? ? ? ? ? ? ? android:angle="45" ? ? ? ? ? ? ? ? ? ? android:centerColor="#2FD2B3" ? ? ? ? ? ? ? ? ? ? android:endColor="#30C0D0" ? ? ? ? ? ? ? ? ? ? android:startColor="#2EE28B" /> ? ? ? ? ? ? </shape> ? ? ? ? </clip> ? ? </item> ? </layer-list>
3.MainActivity
public class MainActivity extends AppCompatActivity { ? ? ? private ProgressBar progesss; ? ? private TextView progesssValue; ? ? private LinearLayout full; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? ? ? progesss = (ProgressBar) findViewById(R.id.progesss1); ? ? ? ? progesssValue = (TextView) findViewById(R.id.progesss_value1); ? ? ? ? full = (LinearLayout) findViewById(R.id.full); ? ? ? ? ? initview(); ? ? } ? ? ? private void initview() { ? ? ? ? ? progesss.setProgress(66); ? ? ? ? progesssValue.setText(new StringBuffer().append(progesss.getProgress()).append("%")); ? ? ? ? ? setPosWay1(); // ? ? ? ?ToastUtil.showToast("進(jìn)度為66"); // ? ? ? ?Toast.makeText(this,"進(jìn)度為:--66",Toast.LENGTH_SHORT).show(); ? ? ? ? ? // ? ? ? ?full.setOnTouchListener(new View.OnTouchListener() { // // ? ? ? ? ? ?@Override // ? ? ? ? ? ?public boolean onTouch(View v, MotionEvent event) { // ? ? ? ? ? ? ? ?int w = getWindowManager().getDefaultDisplay().getWidth(); // ? ? ? ? ? ? ? ?switch (event.getAction()) { // ? ? ? ? ? ? ? ? ? ?case MotionEvent.ACTION_DOWN: // ? ? ? ? ? ? ? ? ? ? ? ?x1 = (int) event.getRawX(); // ? ? ? ? ? ? ? ? ? ? ? ?progesss.setProgress(100 * x1 / w); // ? ? ? ? ? ? ? ? ? ? ? ?setPos(); // ? ? ? ? ? ? ? ? ? ? ? ?break; // ? ? ? ? ? ? ? ? ? ?case MotionEvent.ACTION_MOVE: // ? ? ? ? ? ? ? ? ? ? ? ?x2 = (int) event.getRawX(); // ? ? ? ? ? ? ? ? ? ? ? ?dx = x2 - x1; // ? ? ? ? ? ? ? ? ? ? ? ?if (Math.abs(dx) > w / 100) { //改變條件 調(diào)整進(jìn)度改變速度 // ? ? ? ? ? ? ? ? ? ? ? ? ? ?x1 = x2; // 去掉已經(jīng)用掉的距離, 去掉這句 運(yùn)行看看會(huì)出現(xiàn)效果 // ? ? ? ? ? ? ? ? ? ? ? ? ? ?progesss.setProgress(progesss.getProgress() + dx * 100 / w); // ? ? ? ? ? ? ? ? ? ? ? ? ? ?setPos(); // ? ? ? ? ? ? ? ? ? ? ? ?} // ? ? ? ? ? ? ? ? ? ? ? ?break; // ? ? ? ? ? ? ? ? ? ?case MotionEvent.ACTION_UP: // ? ? ? ? ? ? ? ? ? ? ? ?break; // ? ? ? ? ? ? ? ?} // ? ? ? ? ? ? ? ?return true; // ? ? ? ? ? ?} // ? ? ? ?}); ? ? ? ? } ? ? ? ? @Override ? ? public void onWindowFocusChanged(boolean hasFocus) { ? ? ? ? super.onWindowFocusChanged(hasFocus); ? ? ? ? if (hasFocus) { ? ? ? ? ? ? setPos(); ? ? ? ? } ? ? } ? ? private void setPosWay1() { ? ? ? ? progesssValue.post(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? setPos(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? ? /** ? ? ?* 設(shè)置進(jìn)度顯示在對(duì)應(yīng)的位置 ? ? ?*/ ? ? public void setPos() { ? ? ? ? int w = getWindowManager().getDefaultDisplay().getWidth(); ? ? ? ? Log.e("w=====", "" + w); ? ? ? ? ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) progesssValue.getLayoutParams(); ? ? ? ? int pro = progesss.getProgress(); ? ? ? ? int tW = progesssValue.getWidth(); ? ? ? ? if (w * pro / 100 + tW * 0.3 > w) { ? ? ? ? ? ? params.leftMargin = (int) (w - tW * 1.1); ? ? ? ? } else if (w * pro / 100 < tW * 0.7) { ? ? ? ? ? ? params.leftMargin = 0; ? ? ? ? } else { ? ? ? ? ? ? params.leftMargin = (int) (w * pro / 100 - tW * 0.7); ? ? ? ? } ? ? ? ? progesssValue.setLayoutParams(params); ? ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android自定義view之實(shí)現(xiàn)日歷界面實(shí)例
本篇文章主要介紹了android自定義view之實(shí)現(xiàn)日歷界面實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android LayoutInflater深入分析及應(yīng)用
這篇文章主要介紹了Android LayoutInflater分析的相關(guān)資料,需要的朋友可以參考下2017-02-02Android開(kāi)發(fā)高仿課程表的布局實(shí)例詳解
這篇文章主要介紹了Android開(kāi)發(fā)高仿課程表的布局實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10android利用ContentResolver訪(fǎng)問(wèn)者獲取手機(jī)聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪(fǎng)問(wèn)者獲取手機(jī)聯(lián)系人信息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼
Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼,需要的朋友可以參考下。2011-05-05Android中ShapeableImageView使用實(shí)例詳解(告別shape、三方庫(kù))
之前Google推送了文章,Android?Material組件1.2.0里面就有ShapeableImageView,不用像以前再寫(xiě)shape,下面這篇文章主要給大家介紹了關(guān)于Android中ShapeableImageView使用的相關(guān)資料,需要的朋友可以參考下2022-09-09Flutter?Flow實(shí)現(xiàn)滑動(dòng)顯隱層示例詳解
這篇文章主要為大家介紹了Flutter?Flow實(shí)現(xiàn)滑動(dòng)顯隱層示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03HttpURLConnection和okHttp兩種獲取網(wǎng)絡(luò)數(shù)據(jù)的實(shí)現(xiàn)方法
下面小編就為大家分享一篇HttpURLConnection和okHttp兩種獲取網(wǎng)絡(luò)數(shù)據(jù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01安卓 獲取手機(jī)IP地址的實(shí)現(xiàn)代碼
本篇文章主要介紹 Android 4.0 獲取手機(jī)IP地址的方法,附有實(shí)現(xiàn)代碼,具有參考價(jià)值,希望對(duì)有需要的小伙伴有幫助2016-07-07