1 packagecn.carbs.maxheightview;2
3 importandroid.content.Context;4 importandroid.content.res.TypedArray;5 importandroid.util.AttributeSet;6 importandroid.util.Log;7 importandroid.view.View;8 importandroid.view.WindowManager;9 importandroid.widget.FrameLayout;10
11 /**
12 * 先判断是否设定了mMaxHeight,如果设定了mMaxHeight,则直接使用mMaxHeight的值,13 * 如果没有设定mMaxHeight,则判断是否设定了mMaxRatio,如果设定了mMaxRatio的值14 * 则使用此值与屏幕高度的乘积作为最高高度15 *@authorRick.Wang16 *17 */
18 public class MaxHeightView extendsFrameLayout{19
20 private static final float DEFAULT_MAX_RATIO = 0.4f;21 private static final float DEFAULT_MAX_HEIGHT = -1f;22
23 privateContext mContext;24 private float mMaxHeight = DEFAULT_MAX_HEIGHT;//优先级高
25 private float mMaxRatio = DEFAULT_MAX_RATIO;//优先级低
26
27 publicMaxHeightView(Context context) {28 this(context, null);29 }30
31 publicMaxHeightView(Context context, AttributeSet attrs) {32 this(context, attrs, 0);33 }34
35 public MaxHeightView(Context context, AttributeSet attrs, intdefStyle) {36 super(context, attrs, defStyle);37 mContext =context;38
39 TypedArray a =context.obtainStyledAttributes(attrs, R.styleable.maxHeightView);40
41 final int count =a.getIndexCount();42 for (int i = 0; i < count; ++i) {43 int attr =a.getIndex(i);44 switch(attr) {45 caseR.styleable.maxHeightView_maxHeightRatio:46 mMaxRatio =a.getFloat(attr, DEFAULT_MAX_RATIO);47 break;48 caseR.styleable.maxHeightView_maxHeightDimen:49 mMaxHeight =a.getDimension(attr, DEFAULT_MAX_HEIGHT);50 break;51 }52 }53 a.recycle();54
55 if(mMaxHeight < 0){56 mMaxHeight = mMaxRatio * (float)getScreenHeight(mContext);57 }58 }59
60 @Override61 protected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {62
63 int heightMode =View.MeasureSpec.getMode(heightMeasureSpec);64 int heightSize =View.MeasureSpec.getSize(heightMeasureSpec);65
66 Log.d("wang", "onMeasure heightSize is " + heightSize + " | mMaxHeight is " +mMaxHeight);67
68 if(heightMode ==View.MeasureSpec.EXACTLY){69 Log.d("wang", "heightMode == View.MeasureSpec.EXACTLY");70 heightSize = heightSize <= mMaxHeight ? heightSize : (int)mMaxHeight;71 }72
73 if(heightMode ==View.MeasureSpec.UNSPECIFIED){74 Log.d("wang", "heightMode == View.MeasureSpec.UNSPECIFIED");75 heightSize = heightSize <= mMaxHeight ? heightSize : (int)mMaxHeight;76 }77 if(heightMode ==View.MeasureSpec.AT_MOST){78 Log.d("wang", "heightMode == View.MeasureSpec.AT_MOST");79 heightSize = heightSize <= mMaxHeight ? heightSize : (int)mMaxHeight;80 }81
82 int maxHeightMeasureSpec =View.MeasureSpec.makeMeasureSpec(heightSize, heightMode);83
84 super.onMeasure(widthMeasureSpec, maxHeightMeasureSpec);85 }86
87
88 /**
89 * 获取屏幕高度90 *@paramcontext91 */
92 public static intgetScreenHeight(Context context){93 WindowManager wm =(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);94 returnwm.getDefaultDisplay().getHeight();95 }96
97 /**
98 * 获取屏幕宽度99 *@paramcontext100 */
101 public static intgetScreenWidth(Context context){102 WindowManager wm =(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);103 returnwm.getDefaultDisplay().getWidth();104 }105
106 /**
107 * 获取屏幕大小108 *@paramcontext109 *@paramoutDimension110 */
111 public static void getScreenDimension(Context context, int[] outDimension){112 WindowManager wm =(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);113 outDimension[0] =wm.getDefaultDisplay().getWidth();114 outDimension[1] =wm.getDefaultDisplay().getHeight();115 }116
117 }