zoukankan      html  css  js  c++  java
  • Android自定义控件之自定义属性的添加 -(2)

    还是前面例子中的问题,如果想在xml中设置球的半径,应该怎么办?我们先了解下自定义属性的知识。

    一、属性文件中format

    首先我们要查看values目录下是否有attrs.xml,如果没有要创建一个。

    format可选项

    reference //引用 

    color 

    boolean

    dimension  /尺寸

    float

    integer

    string 

    fraction //百分数,如200%

    下面再自定义几个属性,在attrs.xml文件中,如下

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="BallView">
            <attr name="BallColor" format="color"/>
            <attr name="BallRadius"  format="float"/>
            <attr name="BallStartX" format="float"/>
            <attr name="BallStartY" format="float"/>
        </declare-styleable>
    </resources>
    

    其中BallView中用来在java代码中找到这些数据。

    布局文件中如下

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <chuiyuan.lsj.androidjava.utils.BallView2
            xmlns:ballview="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ballview:BallRadius="20"
            />
    
    </LinearLayout>
    

    注意BallView2中的xmlns,使用的是res-auto。

    二、示例

    接下来是BallView2的代码,构造函数是重点,我们获取定义的属性,获取方法后面通常会设置默认值,以免我们在xml文件中没有定义。获取属性使用  名字_属性  连接起来。TypedArray通常最后调用 recycle()方法,以保持以后使用属性时的一致性。 

    package chuiyuan.lsj.androidjava.utils;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    
    import chuiyuan.lsj.androidjava.R;
    
    /**
     * Created by lsj on 2015/9/26.e
     */
    public class BallView2  extends View{
        private float x ;
        private float y ;
        private float r  ;
        private int color ;
    
        public BallView2(Context context){
            super(context, null);
        }
        public BallView2(Context context, AttributeSet attrs){
            super(context, attrs);
            //得到自定义的属性
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BallView) ;
            //在xml没有定义这个属性时,使用默认值 30
            x = ta.getFloat(R.styleable.BallView_BallStartX,30) ;
            y = ta.getFloat(R.styleable.BallView_BallStartY,30);
            r = ta.getFloat(R.styleable.BallView_BallRadius,30);
            color = ta.getColor(R.styleable.BallView_BallColor, Color.GREEN);
    
            ta.recycle(); //一定要
        }
    
        @Override
        public void onDraw(Canvas canvas){
            Paint paint = new Paint() ;
            paint.setColor(color);
            canvas.drawCircle(x, y, r, paint);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event){
            x = event.getX();
            y = event.getY();
            invalidate(); //刷新
            return  true;  //成功
        }
    }
    

    结束 。。  

      

  • 相关阅读:
    C# bool? 逻辑运算
    C# yield return 用法与解析
    枚举器和迭代器
    C# 事件
    C# 索引器
    C# 实现单例模式的几种方法
    协变 和 逆变
    C# 结构体的特点
    装箱 和 拆箱
    继承之---对象用父类声明,用子类实例化
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4847493.html
Copyright © 2011-2022 走看看