Android ProgressBar實現(xiàn)進度條效果
更新時間:2022年04月19日 11:32:11 作者:抱著回憶旅行
這篇文章主要為大家詳細介紹了Android ProgressBar實現(xiàn)進度條效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android ProgressBar實現(xià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="設置當前進度固定不可拖動" ? ? ? ? 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("進度為66"); // ? ? ? ?Toast.makeText(this,"進度為:--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)整進度改變速度 // ? ? ? ? ? ? ? ? ? ? ? ? ? ?x1 = x2; // 去掉已經(jīng)用掉的距離, 去掉這句 運行看看會出現(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(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? ? /** ? ? ?* 設置進度顯示在對應的位置 ? ? ?*/ ? ? 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); ? ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
android利用ContentResolver訪問者獲取手機聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪問者獲取手機聯(lián)系人信息,非常具有實用價值,需要的朋友可以參考下。2017-02-02Android中利用App實現(xiàn)消息推送機制的代碼
Android中利用App實現(xiàn)消息推送機制的代碼,需要的朋友可以參考下。2011-05-05Android中ShapeableImageView使用實例詳解(告別shape、三方庫)
之前Google推送了文章,Android?Material組件1.2.0里面就有ShapeableImageView,不用像以前再寫shape,下面這篇文章主要給大家介紹了關于Android中ShapeableImageView使用的相關資料,需要的朋友可以參考下2022-09-09HttpURLConnection和okHttp兩種獲取網(wǎng)絡數(shù)據(jù)的實現(xiàn)方法
下面小編就為大家分享一篇HttpURLConnection和okHttp兩種獲取網(wǎng)絡數(shù)據(jù)的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01