android kotlin 自定义随手动的小球

2018-05-04  本文已影响0人  lhybxs

初始化一支 画笔Paint

init {

paint.color = Color.RED;//画笔颜色

paint.style = Paint.Style.FILL;//画笔实心  即画实心圆

}

在values目录下新建一个 attrs.xml 存放自定义属性

创建带两个参数的 构造参数  利用 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;//圆的半径

}

调用 onDraw()绘制,onTouchEvent()监听移动 在布局文件中的使用     
上一篇下一篇

猜你喜欢

热点阅读