本文实例为大家分享了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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)