我有这个自定义按钮,它不会自己绘制,也没有子视图。应用程序启动后,它看起来像这样:
在这一点上,我不知道哪些代码和详细信息可能与在此处发布相关。事实上,在应用程序更改状态后,按钮会检查是留下VISIBLE
还是离开INVISIBLE
。它仍然存在VISIBLE
。它调用setVisibility(View.VISIBLE)
,然后,当屏幕再次显示时,它看起来像这样:
如果我单击该按钮,它会恢复其原始背景尺寸。
到目前为止我做了什么
我已经将代码调试到android源。
第一 onDraw()
;我只打电话给那里super.onDraw();
,它似乎只处理文本而不是背景,如果是这样,它工作正常,因为文本仍然像以前一样定位和标注。
第二 onMeasure()
;我也只在这里打电话super.onMeasure();
;首次显示前调用数 (11) 次,之后调用 5 次setVisibility()
;单击按钮时根本不会调用它。
第三 onTouchEvent()
,当我点击按钮时调用。它设置不同的颜色背景ACTION_DOWN
并恢复原始颜色背景ACTION_UP
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG + " " + getText(), "+ onDraw(canvas:" + canvas + ")");
super.onDraw(canvas);
Log.d(TAG + " " + getText(), "- onDraw()");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(TAG + " " + getText(), String.format("+ onMeasure(widthMeasureSpec:%x, heightMeasureSpec:%x)", widthMeasureSpec,heightMeasureSpec));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d(TAG + " " + getText(), String.format("- onMeasure(): width=%d, hieght=%d", getMeasuredWidth(), getMeasuredHeight()));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG + " " + getText(), "+ onTouchEvent(event:" + event + ")");
super.onTouchEvent(event);
if( clickable ) {
if( event.getAction() == MotionEvent.ACTION_UP ) {
setBackgroundDrawable(normalBackground);
clickUp.soundPlay();
} else if( event.getAction() == MotionEvent.ACTION_DOWN ) {
setBackgroundDrawable(pressedBackground);
clickDown.soundPlay();
}
}
Log.d(TAG + " " + getText(), "- onTouchEvent()");
return true;
}
/**
* Sets the MyButton visible if stateFlags matches.<br>
* @param stateFlags The current app state.<br>
*/
public void setState(int stateFlags) {
Log.d(TAG + " " + getText(), "+ setState(stateFlags:" + stateFlags + ")");
if( state == stateFlags || state == State.NORMAL) {
setVisibility(View.VISIBLE);
Log.d(TAG + " " + getText(), "state(" + state + ") VISIBLE before was " + getVisibility());
} else {
setVisibility(View.INVISIBLE);
Log.d(TAG + " " + getText(), "state(" + state + ") INVISIBLE before was " + getVisibility());
}
requestLayout();
Log.d(TAG + " " + getText(), "- setState()");
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
Log.d(TAG + " " + getText(), "+ onCreateDrawableState(extraSpace:" + extraSpace + ")");
Log.d(TAG + " " + getText(), "- onCreateDrawableState()");
return super.onCreateDrawableState(extraSpace);
}
这些是按钮的日志:
*** Beggining - first show ***
+ onMeasure(widthMeasureSpec:800002b8, heightMeasureSpec:800003aa)
- onMeasure(): width=200, hieght=125
+ onMeasure(widthMeasureSpec:40000099, heightMeasureSpec:800003aa)
- onMeasure(): width=153, hieght=125
+ onMeasure(widthMeasureSpec:800002b8, heightMeasureSpec:80000152)
- onMeasure(): width=200, hieght=125
+ onMeasure(widthMeasureSpec:40000099, heightMeasureSpec:80000152)
- onMeasure(): width=153, hieght=125
+ onMeasure(widthMeasureSpec:800002b8, heightMeasureSpec:80000035)
- onMeasure(): width=200, hieght=53
+ onMeasure(widthMeasureSpec:40000099, heightMeasureSpec:80000035)
- onMeasure(): width=153, hieght=53
+ onSizeChanged(w:153, h:53, oldw:0, oldh:0)
- onSizeChanged()
+ onLayout(changed:true, left:12, top:3, right:165, bottom:56)
- onLayout()
+ onMeasure(widthMeasureSpec:800002b8, heightMeasureSpec:80000035)
- onMeasure(): width=200, hieght=53
+ onMeasure(widthMeasureSpec:40000099, heightMeasureSpec:80000035)
- onMeasure(): width=153, hieght=53
+ onLayout(changed:false, left:12, top:3, right:165, bottom:56)
- onLayout()
+ onDraw(canvas:android.view.Surface$CompatibleCanvas@b3e434b0)
+ onCreateDrawableState(extraSpace:0)
- onCreateDrawableState()
- onDraw()
*** App changes state - button shows wrong ****
+ setState(stateFlags:2)
state(1) VISIBLE before was 0
- setState()
+ onMeasure(widthMeasureSpec:800002b8, heightMeasureSpec:80000152)
- onMeasure(): width=200, hieght=125
+ onMeasure(widthMeasureSpec:800002b8, heightMeasureSpec:80000035)
- onMeasure(): width=200, hieght=53
+ onMeasure(widthMeasureSpec:40000099, heightMeasureSpec:80000035)
- onMeasure(): width=153, hieght=53
+ onLayout(changed:false, left:12, top:3, right:165, bottom:56)
- onLayout()
+ onDraw(canvas:android.view.Surface$CompatibleCanvas@b3e434b0)
- onDraw()
*** I am about to click the button, return to show fine ***
+ onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=61.0, y[0]=36.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2879242, downTime=2879242, deviceId=0, source=0x1002 })
- onTouchEvent()
+ onDraw(canvas:android.view.Surface$CompatibleCanvas@b3e434b0)
+ onCreateDrawableState(extraSpace:0)
- onCreateDrawableState()
- onDraw()
+ onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=61.0, y[0]=36.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2879342, downTime=2879242, deviceId=0, source=0x1002 })
- onTouchEvent()
+ performClick()
- performClick()
+ onDraw(canvas:android.view.Surface$CompatibleCanvas@b3e434b0)
+ onCreateDrawableState(extraSpace:0)
- onCreateDrawableState()
- onDraw()
如果认为相关,将按需发布更多详细信息。