自定义View中三个构造方法的含义(本文以继承View举例)

相信很多人自定义View发现实现构造方法会有几个构造可以选择。。但是怎么选择构造方法才正确才符合需求?下面为大家详解

第一个构造方法参数只有一个参数Context context,这种代表直接在java代码中引用如setContentView(View)
第二个构造方法参数有两个参数Context context, AttributeSet attrs,这种就是你在MainActivity关联中的xml文件中当控件使用
第三个构造方法参数有三个参数Context context, AttributeSet attrs,int defStyleAttr这种就是你既要在xml引用,又要自己定义一些属性
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Lenovo on 2015/11/27.
 //这是简单的实现的进度加载的效果
 */
public class MyView extends View {

    private int mwidth;
    private int height;

    private int progress;
    private int progressing;
    //画笔
    private Paint paint = new Paint();
    //加载的颜色
    private int completedColor;
    //未加载的颜色
    private int waitingColor;
    //字体颜色
    private int progressColor;
    //加载宽度
    private int lineWidth;
    //字体大小
    private int ProgessTextSize;

    public MyView(Context context) {

        this(context, null);

    }

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

    }


    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        progressing = 5;
        //TypedArray 自己定义的一些东西
        TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.ProgressLineView);
        lineWidth = (int) type.getDimension(R.styleable.ProgressLineView_linewidth, 1);
        ProgessTextSize = (int) type.getDimension(R.styleable.ProgressLineView_textSize, 12);
        progressColor = type.getColor(R.styleable.ProgressLineView_progressColor, 0xff515151);
        completedColor = type.getColor(R.styleable.ProgressLineView_progressColor, 0xff085cb2);
        waitingColor = type.getColor(R.styleable.ProgressLineView_waitingColor, 0xffa7f0f9);
        paint.setStrokeWidth(lineWidth);
        paint.setTextSize(ProgessTextSize);
        type.recycle();
    }
//供外部传入当前进度方法
    public void updateProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            mwidth = right - left;
            height = bottom - top;
        }
    }

    //  @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String progressinfo = progress + "%";
        float[] progressWidth = new float[progressinfo.length()];
        paint.getTextWidths(progressinfo, progressWidth);
        float textToalWidth = caloToalwidth(progressWidth);

        float cellWidth = (mwidth - (textToalWidth + progress * 2)) / 100;
        paint.setColor(completedColor);
        //设置加载了的颜色
        canvas.drawLine(0f, height / 2f, cellWidth * progress, height / 2f, paint);
        paint.setColor(progressColor);
        //加载多少
        canvas.drawText(progressinfo, cellWidth * progress + progressing, (height + ProgessTextSize) / 2, paint);
        paint.setColor(waitingColor);
        //未加载的颜色
        canvas.drawLine(mwidth - cellWidth * (100 - progress), height / 2f, mwidth, height / 2f, paint);
    }


    private float caloToalwidth(float[] arrfolat) {

        float ress = 0f;
        for (float f : arrfolat) {

            ress += f;
        }
        return ress;
    }


}


//valuse代码==styles
<declare-styleable name="ProgressLineView">

    <attr name="linewidth" format="dimension"></attr>
    <attr name="completedcolor" format="color"></attr>
    <attr name="waitingColor" format="color"></attr>
    <attr name="progressColor" format="color"></attr>
    <attr name="textSize" format="dimension"></attr>
</declare-styleable>

//扩展一些。。在values下的sytyles中我们可以自己去定义一些东西。。颜色,大小。都可以去定义
然后在代码引用type.getDimension(R.styleable.ProgressLineView_linewidth, 1);后面的1就是定义的大小


转载于:https://my.oschina.net/u/2355512/blog/536597

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值