Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Minimum and Maximum sizes for square layout #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Incorporating a minimum and maximum size for the square layout to kee…
…p images from getting too large on tablets.
  • Loading branch information
paulsmithkc committed Dec 31, 2020
commit fdc34de9c9cd019d31b3f8f91d75922a4dddb51c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.example.android.activityscenetransitionbasic;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.FrameLayout;

Expand All @@ -25,6 +27,8 @@
*/
public class SquareFrameLayout extends FrameLayout {

private int mMaxWidth, mMaxHeight;

public SquareFrameLayout(Context context) {
this(context, null);
}
Expand All @@ -35,6 +39,24 @@ public SquareFrameLayout(Context context, AttributeSet attrs) {

public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray a = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.SquareFrameLayout, 0, 0
);
try {
mMaxWidth = a.getDimensionPixelSize(R.styleable.SquareFrameLayout_maxWidth, Integer.MAX_VALUE);
mMaxHeight = a.getDimensionPixelSize(R.styleable.SquareFrameLayout_maxHeight, Integer.MAX_VALUE);
} finally {
a.recycle();
}
}

public int getMaximumWidth() {
return mMaxWidth;
}

public int getMaximumHeight() {
return mMaxHeight;
}

@Override
Expand All @@ -52,7 +74,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
return;
}

final int size;
int size;
if (widthSize == 0 || heightSize == 0) {
// If one of the dimensions has no restriction on size, set both dimensions to be the
// on that does
Expand All @@ -63,6 +85,17 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
size = Math.min(widthSize, heightSize);
}

// bound the size by the view's min and max dimensions
final int maxSize = Math.min(getMaximumWidth(), getMaximumHeight());
if (size > maxSize) {
size = maxSize;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
final int minSize = Math.max(getMinimumWidth(), getMinimumHeight());
if (size < minSize) {
size = minSize;
}
}

final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(newMeasureSpec, newMeasureSpec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-->

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand All @@ -26,7 +27,9 @@

<com.example.android.activityscenetransitionbasic.SquareFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:minHeight="100dp"
app:maxHeight="400dp">

<ImageView
android:id="@+id/imageview_header"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SquareFrameLayout">
<attr name="maxWidth" format="dimension" />
<attr name="maxHeight" format="dimension" />
</declare-styleable>
</resources>