带进度的progressBar
2016-08-11 17:22
106 查看
优化版本 v2: 进入
1.实现思路:
计算progressBar的宽度,计算屏幕的宽度。根据currentProgress()计算出,progress点在屏幕上的偏移量。然后不断更新偏移量!
这里使用线性布局,嵌套两个TextView
(如果不需要实时更新,定义一个TextView 通过偏移量动态更新TextView的setPadding(leftPadding,0,0,0),如果需要动态更新,就需要定义两个组件。给前一个设置leftPadding,第二个设置具体跟随浮动的内容)。
2.上效果图
3.使用场景:
对于精度要求不高的需求可以使用。因为上面的偏移量和下面的进度有些许的偏差。
4.对比:
市面上同类解决方案,该方式代码量少。容易上手。扩展容易。缺点就是上下位置有偏差。
贴代码:
布局
代码块:
这段代码只作为学习使用参考;
1.实现思路:
计算progressBar的宽度,计算屏幕的宽度。根据currentProgress()计算出,progress点在屏幕上的偏移量。然后不断更新偏移量!
这里使用线性布局,嵌套两个TextView
(如果不需要实时更新,定义一个TextView 通过偏移量动态更新TextView的setPadding(leftPadding,0,0,0),如果需要动态更新,就需要定义两个组件。给前一个设置leftPadding,第二个设置具体跟随浮动的内容)。
2.上效果图
3.使用场景:
对于精度要求不高的需求可以使用。因为上面的偏移量和下面的进度有些许的偏差。
4.对比:
市面上同类解决方案,该方式代码量少。容易上手。扩展容易。缺点就是上下位置有偏差。
贴代码:
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/text_white" android:orientation="vertical"> <LinearLayout android:id="@+id/line_container" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txt_cursor" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/progress_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon_gas_bg" android:gravity="center" android:lines="1" android:textColor="@color/text_white" android:textSize="8sp" android:visibility="gone" /> </LinearLayout> <SeekBar android:id="@+id/seekBar_progressBar" android:layout_width="match_parent" android:layout_height="2dp" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/width_35" android:layout_marginRight="@dimen/width_35" android:layout_marginTop="10dp" android:maxHeight="2dp" android:padding="-2dp" android:progressDrawable="@drawable/progress_background" android:thumb="@null" android:thumbOffset="0dp" /> <TextView android:id="@+id/txt_bytevalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" /> <ImageView android:id="@+id/img_close_pdfdown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="143dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="New Button" /> </LinearLayout> </LinearLayout>
代码块:
package com.example.payui; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.DisplayMetrics; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends Activity { private SeekBar customSeekBar; private TextView txtCur; private int screenWidth; private int defaultOff; private TextView progress_txt; private TextView txt_bytevalue; private int temp = 0; int count = 0; Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { showProgressLoading(msg.what); showDownLoadingText(msg.what, 555524); return false; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pdfactivity); DisplayMetrics displayMetrics = getDisplayMetrics(this); screenWidth = displayMetrics.widthPixels; defaultOff = (int) getResources().getDimension(R.dimen.width_35) * 2; txtCur = (TextView) findViewById(R.id.txt_cursor); txt_bytevalue = (TextView) findViewById(R.id.txt_bytevalue); progress_txt = (TextView) findViewById(R.id.progress_txt); customSeekBar = (SeekBar) findViewById(R.id.seekBar_progressBar); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { customSeekBar.setProgress(0); count = 0; initValue(); } }); initValue(); } public void initValue() { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { final int go = count++; if (go <= 100) { Message message = Message.obtain(); message.what = go; handler.sendMessage(message); } } }, 0, 300); } private void showProgressLoading(long progress) { final int progressWidth = screenWidth - defaultOff; int offLeftWidth = (int) getResources().getDimension(R.dimen.width_35); int endPosition = screenWidth - (int) getResources().getDimension(R.dimen.width_35); int startPositon = (int) (progress * (progressWidth / 100)) + offLeftWidth; int offPosition = startPositon > endPosition ? endPosition : startPositon; if (progress == 0) { txtCur.setPadding(offLeftWidth, 0, 0, 0); } else if (progress == 100) { txtCur.setPadding(progressWidth + (int) getResources().getDimension(R.dimen.width_23), 0, 0, 0); } else { txtCur.setPadding(offPosition, 0, 0, 0); } progress_txt.setText(progress + "%"); customSeekBar.setProgress((int) progress); } private void showDownLoadingText(long progress, long fileSize) { if (progress >= 1) { progress_txt.setVisibility(View.VISIBLE); } temp = (int) (fileSize / 100); txt_bytevalue.setText("DownLoading..." + (progress < 100 ? progress * temp : fileSize) + "k/" + fileSize + "k"); } public static DisplayMetrics getDisplayMetrics(Context context) { DisplayMetrics dm = null; if (context != null) { dm = new DisplayMetrics(); dm = context.getApplicationContext().getResources().getDisplayMetrics(); return dm; } return new DisplayMetrics(); } }
这段代码只作为学习使用参考;
相关文章推荐
- ProgressBar进度条
- ProgressBar 进度条颜色设置
- 设置 ProgressBar 进度条(条形) 的颜色和用代码动态控制显示的长度
- Android-RoundCornerProgressBar——Android圆角进度条(Progress Bar)类库
- Android高级组件之进度条 (progressBar)
- 事件驱动异步模式(BackgroundWorker)ProgressBar设置进度条
- SWT/Jface ProgressBar-进度条(转载自:《Eclipse SWT/JFACE 核心应用》 清华大学出版社 9.7 进度条(ProgressBar))
- CircleNumberProgressBar:显示数字的圆形进度条
- 自定义view圆环跟随progressbar进度改变中心的texview(百分比)
- Jquery progressbar通过Ajax请求获取后台进度演示
- JAVA GUI学习 - JProgressBar进度条组件摘录
- android学习笔记12(ProgressBar--进度条--初级学习)
- (4)ExtJS之进度条ProgressBar
- SquareProgressBar一个方形的加载进度条
- 各种自定义进度条(progressbar或seekbar)的实现
- DataGridView中显示进度条(ProgressBar)
- 自己用图片做的可旋转、不确定进度的ProgressBar
- Android入门逆引手册 - 14 进度条(ProgressBar)的使用
- android之进度条组件ProgressBar
- Flex的进度条(ProgressBar)控件中如何设置进度轨迹条和标签间竖直间隔间距的例子