AspectLayout.java
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.cnlive.demo.videoqn.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.widget.RelativeLayout;
import static com.cnlive.libs.lib_cnvideo.base.Util.getResolution;
/**
* Created by jerikc on 15/11/22.
*/
public class AspectLayout extends RelativeLayout {
private static final String TAG = "AspectLayout";
private int mWidthMeasureSpec;
private int mRootHeight = 0;
private int mRootWidth = 0;
public AspectLayout(Context context) {
super(context);
initialize(context);
}
public AspectLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public AspectLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
private void initialize(Context ctx) {
}
@TargetApi(21)
public AspectLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(TAG, "onMeasure" + " width=[" + MeasureSpec.toString(widthMeasureSpec) +
"] height=[" + MeasureSpec.toString(heightMeasureSpec) + "]");
Rect r = new Rect();
getWindowVisibleDisplayFrame(r);
Pair<Integer, Integer> screenSize = getResolution(getContext());
if (mRootWidth == 0 && mRootHeight == 0) {
mRootWidth = getRootView().getWidth();
mRootHeight = getRootView().getHeight();
}
int totalHeight = 0;
if (screenSize.first > screenSize.second) {
// land
totalHeight = mRootWidth > mRootHeight ? mRootHeight : mRootWidth;
} else {
// port
totalHeight = mRootWidth < mRootHeight ? mRootHeight : mRootWidth;
}
int nowHeight = r.bottom - r.top;
if (totalHeight - nowHeight > totalHeight / 4) {
// soft keyboard show
super.onMeasure(mWidthMeasureSpec, MeasureSpec.makeMeasureSpec(nowHeight + totalHeight - nowHeight, MeasureSpec.EXACTLY));
return;
} else {
// soft keyboard hide
}
mWidthMeasureSpec = widthMeasureSpec;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}