Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the effect of progress bar by Android ProgressBar

Shulou Source: shulou.com Published: 2022-05-31 21:06:29 09月16日 Update

这篇文章主要介绍了Android ProgressBar怎么实现进度条效果的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android ProgressBar怎么实现进度条效果文章都会有所收获,下面我们一起来看看吧。

具体效果如下

1.XML布局

2.myprogressbar布局

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); }}关于"Android ProgressBar怎么实现进度条效果"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"Android ProgressBar怎么实现进度条效果"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

Tags: Progress effect knowledge content layout article value location easy to operate article easy to understand more condition look industry information channel speed channel learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information MariaDB Xiaomi Redmi