安卓开发中,小知识点: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并且父控件设置了matchparentint 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上}另附上一个自定义圆圈,可变大小的demopackage 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
- 安卓开发中,小知识点:onConfigurationChanged方法未调用,onMeasure的使用
- 安卓开发之onMeasure的使用
- Android中onConfigurationChanged未被调用的解决方法
- 关于onConfigurationChanged以及onConfigurationChanged 未被调用的问题
- onConfigurationChanged未被调用的原因
- onConfigurationChanged未被调用的原因
- onConfigurationChanged未被调用的原因
- onConfigurationChanged 未调用不执行的问题
- onConfigurationChanged未被调用的原因
- onConfigurationChanged未被调用的原因
- 屏幕旋转之后,ListView表格表头位置错乱 与onConfigurationChanged方法的使用【安卓布局】
- 安卓开发中调用系统服务的方法
- 安卓的小知识点
- 【安卓开发小知识点二】
- 关于安卓View中onMeasure的详细分析
- onConfigurationChanged 未被 调用 onConfigurationChanged not getting called
- 关于安卓的小知识点
- Android中的onConfigurationChanged方法没有被调用的原因
- 内存数据库优劣势有那些?
- 【网络流二十四题 圆桌聚餐】【二分图多重匹配->最大流】
- Android 子view超出父View效果
- python基础之正则表达式和re模块
- JavaWeb Session过期验证
- 安卓开发中,小知识点:onConfigurationChanged方法未调用,onMeasure的使用
- 镜映自我与群体结构
- Capstone训练营5
- ConfigHelper.cs
- 【转】苹果开发者账号申请(2017最新版)
- 什么是数?什么是量?
- Twitch如何实现转码器比FFmepg性能提升65%?(上)
- 技术性能领先,阿里云网络产品全面升级为企业级
- 关于os模块中查看文件时间的几种用法