package com.boeyu.test;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class WalxLayout extends FrameLayout implements View.OnLayoutChangeListener {
private Context mContext;
//private int mWrapMode;
private float mWrapScale;
private Boolean mLandscape;
private OnOrientationListener onOrientationListener;
public WalxLayout(@NonNull Context context) {
super(context);
init(context, null);
}
public WalxLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public WalxLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
mContext = context;
TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.WalxLayout);
mWrapScale = array.getFloat(R.styleable.WalxLayout_wrap_scale, 0);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mWrapScale > 0) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
print("onMeasure", widthSize, widthMode, heightSize, heightMode);
float parentScale = (float) widthSize / heightSize;
if (mWrapScale > parentScale) {
int height = (int) (widthSize / mWrapScale);
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
} else {
int width = (int) (heightSize * mWrapScale);
super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), heightMeasureSpec);
}
return;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
View parent = (View) getParent();
if (parent != null) {
parent.addOnLayoutChangeListener(this);
}
}
private static void print(String name, Object... values) {
StringBuilder builder = new StringBuilder();
for (Object value : values) {
builder.append(value).append(", ");
}
Log.i("WALX2020", builder.toString());
}
public void setOnOrientationListener(OnOrientationListener onOrientationListener) {
this.onOrientationListener = onOrientationListener;
}
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
int width = right - left - v.getPaddingLeft() - v.getPaddingRight();
int height = bottom - top - v.getPaddingTop() - v.getPaddingBottom();
boolean isLandscape = width > height;
if (mLandscape == null || mLandscape != isLandscape) {
mLandscape = isLandscape;
if (onOrientationListener != null) {
onOrientationListener.onOrientationChanged(this, mLandscape);
}
}
}
public interface OnOrientationListener {
void onOrientationChanged(View view, boolean isLandscape);
}
}
<declare-styleable name="WalxLayout">
<attr name="wrap_scale" format="float" />
</declare-styleable>
<com.boeyu.test.WalxLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:layout_gravity="center"
app:wrap_mode="all"
app:wrap_scale="1.6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="#80ff0000"
android:padding="10dp"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#8000ff00"
android:layout_weight="1"/>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#800000ff"
android:layout_weight="1"/>
</LinearLayout>
</com.boeyu.test.WalxLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
WalxLayout mWalxLayout = findViewById(R.id.mWalxLayout);
mWalxLayout.setOnOrientationListener(this);
}
@Override
public void onOrientationChanged(View view, boolean isLandscape) {
Toast.makeText(this, isLandscape ? "横屏" : "竖屏", Toast.LENGTH_SHORT).show();
}