自定义View

自定义View类

public class MyView extends View {

    private String mytext;
    private int color;
    private int distance;

    public MyView(Context context) {
        this(context, null);

    }

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

    }

    //AttributeSet 属性
    //defStyleAttr 默认的一个主题
    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        mytext = typedArray.getString(R.styleable.MyView_myText);
        color = typedArray.getColor(R.styleable.MyView_myColor, Color.BLUE);
        distance = typedArray.getInt(R.styleable.MyView_distance, 0);

        //释放资源
        typedArray.recycle();
    }


    public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = 0;
        int height = 0;

        int widtMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);

        if (widtMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            width = widthsize;
            height = heightsize;
        } else {
            width = 400;
            height = 500;
        }

        setMeasuredDimension(width, height);

    }

    //布局 位置
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    //绘画
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//      Paint paint = new Paint();
//      paint.setColor(Color.RED);
//      //设置镂空
//      paint.setStyle(Paint.Style.STROKE);
//      canvas.drawCircle(300f,300f,200,paint);
//      canvas.drawARGB(200,100,200,100);
//      canvas.drawLine(300f,300f,500f,500f,paint);
        //设置椭圆
//        Paint paint1 =new Paint();
//        paint1.setColor(Color.RED);
//        canvas.drawArc(100f,100f,500f,500f,0f,45f,true,paint1);
//        Paint paint2 =new Paint();
//        paint2.setColor(Color.BLUE);
//        canvas.drawArc(100f,100f,500f,500f,45f,45f,true,paint2);
//        Paint paint3 =new Paint();
//        paint3.setColor(Color.GREEN);
//        canvas.drawArc(100f,100f,500f,500f,90f,130f,true,paint3);
//        Paint paint4 =new Paint();
//        paint4.setColor(Color.YELLOW);
//        canvas.drawArc(100f,100f,500f,500f,220f,90f,true,paint4);
//        Paint paint5 =new Paint();
//        paint5.setColor(Color.BLACK);
//        canvas.drawArc(100f,100f,500f,500f,310f,50f,true,paint5);

        //控件屏幕高度
        int measuredHeight = getMeasuredHeight() / 2;
        //控件屏幕宽度
        int measuredWidth = getMeasuredWidth() / 2;
        //设置空间背景颜色
//        canvas.drawColor(Color.BLUE);
        Paint paint = new Paint();
        //设置字体颜色
//        paint.setColor(Color.RED);
        paint.setColor(color);
        paint.setTextSize(100);
        //设置控件的水平居中
        paint.setTextAlign(Paint.Align.CENTER);

        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
        //获取center线
        int baseLineY = (fm.bottom - fm.top) / 2 - fm.bottom;
        canvas.drawText(mytext, measuredWidth, measuredHeight + baseLineY, paint);
        canvas.drawText("BBBB", measuredWidth, measuredHeight + distance, paint);

    }

新建MyLinearLayout:

public class MyLinearLayout extends LinearLayout {
    private int marginTop = 50;
    private int marginleft = 50;
    private int marginBottom = 50;
    private int marginRight = 50;
    private int oritation = 1;

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

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

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

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = 0;
        int height = 0;
        int widtMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);

        if (widtMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            width = widthsize;
            height = heightsize;
        } else {

            for (int i = 0; i < getChildCount(); i++) {
                int measuredWidth = getChildAt(i).getMeasuredWidth();
                int measuredHeight = getChildAt(i).getMeasuredHeight();
                if (oritation == 0) {

                    height = measuredHeight + marginTop + marginBottom;

                    if (i == 0) {
                        width += measuredWidth + marginleft + marginRight;
                    } else {
                        width += measuredWidth + marginleft;
                    }
                } else if (oritation == 1) {
                    width = measuredWidth + marginleft + marginRight;
                    if (i == 0) {
                        height += measuredHeight + marginTop + marginBottom;
                    } else {
                        height += measuredHeight + marginBottom;
                    }
                }
            }
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = marginleft;
        int top = marginTop;
        int bottom = marginBottom;
        int right = marginRight;

        for (int i = 0; i < getChildCount(); i++) {
            int measuredWidth = getChildAt(i).getMeasuredWidth();
            int measuredHeight = getChildAt(i).getMeasuredHeight();

            if (oritation == 0) {
                if (i == 0) {
                    right = right + measuredWidth;
                } else {
                    right = right + measuredWidth + marginRight;
                }
                getChildAt(i).layout(left, top, right, measuredHeight + top);

                if (i == 0) {
                    left = left + right;
                } else {
                    left = right + marginRight;
                }
            } else if (oritation == 1) {

                if (i == 0) {
                    bottom = bottom + measuredHeight;
                } else {
                    bottom = bottom + measuredHeight + marginBottom;
                }

                getChildAt(i).layout(left, top, measuredWidth + left, bottom);

                if (i == 0) {
                    top = top + bottom;
                } else {
                    top = bottom + marginBottom;
                }
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值