onFinishInflate onSizeChanged onDraw 运行顺序

public class TestView extends View {


public TestView(Context context) {
super(context);
Log.d("mDebug", "TestView context");
}




public TestView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Log.d("mDebug", "TestView context,attrs,defStyle attrs="+attrs.getAttributeValue(0));
}






public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d("mDebug", "TestView context, attrs="+attrs.getAttributeValue(0));
}






@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Log.d("mDebug", "onDraw");
}


@Override
protected void onFinishInflate() {
// TODO Auto-generated method stub
super.onFinishInflate();
Log.d("mDebug", "onFinishInflate");
}






@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
Log.d("mDebug", "onSizeChanged,w="+w+",h="+h+",oldw="+oldw+",oldh="+oldh);
}


}



结果:

12-05 22:23:03.587: D/mDebug(9715): TestView context, attrs=@2131034112
12-05 22:23:03.597: D/mDebug(9715): onFinishInflate
12-05 22:23:03.667: D/mDebug(9715): onSizeChanged,w=240,h=282,oldw=0,oldh=0
12-05 22:23:03.727: D/mDebug(9715): onDraw
12-05 22:23:03.757: D/mDebug(9715): onDraw

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class JoystickControl(context: Context, attrs: AttributeSet?) : View(context, attrs) { private val paint = Paint(Paint.ANTI_ALIAS_FLAG) private var centerX = 0f private var centerY = 0f private var radius = 0f private var thumbX = 0f private var thumbY = 0f private var onJoystickChangeListener: OnJoystickChangeListener? = null interface OnJoystickChangeListener { fun onJoystickChanged(thumbX: Float, thumbY: Float) } fun setOnJoystickChangeListener(listener: OnJoystickChangeListener) { onJoystickChangeListener = listener } override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { centerX = w / 2f centerY = h / 2f radius = min(w, h) / 2f * 0.8f } override fun onDraw(canvas: Canvas) { paint.color = Color.GRAY canvas.drawCircle(centerX, centerY, radius, paint) paint.color = Color.RED val thumbRadius = radius / 5f canvas.drawCircle(thumbX + centerX, thumbY + centerY, thumbRadius, paint) } override fun onTouchEvent(event: MotionEvent): Boolean { when (event.action) { MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { val distance = sqrt((event.x - centerX) * (event.x - centerX) +(event.y - centerY) * (event.y - centerY)) if (distance < radius) { thumbX = event.x - centerX thumbY = event.y - centerY invalidate() onJoystickChangeListener?.onJoystickChanged(thumbX, thumbY) } else { val angle = atan2(event.y - centerY, event.x - centerX) thumbX = cos(angle) * radius thumbY = sin(angle) * radius invalidate() onJoystickChangeListener?.onJoystickChanged(thumbX, thumbY) } } MotionEvent.ACTION_UP -> { thumbX = 0f thumbY = 0f invalidate() onJoystickChangeListener?.onJoystickChanged(thumbX, thumbY) } } return true } }我想加入传递游戏手柄参数的代码
最新发布
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值