網站首頁 編程語言 正文
本文實例為大家分享了Android ProgressBar實現進度條的具體代碼,供大家參考,具體內容如下
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) { //改變條件 調整進度改變速度
// ? ? ? ? ? ? ? ? ? ? ? ? ? ?x1 = x2; // 去掉已經用掉的距離, 去掉這句 運行看看會出現效果
// ? ? ? ? ? ? ? ? ? ? ? ? ? ?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);
?
? ? }
}
原文鏈接:https://blog.csdn.net/weixin_42630638/article/details/117951290
相關推薦
- 2023-12-15 Linux系統——退出vi編輯模式
- 2022-10-16 QT網絡通信TCP客戶端實現詳解_C 語言
- 2022-11-25 jar包在linux服務器已經運行好但是訪問不到地址的問題及解決方法_Linux
- 2022-06-19 教你用vbs實現微信自動發送消息功能_vbs
- 2022-04-09 Nginx1.21.6生產環境升級步驟_nginx
- 2022-10-16 Qt實現TCP網絡編程_C 語言
- 2022-05-24 C++中的Qt?QTableView詳解_C 語言
- 2022-06-07 Python必備技巧之函數的使用詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支