Android自定义View-自定义进度条

先看效果图:

实现思路:

1.先画一条平行于X轴的直线。左右两边有间距,画的时候起始点的X轴大于0(代码里面设置为20),终止点距离右边间距也是大于0的(屏幕宽度-20就是X轴的坐标值)。

2.当前进度的颜色和占总进度的百分比,总进度 = 屏幕宽度 - 左右间距。我们传入的当前进度值/100*总进度就是我们在界面上实际的进度值了,然后剩余的就是未走完的进度了。思路清楚了我们来看实现代码:

 

 

package com.example.customeview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyCustomProgress extends View {

    private Paint paint;

    private int width;
    private int height;
    private int progress = 0;


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

    public MyCustomProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    public MyCustomProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        width = getWidth();
        height = getHeight();


        canvas.drawLine(20, height/2, 20 + (width - 40) * progress/100, height/2, paint);

        paint.setColor(Color.BLUE);
        canvas.drawLine(20 + (width - 40) * progress/100, height/2, width - 20, height/2, paint);

        paint.setColor(Color.DKGRAY);
        canvas.drawCircle(20 +  (width - 40) * progress/100, height/2, 10, paint);

        paint.setColor(Color.GREEN);
        paint.setTextSize(25);
        canvas.drawText(progress + "%", (width - 60) * progress/100, height/2 - 20, paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }
}

xml布局中使用:

<com.example.customeview.MyCustomProgress
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp40"
       />

Activity中我们使用Handler进行模拟:

package com.example.customeview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MyCustomProgressActivity extends AppCompatActivity {

    private MyCustomProgress myCustomProgress;
    private int progress = 0;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                if (progress <= 99) {
//                    animatorSet.start();
                    ++progress;
                    myCustomProgress.setProgress(progress);  //更新进度条
                    sendEmptyMessageDelayed(1, 100);
                }
//                if (progress == 100) {
//                    startActivity(new Intent(MainActivity.this, TwoActivity.class));
//                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_custom_progress);

        myCustomProgress = findViewById(R.id.progress);
        handler.sendEmptyMessageDelayed(1, 100);
    }
}

这样就完成了自定义进度条了,有什么不对和错误的地方还请各位大佬指教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值