Android ProgressBar實現(xiàn)進(jìn)度條效果
更新時間:2022年04月19日 11:32:11 作者:抱著回憶旅行
這篇文章主要為大家詳細(xì)介紹了Android ProgressBar實現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android ProgressBar實現(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)度固定不可拖動" ? ? ? ? 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)行看看會出現(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)度顯示在對應(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);
?
? ? }
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin學(xué)習(xí)教程之協(xié)程Coroutine
這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)教程之協(xié)程Coroutine的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Android 6.0 無法在SD卡創(chuàng)建目錄的方法
今天小編就為大家分享一篇Android 6.0 無法在SD卡創(chuàng)建目錄的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
詳解android在mob平臺實現(xiàn)qq登陸和分享
這篇文章主要介紹了詳解android在mob平臺實現(xiàn)qq登陸和分享,對接入第三方平臺SDK感興趣的同學(xué)們,可以參考下2021-04-04
Android Tween動畫之RotateAnimation實現(xiàn)圖片不停旋轉(zhuǎn)效果實例介紹
Android中如何使用rotate實現(xiàn)圖片不停旋轉(zhuǎn)的效果,下面與大家共同分析下Tween動畫的rotate實現(xiàn)旋轉(zhuǎn)效果,感興趣的朋友可以參考下哈2013-05-05
Android?鼠標(biāo)光標(biāo)的圖形合成原理實例探究
這篇文章主要為大家介紹了Android?鼠標(biāo)光標(biāo)的圖形合成原理實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01

