你好,游客 登录 注册 搜索
背景:
阅读新闻

Android自定义进度条VIEW

[日期:2017-05-19] 来源:www.jianshu.com  作者:LOP_zzy3 [字体: ]

最近做开发遇到的 要写一个小视频录制的功能,本来采用的趣拍的SDK,但是方法数超了,所以没有办法就用了一个开源的录制功能自己来写页面,这次就先分享一下我自定义的进度条view,实现的功能是按下拍摄进度条走同时视频拍摄,抬起手 进度条暂停,拍摄暂停,再次按下进度条继续走动,拍摄继续,全部完成了拍摄合成一个小视频,这一次就先分享这个进度条,下一次再来分享拍摄的部分。
下面上代码,先是XML文件

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.example.zzy.progreeviewdemo.ProgressView
        android:id="@+id/my_progress_view"
        android:layout_width="match_parent"
        app:topcolor="@color/colorAccent"
        android:layout_height="40dp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.48"
            android:orientation="horizontal">
    <Button
        android:id="@+id/tv_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始" />
    <Button
        android:id="@+id/tv_reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="重拍" />
    </LinearLayout>
</LinearLayout>

这个就是主页面包含了一个自定义的进度条和一个开始的按钮还有一个重拍的按钮


 

接下来上java代码

public class ProgressView extends View {

    private Context mContext;
    private WindowManager mWindowManager;


    public ProgressView(Context context) {
        this(context, null);
        this.mContext = context;
    }

    public ProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        progressPaint = new Paint();
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.ProgressView, 0, 0);
        int topColor = a.getColor(R.styleable.ProgressView_topcolor, mContext.getResources().getColor(R.color.default_color));
        progressPaint.setColor(topColor);
        maxProgressSize = a.getInteger(R.styleable.ProgressView_time, 10 * millisecond);
        this.mContext = context;
        init();

    }


    private int millisecond = 1000;
    private float maxProgressSize = 8 * millisecond;//总进度是8
    private float eachProgressWidth = 0;
    private Paint progressPaint;

    public void setMaxProgressSize(float maxProgressSize) {
        this.maxProgressSize = maxProgressSize;
    }

    private void init() {
        //设置每一刻度的宽度
        DisplayMetrics dm = new DisplayMetrics();
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mWindowManager.getDefaultDisplay().getMetrics(dm);
        eachProgressWidth = dm.widthPixels / (maxProgressSize * 1.0f);
        //进度条的背景颜色
        setBackgroundColor(Color.parseColor("#19000000"));
        //进度条的前景颜色,画笔
        progressPaint = new Paint();
        progressPaint.setStyle(Paint.Style.FILL);
        //    progressPaint.setColor(Color.parseColor("#ffffff"));

    }

    private long initTime = -1;//上一次刷新完成后的时间
    private boolean isStart = false;
    private float countWidth = 0;//进度条进度的进程,每次调用invalidate()都刷新一次
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isStart) {
            canvas.drawRect(0, 0, countWidth, getMeasuredHeight(), progressPaint);
            return;
        }
        if (initTime == -1) {
            initTime = System.currentTimeMillis();
            canvas.drawRect(0, 0, countWidth, getMeasuredHeight(), progressPaint);
            invalidate();
            return;
        }
        //这次刷新的时间,用于与上一次刷新完成的时间作差得出进度条需要增加的进度
        long thisTime = System.currentTimeMillis();
        countWidth += eachProgressWidth * (thisTime - initTime) * 1.0f;
        if (countWidth > getMeasuredWidth()) {
            countWidth = getMeasuredWidth();
        }
        canvas.drawRect(0, 0, countWidth, getMeasuredHeight(), progressPaint);
        //如果都了最大长度,就不再调用invalidate();了
        if (countWidth < getMeasuredWidth() && isStart) {
            initTime = System.currentTimeMillis();
            invalidate();
        }
        else {
//            c
            isStart = false;
        }
    }
    public float getTime() {
        return countWidth/getMeasuredWidth() * maxProgressSize;
    }
    public float getMaxProgressSize() {
        return maxProgressSize;
    }
    //开始或暂停进度条进度刷新
    public void setIsStart(boolean isStart) {
        if (isStart == this.isStart)
            return;
        this.isStart = isStart;
        if (isStart) {
            initTime = -1;
            invalidate();
        }
    }
    //重置进度条
    public void reset() {
        countWidth = 0;
        initTime = -1;
        isStart = false;
        invalidate();
    }
}

再写一个管理类

public class ProgressViewMag{
    private Button start;
    private ProgressView progressView;
    private Button restart;
    public ProgressViewMag(Button start, ProgressView progressView, Button restart) {
        this.start = start;
        this.progressView = progressView;
        this.restart = restart;
        initView();
    }
    private void initView() {
        Start();
        restart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressView.reset();
            }
        });
    }
    public float getTime() {
        return progressView.getTime();
    }
    public float getMaxProgressSize(){
        return progressView.getMaxProgressSize();
    }
    public void Start() {
            start.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        progressView.setIsStart(false);
                    }
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        progressView.setIsStart(true);
                    }
                    return false;
                }
            });
    }
}

然后是主页

public class MainActivity extends Activity {
    private Button start;
    private ProgressView progressView;
    private Button restart;
    ProgressViewMag manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = (Button) findViewById(R.id.tv_start);
        restart = (Button) findViewById(R.id.tv_reset);
        progressView = (ProgressView)findViewById(R.id.my_progress_view);
        manager = new ProgressViewMag(start,progressView,restart);
    }
}

这就是实现自定义进度条的全部代码了,写的可能有些糙,如果有错误的地方欢迎给我指出,下次我会更新配上拍摄视频的部分。

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-05/143963.htm

linux
相关资讯       Android进度条  Android自定义进度条 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款