自定义View(进度条跳转)

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/com.bawie.www.week1demo."
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bawie.www.week1demo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#6a9ff5"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="@drawable/topbar_up"
            android:id="@+id/toleft"
            android:layout_weight="1"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="那些花儿"
            android:layout_weight="8"
            android:textSize="40dp"
            android:gravity="center"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="@mipmap/ic_launcher"
            android:id="@+id/btn_tiao"
            android:layout_weight="1"
            />

    </LinearLayout>
    <com.bawie.www.week1demo.CompletedView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/circleView"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        />

    <Button
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="扫描二维码"
        android:id="@+id/btn_sm"
        android:layout_marginTop="30dp"
        android:onClick="start"
        />

</LinearLayout>

MainActivity

package com.bawie.www.week1demo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

public class MainActivity extends AppCompatActivity {
    private Button btn_tiao;
    private Button btn_sm;
    int REQUEST_CODE=1;
    private CompletedView circleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        ZXingLibrary.initDisplayOpinion(this);
        circleView = (CompletedView) findViewById(R.id.circleView);
        btn_sm =(Button) findViewById(R.id.btn_sm);
        btn_tiao= (Button) findViewById(R.id.btn_tiao);
        btn_tiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in =new Intent(MainActivity.this,Trapezoid.class);
                startActivity(in);
            }
        });
    }
    int progress = 0;
    public void start(View v) {
        circleView.setMax(100);
        progress=0;
        new Thread() {
            public void run() {
                while (true) {
                    progress = progress + 1;
                    String text = progress + "%";
                    circleView.setProgressAndText(progress, text);
                    try {
                        sleep(30);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (progress == 100) {
                        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                        startActivityForResult(intent, REQUEST_CODE);
                        break;
                    }
                }
            };
        }.start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果(在界面上显示)
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

}
Complete
package com.bawie.www.week1demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 杨文倩 on 2017/11/4.
 */

public class CompletedView extends View{

    int progress = 0;
    private String text="0%";
    private int max = 100;

    public CompletedView(Context context) {
        super(context);
    }

    public CompletedView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CompletedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 对于画笔
        Paint paint = new Paint();
        // 设置抗锯齿
        paint.setAntiAlias(true);
        // 设置画笔颜色

        // 三种样式--Stroke 只要描边 Fill 填充 FILL_AND_STROKE和既有描边又有填充
        paint.setStyle(Paint.Style.STROKE);
        //设置描边宽度
        paint.setStrokeWidth(2);
        //定义外圈员的颜色
        paint.setColor(Color.RED);
        //绘制圆形进度条--获取当前控件多大,正好让进度条在这个控件区间内
        canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, getMeasuredWidth()/2, paint);
        //重新设置描边宽度,这个宽度最好能完全盖过圆形
        paint.setStrokeWidth(3);

        //定义限制圆弧的矩形,当前这样定义正好让圆弧和圆重合
        RectF oval = new RectF(0, 0, getMeasuredWidth(), getMeasuredWidth());
        //设置进度条(圆弧的颜色)
        paint.setColor(Color.GREEN);
        //绘制,设置进度条的度数从0开始,结束值是个变量,可以自己自由设置,来设置进度
        //true和false 代表是否使用中心点,如果true,代表连接中心点,会出现扇形的效果
        canvas.drawArc(oval, 0, 360 * progress / max, false, paint);
        //文字的绘制
        paint.setTextSize(40);
        //设置文字宽度
        paint.setStrokeWidth(1.0f);
        //测量文字大小-提前准备个矩形
        Rect bounds = new Rect();
        //测量文字的宽和高,测量的值可以根据矩形获取
        paint.getTextBounds(text, 0, text.length(), bounds);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        //绘制文字,计算文字的宽高进行设置
        canvas.drawText(text, getMeasuredWidth()/2 - bounds.width() / 2,
                getMeasuredWidth()/2 + bounds.height() / 2, paint);

    }
    /**
     * 初始设置当前进度的最大值-默认100
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
    }
    /**
     * 更新进度和文字
     * @param progress
     * @param text
     */
    public void setProgressAndText(int progress, String text) {
        this.progress = progress;
        this.text = text;
        //重新绘制
        postInvalidate();
    }
}

attrs

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--圆弧进度条-->
    <declare-styleable name="TasksCompletedView">
        <attr name="radius" format="dimension"/>
        <attr name="strokeWidth" format="dimension"/>
        <attr name="circleColor" format="color"/>
        <attr name="ringColor" format="color"/>
        <attr name="ringBgColor" format="color"/>
    </declare-styleable>
</resources>

color

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="white">#FFFFFF</color>
    <color name="white2">#f5f3f3</color>
    <color name="colorRed">#d50f09</color>
</resources>
dView
原文地址:https://www.cnblogs.com/yu12/p/7883595.html