初始化一支 畫筆Paint
init {
paint.color = Color.RED;//畫筆顏色
paint.style = Paint.Style.FILL;//畫筆實心? 即畫實心圓
}
在values目錄下新建一個 attrs.xml 存放自定義屬性
創(chuàng)建帶兩個參數(shù)的 構(gòu)造參數(shù)? 利用 AttributeSet獲取自定義的屬性:
constructor(context: Context, attributeSet: AttributeSet) :this(context) {
var a:TypedArray=context.obtainStyledAttributes(attributeSet,R.styleable.MyBall_attrs);
mWidth=a.getDimension(R.styleable.MyBall_attrs_width,100f ).toInt();
mHight=a.getDimension(R.styleable.MyBall_attrs_height,100f).toInt()
moveX=mWidth/2f;//moveX 圓心的移動坐標X
? ? moveY=mHight/2f;//moveY 圓心的移動坐標Y
? ? mRadius=mWidth/2f;//圓的半徑
}
調(diào)用 onDraw()繪制,onTouchEvent()監(jiān)聽移動
在布局文件中的使用? ? ?