Android 自定义 view 左右进度

package com.inks.jisaiclothesford.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.inks.inkslibrary.utils.L;
import com.inks.jisaiclothesford.R;
import com.inks.jisaiclothesford.data.Datas;

/**
 * Created by inks on 2018/9/21 0021.
 */

public class MyProgress extends View {

    private Paint mPaint;//文字的画笔
    private Paint bgPaint;//
    private Paint circlePaint;//
    public static Typeface typeFace;

    private int specWidthSize,specHeightSize ;
    private String textString = " ";
    private  float textSize;
    private int bgColor,textColor,circleColor;

    private int maxPro = 100;
    private int pro = 50;

    private float onePro;//每一份的宽度

    public void setPro(int pro){
        this.pro = pro;
        invalidate();
    }

    public MyProgress(Context context) {
        this(context, null);
    }

    public MyProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public MyProgress(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/" + Datas.logoFont);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyProgress);
        textSize =typedArray.getDimension(R.styleable.MyProgress_progressTextSize,22);
        bgColor =  typedArray.getColor(R.styleable.MyProgress_bgColor,Color.argb(255, 255, 255, 255));
        textColor =  typedArray.getColor(R.styleable.MyProgress_textColor,Color.argb(255, 255, 255, 255));
        circleColor = typedArray.getColor(R.styleable.MyProgress_circleColor,Color.argb(255, 255, 255, 255));
        textString= typedArray.getString(R.styleable.MyProgress_progresstext);
        typedArray.recycle();

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextSize(textSize);
        mPaint.setColor(textColor);

        bgPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
        bgPaint.setColor(bgColor);
        circlePaint= new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(circleColor);
        mPaint.setAntiAlias(true);//抗锯齿
        bgPaint.setAntiAlias(true);//抗锯齿
        circlePaint.setAntiAlias(true);//抗锯齿
        circlePaint.setStrokeWidth(6);
        //circlePaint.setStyle(Paint.Style.STROKE);
    }


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

         specWidthSize = MeasureSpec.getSize(widthMeasureSpec);//宽
         specHeightSize = MeasureSpec.getSize(heightMeasureSpec);//高

        L.e("specWidthSize:"+specWidthSize);
        L.e("specHeightSize:"+specHeightSize);
        setMeasuredDimension(specWidthSize, specHeightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        L.e("specWidthSize2:"+specWidthSize);
        onePro = (specWidthSize-specHeightSize)/(float)maxPro;
        canvas.save();
        Path probg = new Path();
        RectF rect = new RectF(specHeightSize*0.5f, specHeightSize*0.35f, specWidthSize-specHeightSize*0.5f, specHeightSize*0.65f);
        probg.addRoundRect(rect,specHeightSize*0.25f,specHeightSize*0.25f ,Path.Direction.CCW);
        canvas.clipPath(probg);
        canvas.drawColor(bgColor);
        canvas.restore();
        canvas.save();
        Path probar = new Path();
        probar.addCircle(specHeightSize*0.5f+(pro*onePro), specHeightSize*0.5f, specHeightSize*0.5f-3 ,Path.Direction.CCW);
        canvas.clipPath(probar);
        canvas.drawColor(bgColor);
        canvas.restore();

        if(pro==50){
            textString ="0";
        }else{
            if(pro>50){
                textString ="+"+(pro-50);
            }else if(pro<50){
                textString ="-"+(50-pro);
            }
        }

        Rect textRect = new Rect();
        mPaint.getTextBounds(textString,0,textString.length(), textRect);
       float textWidth = textRect.width();
       L.e("textWidth:"+textWidth);
        float textHeight = textRect.height();
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        float top = fontMetrics.top;//为基线到字体上边框的距离
        float bottom = fontMetrics.bottom;//为基线到字体下边框的距离
        int baseLineY = (int) (specHeightSize / 2 - top / 2 - bottom / 2);//基线中间点的y轴计算公式
        // canvas.drawText(textString, (int) (width*0.5), baseLineY, mPaint);
        canvas.drawText(textString, specHeightSize*0.5f+(pro*onePro)-textWidth/2, baseLineY, mPaint);

//        canvas.save();
//        RectF rect = new RectF(specHeightSize*0.5f, specHeightSize*0.35f, pro*onePro, specHeightSize*0.65f);
//        Path path1 = new Path();
//        path1.addRoundRect(rect,specHeightSize*0.25f,specHeightSize*0.25f ,Path.Direction.CCW);
//        Path path2 = new Path();
//        path2.addCircle(pro*onePro, specHeightSize*0.5f, specHeightSize*0.5f , Path.Direction.CCW);
//        canvas.clipPath(path1);
//        //canvas.clipPath(path2, Region.Op.DIFFERENCE    );
//        canvas.drawColor(bgColor);
//        canvas.restore();


//        canvas.save();
//        RectF rect2 = new RectF(pro*onePro, specHeightSize*0.35f, specWidthSize-specHeightSize*0.5f, specHeightSize*0.65f);
//        Path path3 = new Path();
//        path3.addRoundRect(rect2,specHeightSize*0.25f,specHeightSize*0.25f ,Path.Direction.CCW);
//        Path path4 = new Path();
//        path4.addCircle(pro*onePro, specHeightSize*0.5f, specHeightSize*0.5f , Path.Direction.CCW);
//        canvas.clipPath(path3);
//        //canvas.clipPath(path4, Region.Op.DIFFERENCE    );
//        canvas.drawColor(bgColor);
//        canvas.restore();
//
//        canvas.drawCircle(specHeightSize*0.5f+(pro*onePro), specHeightSize*0.5f, specHeightSize*0.5f-3 ,circlePaint);

//        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
//        float top = fontMetrics.top;//为基线到字体上边框的距离
//        float bottom = fontMetrics.bottom;//为基线到字体下边框的距离
//        int baseLineY = (int) (specHeightSize / 2 - top / 2 - bottom / 2);//基线中间点的y轴计算公式
//        // canvas.drawText(textString, (int) (width*0.5), baseLineY, mPaint);
//        canvas.drawText(textString, 0, baseLineY, mPaint);
    }

}
<com.inks.jisaiclothesford.view.MyProgress
    android:id="@+id/MyProgress"
