android kotlin 自定義隨手動的小球

初始化一支 畫筆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)聽移動
在布局文件中的使用? ? ?
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容