博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发-各种各样好看漂亮的进度条,指示器,加载提示汇总
阅读量:6984 次
发布时间:2019-06-27

本文共 14336 字,大约阅读时间需要 47 分钟。

导读:之前项目中用到一些进度条,找了不少,打算写个demo自己总结一下,留着以后用, 有些是自己写的,有些是github上找的别人的库,如果大家觉得好看可以用,直接下载复制代码到项目里就可以用,ok 废话少说,先上图
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

1.绿色进度条,可以固定的

布局;

其实就是普通的进度条改了样式而已通过设置一个父view包裹progress和textview

通过计算距离父view偏移量,把textview设置距离数值的位置,然后数值就是直接得到进度条的进度然后添加就ok了
代码;

public class Loading1 extends Activity { private ProgressBar progesss; private TextView progesssValue; private LinearLayout full; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading1); 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); } }

2.可拖动的绿色进度条

布局;

代码;设置外层group的触摸监听事件,判断手势,按下,移动,抬起然后设置进度变化,最主要就是文本要对应在进度条数值位置上

public class Loading11 extends Activity { private ProgressBar progesss; private TextView progesssValue,textnum; private LinearLayout full; private int x0,x1, x2, dx; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading11); progesss = (ProgressBar) findViewById(R.id.progesss1); progesssValue = (TextView) findViewById(R.id.progesss_value1); full = (LinearLayout) findViewById(R.id.full); textnum = (TextView) findViewById(R.id.numjd); initview(); } private void initview() { //外面的父view设置触摸监听事件 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); progesssValue.setText(new StringBuffer().append(progesss.getProgress()).append("%")); // ToastUtil.showToast("进度为:--"+progesss.getProgress()); // Toast.makeText(this,"进度为:--"+progesss.getProgress(),Toast.LENGTH_SHORT).show(); textnum.setText("当前进度:"+progesss.getProgress()); }

3.简易seekbar进度条

布局;

代码;自定义view 实现seekbar的监听 ui界面比原生的好看

package com.mylibrary;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.Rect; import android.os.Build; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.SeekBar; import android.widget.TextView; @SuppressLint("NewApi") public class SeekBarIndicated extends FrameLayout implements SeekBar.OnSeekBarChangeListener { private ViewGroup mWrapperIndicator; private ImageView mImageViewIndicator; //显示图片 private TextView mTextViewProgress; //显示文字 private SeekBar mSeekBar; //滑动 private int mSeekBarMarginLeft = 0; private int mSeekBarMarginTop = 0; private int mSeekBarMarginBottom = 0; private int mSeekBarMarginRight = 0; private String mIndicatorText; private int mSeekBarMin = 0; private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener; private TextProvider mTextProviderIndicator; private int mMeasuredWidth; public SeekBarIndicated(Context context) { super(context); if (!isInEditMode()) init(context); } public SeekBarIndicated(Context context, AttributeSet attrs) { super(context, attrs); if (!isInEditMode()) init(context, attrs, 0); } public SeekBarIndicated(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); if (!isInEditMode()) init(context, attrs, defStyleAttr); } private void init(Context context) { init(context, null, 0); } private void init(Context context, AttributeSet attrs, int defStyle) { View view = LayoutInflater.from(context).inflate( R.layout.view_seekbar_indicated, this); bindViews(view); if (attrs != null) setAttributes(context, attrs, defStyle); mSeekBar.setOnSeekBarChangeListener(this); mTextViewProgress.setText(String.valueOf(mSeekBar.getProgress())); getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onGlobalLayout() { mMeasuredWidth = mSeekBar.getWidth() - mSeekBar.getPaddingLeft() - mSeekBar.getPaddingRight(); mSeekBar.setPadding( mSeekBar.getPaddingLeft(), mSeekBar.getPaddingTop() + mWrapperIndicator.getHeight(), mSeekBar.getPaddingRight(), mSeekBar.getPaddingBottom()); setIndicator(); getViewTreeObserver() .removeOnGlobalLayoutListener(this); } }); // mWrapperIndicator.setVisibility(View.GONE); } private void bindViews(View view) { mWrapperIndicator = (ViewGroup) view .findViewById(R.id.wrapper_seekbar_indicator); mImageViewIndicator = (ImageView) view .findViewById(R.id.img_seekbar_indicator); mTextViewProgress = (TextView) view .findViewById(R.id.txt_seekbar_indicated_progress); mSeekBar = (SeekBar) view.findViewById(R.id.seekbar); mTextViewProgress.setGravity(1); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { setIndicator(); if (mOnSeekBarChangeListener != null) mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser); } @Override public void onStartTrackingTouch(SeekBar seekBar) { if (mOnSeekBarChangeListener != null) { mOnSeekBarChangeListener.onStartTrackingTouch(seekBar); mWrapperIndicator.setVisibility(View.VISIBLE); } } @Override public void onStopTrackingTouch(SeekBar seekBar) { if (mOnSeekBarChangeListener != null) { mOnSeekBarChangeListener.onStopTrackingTouch(seekBar); //通过修改这里显示隐藏滑动后的进度数 mWrapperIndicator.setVisibility(View.VISIBLE); } } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void setIndicator() { if (mTextProviderIndicator != null) { mTextViewProgress.setText(mTextProviderIndicator .provideText(getProgress())); } else { if (mIndicatorText != null) { try { mTextViewProgress.setText(String.valueOf(String.format( mIndicatorText, getProgress()))); } catch (Exception e) { mTextViewProgress.setText(String.valueOf(getProgress())); } } else { mTextViewProgress.setText(String.valueOf(getProgress())); } } Rect padding = new Rect(); mSeekBar.getThumb().getPadding(padding); int thumbPos = mSeekBar.getPaddingLeft() + mMeasuredWidth * mSeekBar.getProgress() / mSeekBar.getMax(); thumbPos = (int) Math.ceil(thumbPos); mWrapperIndicator.setX(thumbPos - (int) Math.ceil(mWrapperIndicator.getWidth() / 2)); } private void setAttributes(Context context, AttributeSet attrs, int defStyle) { // then obtain typed array TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.SeekBarIndicated, defStyle, 0); // and get values you need by indexes from your array attributes defined // above mSeekBarMarginLeft = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_seekbar_marginLeft, 0); mSeekBarMarginTop = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_seekbar_marginTop, 0); mSeekBarMarginRight = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_seekbar_marginRight, 0); mSeekBarMarginBottom = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_seekbar_marginBottom, 0); mSeekBarMin = arr.getInt(R.styleable.SeekBarIndicated_seekbar_min, 0); int seekBarMax = arr.getInt(R.styleable.SeekBarIndicated_seekbar_max, 100); int seekBarThumbId = arr.getResourceId( R.styleable.SeekBarIndicated_seekbar_thumb, 0); int seekBarProgressDrawableId = arr.getResourceId( R.styleable.SeekBarIndicated_seekbar_progressDrawable, 0); int indicatorTextStyle = arr.getInt( R.styleable.SeekBarIndicated_indicator_textStyle, 0); int indicatorPaddingLeft = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_paddingLeft, 0); int indicatorPaddingTop = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_paddingTop, 0); int indicatorPaddingRight = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_paddingRight, 0); int indicatorPaddingBottom = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_paddingBottom, 0); mWrapperIndicator.setPadding(indicatorPaddingLeft, indicatorPaddingTop, indicatorPaddingRight, indicatorPaddingBottom); setMin(mSeekBarMin); setMax(seekBarMax); if (seekBarThumbId > 0) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) mSeekBar.setThumb(getResources().getDrawable(seekBarThumbId)); else mSeekBar.setThumb(getResources().getDrawable(seekBarThumbId, null)); } if (seekBarProgressDrawableId > 0) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) mSeekBar.setProgressDrawable(getResources().getDrawable( seekBarProgressDrawableId)); else mSeekBar.setProgressDrawable(getResources().getDrawable( seekBarProgressDrawableId, null)); } mIndicatorText = arr .getString(R.styleable.SeekBarIndicated_indicator_text); mTextViewProgress.setTypeface(mTextViewProgress.getTypeface(), indicatorTextStyle); mSeekBar.setPadding(mSeekBar.getPaddingLeft() + mSeekBarMarginLeft, mSeekBar.getPaddingTop() + mSeekBarMarginTop, mSeekBar.getPaddingRight() + mSeekBarMarginRight, mSeekBar.getPaddingBottom() + mSeekBarMarginBottom); setIndicatorImage(arr); setIndicatorTextAttributes(arr); arr.recycle(); } private void setIndicatorTextAttributes(TypedArray arr) { RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mTextViewProgress .getLayoutParams(); int indicatorTextMarginLeft = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_textMarginLeft, layoutParams.leftMargin); int indicatorTextMarginTop = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_textMarginTop, getContext().getResources().getDimensionPixelSize( R.dimen.indicator_txt_margin_top)); int indicatorTextMarginRight = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_textMarginRight, layoutParams.rightMargin); int indicatorTextMarginBottom = arr.getDimensionPixelSize( R.styleable.SeekBarIndicated_indicator_textMarginBottom, layoutParams.bottomMargin); int indicatorTextColor = arr.getColor( R.styleable.SeekBarIndicated_indicator_textColor, Color.WHITE); if (arr.hasValue(R.styleable.SeekBarIndicated_indicator_textCenterHorizontal)) { boolean centerHorizontal = arr .getBoolean( R.styleable.SeekBarIndicated_indicator_textCenterHorizontal, false); if (centerHorizontal) { layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); if (!arr.hasValue(R.styleable.SeekBarIndicated_indicator_textMarginTop)) indicatorTextMarginTop = 0; } } else { layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); } if (arr.hasValue(R.styleable.SeekBarIndicated_indicator_textCenterVertical)) { boolean centerVertical = arr.getBoolean( R.styleable.SeekBarIndicated_indicator_textCenterVertical, false); if (centerVertical) layoutParams.addRule(RelativeLayout.CENTER_VERTICAL); } // mTextViewProgress.setTextColor(indicatorTextColor); layoutParams.setMargins(indicatorTextMarginLeft, indicatorTextMarginTop, indicatorTextMarginBottom, indicatorTextMarginRight); mTextViewProgress.setLayoutParams(layoutParams); } private void setIndicatorImage(TypedArray arr) { int imageResourceId = arr.getResourceId( R.styleable.SeekBarIndicated_indicator_src, R.drawable.color_seekbar_pop_bg); mImageViewIndicator.setImageResource(imageResourceId); } public void setMax(int max) { mSeekBar.setMax(max - mSeekBarMin); } public void setMin(int min) { mSeekBarMin = min; } public void setValue(int value) { mSeekBar.setProgress(value); setIndicator(); }

转载地址:http://twtpl.baihongyu.com/

你可能感兴趣的文章
2016及以后的自动化测试趋势 -《测试技术六月刊》
查看>>
基于Angular创建后台数据模拟(译)
查看>>
Spring中bean配置的继承
查看>>
用JSP实现学生查询
查看>>
企业网站怎么建设
查看>>
数据库和MySQL相关面试题目
查看>>
Yii 框架学习--01 框架入门
查看>>
All Things OpenTSDB
查看>>
android 网络通信框架volly
查看>>
二分查找算法及其变种
查看>>
一个泛型冒泡排序的实现
查看>>
大型分布式网站架构设计与实践 第一章《面向服务的体系架构(SOA)》
查看>>
[From OpenBSD Man Page]PFSYNC
查看>>
hdu 5131 Song Jiang&#39;s rank list 【2014ACM/ICPC亚洲区广州站-重现赛】
查看>>
Moose File System分布文件系统测试
查看>>
mysql 高可用方案漫谈(二)
查看>>
Java中断机制
查看>>
JS笔记(20): JS中的同步编程和异步编程
查看>>
那几个题(没懂的地方留言)
查看>>
如何改变UITableViewCell的选中样式(颜色)?storyboard上cell的selection不可用?
查看>>