项目需要,需要做一个类微信小视频拍摄程序,需要用到进度条,于是做了个简单的中间向两端,两端向中间并发前进的进度条,使用线性布局作为基础控件,控件拿来即可直接使用,就不多做累赘。
新建测试用布局Xml文件(包名请自定):
<?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:orientation="vertical" > <com.example.smallvideo.BothLineProgress android:id="@+id/mybp" android:layout_width="0dp" android:layout_height="3dp" android:background="#ff8a00" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/btaas" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按下"/> </LinearLayout>
自定义进度条,设置Mode可设置方向,详细说明都在代码内:
package com.example.smallvideo; import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.graphics.Color; import android.os.Handler; import android.util.AttributeSet; import android.util.Log; import android.widget.LinearLayout; /** * 中间向两端或两端向中间并发进度条 * @author Jaiky * @date 2015年7月1日 * PS: Not easy to write code, please indicate. */ public class BothLineProgress extends LinearLayout { private Context context; private Handler handler = new Handler(); private android.view.ViewGroup.LayoutParams layoutParams = null; //现在高度 private float nowWidth = 0; //屏幕宽度 private int screenWidth = 0; //行走距离 private float runDistance = 1; //计时器 private Timer timer; //回调接口 private OnBothLineProgressFinishListener obpf; //模式,默认为中间到两端 private int mode = MODE_CENTER_TO_BOTH; /** * 中间到两端延伸 */ public static final int MODE_CENTER_TO_BOTH = 0; /** * 两端到中间延伸 */ public static final int MODE_BOTH_TO_CENTER = 1; //计算耗时用的,可注释 private long starttime = 0; public BothLineProgress(Context context) { super(context); this.context = context; init(); } public BothLineProgress(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; init(); } public BothLineProgress(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; init(); } private void init() { screenWidth = context.getResources().getDisplayMetrics().widthPixels; // setBackgroundColor(Color.parseColor("#ff8a00")); } /** * 开始启动进度条 * @param duration 需要多少毫秒跑完进度 (1秒 = 1000毫秒) * @param runTime 频率,默认为10,越大更新频率越慢 * @param mode 模式,可选(BothLineProgress.MODE_CENTER_TO_BOTH) 或(BothLineProgress.MODE_BOTH_TO_CENTER) */ public void startRunProgress(long duration, int runTime, int mode) { this.mode = mode; if (runTime < 1) runTime = 10; layoutParams = getLayoutParams(); nowWidth = layoutParams.width; //跑满屏幕或设置XML的进度条长度,需要每10毫秒跑多远距离(根据需求设置布局XML文件android:layout_width="0dp") if (mode == MODE_CENTER_TO_BOTH) { runDistance = (float)(screenWidth - nowWidth) / ((float)duration / (float)runTime); } else { if (nowWidth <= 0) { nowWidth = screenWidth; } runDistance = (float)nowWidth / ((float)duration / (float)runTime); } Log.e("XXX", "每次跑:" + runDistance + " 开始宽度:" + nowWidth + " 频率:" + runTime); timer = new Timer(); timer.schedule(new MyTimerTask(), 0, runTime); starttime = System.currentTimeMillis(); } /** * 停止进度条 */ public void stopProgress(){ if (timer != null) { timer.cancel(); } } /** * 设置进度条颜色 * @param colorString 颜色码,如“#000000” */ public void setPorgressColor(String colorString) { setBackgroundColor(Color.parseColor(colorString)); } //计时器任务 private class MyTimerTask extends TimerTask{ @Override public void run() { boolean isnext = true; if (mode == MODE_CENTER_TO_BOTH) isnext = (nowWidth < screenWidth) ? true : false; else isnext = (nowWidth > 0) ? true : false; if (isnext) { if (mode == MODE_CENTER_TO_BOTH) nowWidth = nowWidth + runDistance; else nowWidth = nowWidth - runDistance; Log.i("XXX", nowWidth + ""); handler.post(new Runnable() { @Override public void run() { //设置新布局 layoutParams.width = (int)nowWidth; setLayoutParams(layoutParams); } }); } else { long c = System.currentTimeMillis() - starttime; Log.e("XXX", "时间:" + c); //回调,到UI线程更新 if (obpf != null) { handler.post(new Runnable() { @Override public void run() { obpf.onFinished(); } }); } timer.cancel(); } } } /** * 设置进度条完成回调接口 * @param obpf */ public void setOnBothLineProgressFinishListener(OnBothLineProgressFinishListener obpf) { this.obpf = obpf; } /** * 进度条处理完成回调接口 * @author Jaiky * @date 2015年7月1日 * PS: Not easy to write code, please indicate. */ public interface OnBothLineProgressFinishListener { public void onFinished(); } /** * DP转PX * @param dpValue * @return */ public int dip2px(float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } }
新建测试TestActivity文件,注意在AndroidManifest中添加:
package com.example.smallvideo; import com.example.smallvideo.BothLineProgress.OnBothLineProgressFinishListener; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class TestActivity extends Activity{ BothLineProgress bp; Button btaas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.item); bp = (BothLineProgress) findViewById(R.id.mybp); btaas = (Button) findViewById(R.id.btaas); btaas.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //10秒完成 bp.startRunProgress(10000L, 10, BothLineProgress.MODE_BOTH_TO_CENTER); btaas.setEnabled(false); } }); bp.setOnBothLineProgressFinishListener(new OnBothLineProgressFinishListener() { @Override public void onFinished() { Toast.makeText(getApplicationContext(), "进度条完成了", Toast.LENGTH_SHORT).show(); btaas.setEnabled(true); } }); } }
欢迎转载,但请保留文章原始出处
作者:Jaiky_杰哥
出处:http://blog.csdn.net/jaikydota163/article/details/46712057