自定义控件,设置其控件大小

自定义控件,设置其控件大小

效果图如下:
效果图如下

思路:
1.创建一个控件类,继承View,然后重写构造,可以重写出所有构造,但至少得有一个。
2.画圆。重写onDraw()方法,画完圆在上面画一个扇形的圆弧。
3.为控件设置默认大小,重写onMeasure方法,然后在构造里面给Bitmap赋值,并且写一个方法获得控件宽高和模式。
注备:模式分为:
MeasureSpec.EXACTLY :控件在xml布局中被指定过宽或高。match_parent也属于MeasureSpec.EXACTLY。
MeasureSpec.AT_MOST:控件在xml布居中使用wrap_content。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。
MeasureSpec.UNSPECIFIED:未指定控件大小,是由父控件通过通过measure方法传入。


xml布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/ll_main_LL"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.androideighteen.MainActivity">
<com.example.androideighteen.MyButton
    android:id="@+id/myButton_main_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        />

</LinearLayout>

Activity文件为:

package com.example.androideighteen;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


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

    }

}

自定义控件代码为:

package com.example.androideighteen;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/7/8.
 */

public class MyButton extends View {
    private  Bitmap bitmp ;
    public MyButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bitmp =Bitmap.createBitmap(200,200, Bitmap.Config.ARGB_8888);
    }



    /**
     * 画布
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.RED);
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        int with = getWidth();
        int height = getHeight();
       float radius = with / 4;
        canvas.drawCircle(with / 2, with / 2, radius, mPaint);
        mPaint.setColor(Color.BLUE);
        RectF oval = new RectF(with / 2 - radius, with / 2 - radius, with / 2
                               + radius, with / 2 + radius); //用于定义的圆弧的形状和大小的界限
        canvas.drawArc(oval, 270, 120, true, mPaint); //根据进度画圆弧
    }

  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       int width = this.getMeasuredSize(widthMeasureSpec,true);
       int height = this.getMeasuredSize(heightMeasureSpec,false);
       setMeasuredDimension(width,height);
    }

    private int getMeasuredSize(int widthMeasureSpec, boolean b) {
        //模式
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        //尺寸
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        //计算所得的实际尺寸,要被返回
        int retSize = 0;
        //得到两侧的留边
        int padding =(b?getPaddingLeft()+getPaddingRight():getPaddingTop()+getPaddingBottom());
        //对不同模式进行判断
        if(specMode == MeasureSpec.EXACTLY){//显示指定控件大小
            retSize = specSize;
        }else{
            retSize = (b?bitmp.getWidth()+padding:bitmp.getHeight()+padding);
            if(specMode==MeasureSpec.UNSPECIFIED){
                retSize = Math.min(retSize,specSize);
            }
        }
        return retSize;
    }
}

如有好的建议或者本博文有误的地方,欢迎指出。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值