高仿微信主界面

测量需要icon显示的区域,需要在super.onMeasure(widthMeasureSpec, heightMeasureSpec);之后才得到控件的大小。getMeasuredWidth()、getMeasuredHeight()

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int iconWidth = Math.min(getMeasuredWidth() - getPaddingLeft()
            - getPaddingRight(), getMeasuredHeight() - getPaddingTop()
            - getPaddingBottom() - mTextBound.height());

    int left = getMeasuredWidth() / 2 - iconWidth / 2;
    int top = getMeasuredHeight() / 2 - (mTextBound.height() + iconWidth)
            / 2;
    mIconRect = new Rect(left, top, left + iconWidth, top + iconWidth);
}

防止系统回收:在view中同样也有类似activity的onSaveInstanceState和onRestoreInstanceState方法,用法也类似。

private static final String INSTANCE_STATUS = "instance_status";
private static final String STATUS_ALPHA = "status_alpha";
@Override
protected Parcelable onSaveInstanceState() {
    Bundle bundle = new Bundle();
    bundle.putParcelable(INSTANCE_STATUS, super.onSaveInstanceState());
    bundle.putFloat(STATUS_ALPHA, mAlpha);
    return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (state instanceof Bundle) {
        Bundle bundle = (Bundle) state;
        mAlpha = bundle.getFloat(STATUS_ALPHA);
        super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATUS));
        return;
    }
    super.onRestoreInstanceState(state);
}

需要说明下:在onSaveInstanceState中将super.onSaveInstanceState()保存到bundle中,在onRestoreInstanceState中判断是自己保存的之后再取出来,保持了原有的系统属性。


你可能感兴趣的:(高仿微信主界面)