android:layout_width="@dimen/dp200"
android:layout_height="@dimen/dp30"
android:layout_marginTop="@dimen/dp30"
android:layout_marginLeft="@dimen/dp10"
app:bgColor="@color/color8"
app:circleColor="@color/color8"
app:progressTextSize="@dimen/dp12"
app:textColor="@color/color3"
/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义一个水波进度 View,你需要完成以下几个步骤: 1. 创建一个自定义 View 类,并在构造函数中初始化一些必要的属性,如颜色、线宽等。 2. 重写 onSizeChanged() 方法,在该方法中获取 View 的宽度和高度,并计算出进度条的半径、圆心等相关参数。 3. 重写 onDraw() 方法,在该方法中绘制水波纹效果。 4. 在自定义 View 中添加一个 setProgress() 方法,用于设置进度条的进度。 5. 在布局文件中引入自定义 View,设置 layout_width 和 layout_height 属性,并在代码中调用 setProgress() 方法设置进度条的进度。 下面是一个简单的自定义水波进度 View 的代码示例: ```java public class WaterWaveProgressView extends View { private Paint mPaint; private int mWidth, mHeight; private float mRadius; private float mProgress; public WaterWaveProgressView(Context context) { super(context); init(); } public WaterWaveProgressView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WaterWaveProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(5); mPaint.setStyle(Paint.Style.STROKE); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; mRadius = Math.min(mWidth, mHeight) / 2 * 0.8f; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mPaint); float angle = mProgress / 100 * 360; canvas.drawArc(mWidth / 2 - mRadius, mHeight / 2 - mRadius, mWidth / 2 + mRadius, mHeight / 2 + mRadius, -90, angle, false, mPaint); } public void setProgress(float progress) { mProgress = progress; invalidate(); } } ``` 通过调用 setProgress() 方法来更新进度条的进度,如下所示: ```java WaterWaveProgressView progressView = findViewById(R.id.progress_view); progressView.setProgress(50); // 设置进度为 50% ``` 在布局文件中引入自定义 View: ```xml <com.example.waterwaveprogressview.WaterWaveProgressView android:id="@+id/progress_view" android:layout_width="150dp" android:layout_height="150dp" /> ``` 这样就能够实现一个简单的水波进度 View 了。如果需要更加复杂的效果,可以在 onDraw() 方法中绘制多个水波纹,或者使用 Path 绘制波形等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值