安卓开发中,小知识点:onConfigurationChanged方法未调用,onMeasure的使用

来源:互联网 发布:sql图书管理系统 编辑:程序博客网 时间:2024/05/22 04:54
1.只有在配置文件里做出如下配置,该方法才会执行,同时activity不会销毁和重建*   <activity android:name=".MainActivity"       android:configChanges="orientation|screenSize"
2.
onMeasure(int widthMeasureSpec, int heightMeasureSpec) 方法中
//1.当前view设置了精确值//2.当前view设置了matchparent并且父窗体设置了明确值//3.当前view设置了matchparent并且父控件设置了matchparent
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//1或者2或者3满足一种条件就认为有确切值也就是
widthMode = MeasureSpec.EXACTLY
示例代码如下:
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int HeightMode = MeasureSpec.getMode(heightMeasureSpec);    int width = MeasureSpec.getSize(widthMeasureSpec);    int height = MeasureSpec.getSize(heightMeasureSpec);    int measureWidth = 0, measureHeight = 0;    if(widthMode == MeasureSpec.EXACTLY) {        //1.当前view设置了精确值        //2.当前view设置了matchparent并且父窗体设置了明确值        //3.当前view设置了matchparent并且父控件设置了matchparent        //1或者2或者3满足一种条件就认为有确切值        Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                +"宽度有精确值 getWidth = "+getWidth());        measureWidth = width;    }else{        Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                +"宽度没有精确值,使用默认值");        measureWidth = SIZE;    }    if(HeightMode == MeasureSpec.EXACTLY) {        //        Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                +"高度有精确值 getHeight = "+getHeight());        measureHeight = height;    }else{        Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                +"高度没有精确值,使用默认值");        measureHeight = SIZE;    }    setMeasuredDimension(measureWidth,measureHeight);//至关重要,把最终测量到的view宽高设置到view上
}
另附上一个自定义圆圈,可变大小的demo
package com.example.admin.customtextview;import android.animation.PropertyValuesHolder;import android.animation.ValueAnimator;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Parcelable;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.util.Log;import android.view.View;/** * Created by admin on 2017/12/12. */public class CustomCircleAnim extends View {    private static final String TAG0 = " yyy ";    private static final String TAG = " CustomCircleAnim ";    private static final int SIZE = 15;//默认的大小    private static final int DEFAULT_COLOR = Color.BLUE;//圆圈的颜色    private int customViewradiuSize;//从布局里读到的圆圈半径大小    private int circleBgColor;//从布局里读到的圆圈背景颜色    private int mWidth;    private int mHeight;    private Paint p;    private float scale = 1f;    private ValueAnimator va;    public CustomCircleAnim(Context context) {        this(context,null);    }    public CustomCircleAnim(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }    public CustomCircleAnim(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.custom_circle_anim_style,defStyleAttr,R.style.AppTheme);        customViewradiuSize = typedArray.getDimensionPixelSize(R.styleable.custom_circle_anim_style_circle_size,SIZE);        circleBgColor = typedArray.getColor(R.styleable.custom_circle_anim_style_circle_bg_color,DEFAULT_COLOR);        Log.d(TAG0,TAG+" CustomCircleAnim(Context context, @Nullable AttributeSet attrs, int defStyleAttr)"                +"\n"+" customViewSize = "+customViewradiuSize+" circleBgColor = "+circleBgColor);        typedArray.recycle();        p = new Paint();        p.setColor(circleBgColor);        p.setAntiAlias(true);        p.setStrokeWidth(5);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Log.d(TAG0,TAG+" onDraw(Canvas canvas)"                +" customViewSize = "+customViewradiuSize+" circleBgColor = "+circleBgColor);        Log.d(TAG0,TAG+" onDraw() mWidth = "+getWidth()+" mHeight = "+getHeight());        canvas.drawCircle(mWidth/2,mHeight/2,customViewradiuSize*scale,p);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int HeightMode = MeasureSpec.getMode(heightMeasureSpec);        int width = MeasureSpec.getSize(widthMeasureSpec);        int height = MeasureSpec.getSize(heightMeasureSpec);        int measureWidth = 0, measureHeight = 0;        if(widthMode == MeasureSpec.EXACTLY) {            //1.当前view设置了精确值            //2.当前view设置了matchparent并且父窗体设置了明确值            //3.当前view设置了matchparent并且父控件设置了matchparent            //1或者2或者3满足一种条件就认为有确切值            Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                    +"宽度有精确值 getWidth = "+getWidth());            measureWidth = width;        }else{            Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                    +"宽度没有精确值,使用默认值");            measureWidth = SIZE;        }        if(HeightMode == MeasureSpec.EXACTLY) {            //            Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                    +"高度有精确值 getHeight = "+getHeight());            measureHeight = height;        }else{            Log.d(TAG0,TAG+" onMeasure(int widthMeasureSpec, int heightMeasureSpec)"                    +"高度没有精确值,使用默认值");            measureHeight = SIZE;        }        setMeasuredDimension(measureWidth,measureHeight);    }    @Override    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {        super.onLayout(changed, left, top, right, bottom);        mWidth = getWidth();        mHeight = getHeight();        //在此方法中才能拿到最终的宽高        Log.d(TAG0,TAG+" onLayout() mWidth = "+mWidth+" mHeight = "+mHeight);    }    public void startAnim() {        va = ValueAnimator.ofFloat(1,2);        va.setRepeatMode(ValueAnimator.REVERSE);        va.setRepeatCount(-1);        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                scale = (float) animation.getAnimatedValue();                Log.d(TAG0,TAG+" onAnimationUpdate  scale= "+scale);                postInvalidate();            }        });        va.setStartDelay(2000);        va.start();    }    @Override    protected void onDetachedFromWindow() {        super.onDetachedFromWindow();        Log.d(TAG0,TAG+" onDetachedFromWindow");        stopAnim();    }    public void stopAnim() {        if(va != null) {            va.cancel();            va = null;        }    }    @Nullable    @Override    protected Parcelable onSaveInstanceState() {        Log.d(TAG0,TAG+" onSaveInstanceState");        return super.onSaveInstanceState();    }    @Override    protected void onRestoreInstanceState(Parcelable state) {        Log.d(TAG0,TAG+" onRestoreInstanceState");        super.onRestoreInstanceState(state);    }}

阅读全文
0 0