安卓开发中,小知识点:onConfigurationChanged方法未调用,onMeasure的使用
2017-12-12 16:07
417 查看
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设置了精确值<4000/span> //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); }}
相关文章推荐
- 屏幕旋转之后,ListView表格表头位置错乱 与onConfigurationChanged方法的使用【安卓布局】
- 自定义view中onMeasure、onLayout、onDraw、onFinishInflate、onSizeChanged方法调用时机
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
- Android中横竖屏切换时,onConfigurationChanged方法不被调用
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
- 安卓应用开发通过java调用c++ jni的图文使用方法
- Android中的onConfigurationChanged方法没有被调用的原因
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法实现边缘检测代码(2)
- 安卓开发之onMeasure的使用
- onConfigurationChanged方法的使用
- 安卓开发没有关闭界面就调用了onActivityResult方法?onActivityResult方法Intent-data总是null
- 针对横竖屏切换没有调用onConfigurationChanged方法
- Android中的onConfigurationChanged方法不能被调用
- 使用GPS定位为什么location总为空 而且onLocationChanged()方法没调用呀
- RadioGroup调用check(int)方法时,onCheckedChanged方法被执行两次
- 横竖屏切换问题(不使用onConfigurationChanged)
- Android 解决setRequestedOrientation之后手机屏幕的旋转不触发onConfigurationChanged方法
- Android 强制横屏或竖屏注意事项及onConfigurationChanged的使用
- 图解安卓-c++开发-通过java 调用c++ jni的使用
- Android 屏幕切换和onConfigurationChanged不调用的原因