美文网首页Android开发
自定义View-第十五步:封装控件-自定义控件属性

自定义View-第十五步:封装控件-自定义控件属性

作者: crossroads | 来源:发表于2017-02-16 11:39 被阅读163次

    前言

    根据启舰 的博客所学习的自定义View。

    什么是自定义控件属性?

    首先,咱们先看看这个代码

    croosRoads:age="18"
    

    这可不是系统自带的,而是自定义的属性,那么怎么做呢?利用XML中的declare-styleable标签就可以啦~
    下面就来用declare-styleable来封装控件吧

    一、具体使用方法

    1. 在res/values下创建attrs.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!--为MineView添加自定义属性-->
        <declare-styleable name="MineView">
            <attr name="img" format="reference" />
            <attr name="height" format="dimension" />
            <!--表示既可以自定义color,如##ff00ff,也可以引用,如@color/XXX-->
            <attr name="paintColor" format="color|reference" />
            <attr name="radius" format="integer">
                <enum name="small" value="3"/>
                <enum name="large" value="50"/>
            </attr>
        </declare-styleable>
    </resources>
    

    **2. 布局文件中引用 **

       <com.qianqian.demo.viewdemo.MineView
            android:layout_width="match_parent"
            android:layout_centerInParent="true"
            android:layout_height="match_parent"
            crossRoads:height="300dp"
            android:textColor="#fff"
            crossRoads:img="@mipmap/bear"
            crossRoads:paintColor="@color/colorAccent"
            crossRoads:radius="large"
            />
        <!-- 也可以这样 crossRoads:paintColor="#0fff00"-->
    

    3. MineView中获取这些值

    public class MineView extends View {
        private Paint paint;
        int color;
        float radius;
        Drawable drawable;
        float height;
    
        public MineView(Context context) {
            this(context, null);
        }
    
        public MineView(Context context, AttributeSet attrs) {
            super(context, attrs);
            paint = new Paint();
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MineView);
            color = typedArray.getColor(R.styleable.MineView_paintColor, Color.BLUE);
            paint.setColor(color);
            radius = typedArray.getInteger(R.styleable.MineView_radius, 1);
            drawable = typedArray.getDrawable(R.styleable.MineView_img);
            height = typedArray.getDimension(R.styleable.MineView_height, 100); //获取的值为450px
            //必须回收
            typedArray.recycle();
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            //获取宽高
            int measureHeight=MeasureSpec.getSize(heightMeasureSpec);
            int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
            //获取高度的属性
            int mode=MeasureSpec.getMode(heightMeasureSpec);
            //如果属性是MATCH_PARENT或者是具体的值,就使用自定义的高度
            if (mode==MeasureSpec.EXACTLY) {
                setMeasuredDimension(measureWidth, (int) height);
            }
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            setLayerType(LAYER_TYPE_SOFTWARE, null);//对单独的View在运行时阶段禁用硬件加速
            canvas.drawColor(Color.BLACK);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(2);
            canvas.drawCircle(100, 100, radius, paint);
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            canvas.drawBitmap(bitmap, 100, 200, paint);
        }
    
    }
    
    效果图

    二、declare-styleable标签属性用法

    1. reference: 参考某一资源ID

    <declare-styleable name = "名称">  
           <attr name = "background" format = "reference" />  
    </declare-styleable>  
    
    <ImageView  
            android:layout_width = "42dip"  
            android:layout_height = "42dip"  
            android:background = "@drawable/图片ID"/>  
    

    **2.color: 颜色值 **

    <declare-styleable name = "名称">  
           <attr name = "textColor" format = "color" />  
    </declare-styleable>  
    
    <TextView  
             android:layout_width = "42dip"  
             android:layout_height = "42dip"  
             android:textColor = "#00FF00"/>  
    

    3.boolean:布尔值

    <declare-styleable name = "名称">  
           <attr name = "focusable" format = "boolean" />  
    </declare-styleable>  
    
    <Button  
            android:layout_width = "42dip"  
            android:layout_height = "42dip"  
            android:focusable = "true"/>  
    

    4. dimension:尺寸值

    <declare-styleable name = "名称">  
           <attr name = "layout_width" format = "dimension" />  
    </declare-styleable>  
    
    <Button  
            android:layout_width = "42dip"  
            android:layout_height = "42dip"/>  
    

    5.float:浮点值

    <declare-styleable name = "AlphaAnimation">  
           <attr name = "fromAlpha" format = "float" />  
           <attr name = "toAlpha" format = "float" />  
    </declare-styleable> 
    
    <alpha  
           android:fromAlpha = "1.0"  
           android:toAlpha = "0.7"/>  
    

    6. integer: 整型值

    <declare-styleable name = "AnimatedRotateDrawable">  
           <attr name = "visible" />  
           <attr name = "frameDuration" format="integer" />  
           <attr name = "framesCount" format="integer" />  
           <attr name = "pivotX" />  
           <attr name = "pivotY" />  
           <attr name = "drawable" />  
    </declare-styleable>  
    
    <animated-rotate  
           xmlns:android = "http://schemas.android.com/apk/res/android"   
           android:drawable = "@drawable/图片ID"   
           android:pivotX = "50%"   
           android:pivotY = "50%"   
           android:framesCount = "12"   
           android:frameDuration = "100"/>  
    

    7.string:字符串

    <declare-styleable name = "MapView">  
           <attr name = "apiKey" format = "string" />  
    </declare-styleable>  
    
    <com.google.android.maps.MapView  
           android:layout_width = "fill_parent"  
           android:layout_height = "fill_parent"  
           android:apiKey = "haha" />  
    

    8. fraction:百分数

    <declare-styleable name="RotateDrawable">  
          <attr name = "visible" />  
          <attr name = "fromDegrees" format = "float" />  
          <attr name = "toDegrees" format = "float" />  
          <attr name = "pivotX" format = "fraction" />  
          <attr name = "pivotY" format = "fraction" />  
          <attr name = "drawable" />  
    </declare-styleable>  
    
    <rotate  
          xmlns:android = "http://schemas.android.com/apk/res/android"  
         android:interpolator = "@anim/动画ID"  
          android:fromDegrees = "0"  
         android:toDegrees = "360"  
          android:pivotX = "200%"  
          android:pivotY = "300%"  
         android:duration = "5000"  
          android:repeatMode = "restart"  
          android:repeatCount = "infinite"/>  
    

    9. enum:枚举值

    <declare-styleable name="名称">  
          <attr name="orientation">  
                 <enum name="horizontal" value="0" />  
                 <enum name="vertical" value="1" />  
          </attr>             
    </declare-styleable> 
    
    <LinearLayout  
            xmlns:android = "http://schemas.android.com/apk/res/android"  
            android:orientation = "vertical"  
            android:layout_width = "fill_parent"  
            android:layout_height = "fill_parent">  
    </LinearLayout>  
    

    10. flag:位或运算

    <declare-styleable name="名称">  
           <attr name="windowSoftInputMode">  
                   <flag name = "stateUnspecified" value = "0" />  
                   <flag name = "stateUnchanged" value = "1" />  
                   <flag name = "stateHidden" value = "2" />  
                   <flag name = "stateAlwaysHidden" value = "3" />  
                   <flag name = "stateVisible" value = "4" />  
                   <flag name = "stateAlwaysVisible" value = "5" />  
                   <flag name = "adjustUnspecified" value = "0x00" />  
                   <flag name = "adjustResize" value = "0x10" />  
                   <flag name = "adjustPan" value = "0x20" />  
                   <flag name = "adjustNothing" value = "0x30" />  
            </attr>          
    </declare-styleable> 
    
    <activity  
           android:name = ".StyleAndThemeActivity"  
           android:label = "@string/app_name"  
           android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">  
           <intent-filter>  
                  <action android:name = "android.intent.action.MAIN" />  
                  <category android:name = "android.intent.category.LAUNCHER" />  
           </intent-filter>  
     </activity>  
    

    11.定义多种类型

    <declare-styleable name = "名称">  
           <attr name = "background" format = "reference|color" />  
    </declare-styleable>  
    
    <ImageView  
            android:layout_width = "42dip"  
            android:layout_height = "42dip"  
            android:background = "@drawable/图片ID|#00FF00"/>  
    

    后记

    这样子,以后我们写自定义View后,小伙伴们引用的时候就方便多啦!
    下面,补充一个小知识:
    wrap_content-> MeasureSpec.AT_MOST
    match_parent -> MeasureSpec.EXACTLY
    具体值 -> MeasureSpec.EXACTLY

    相关文章

      网友评论

        本文标题:自定义View-第十五步:封装控件-自定义控件属性

        本文链接:https://www.haomeiwen.com/subject/hpovittx.html