Created
August 28, 2016 06:51
-
-
Save NightlyNexus/615e1f2c48ac255f07bc478b295da930 to your computer and use it in GitHub Desktop.
A quickly made Android View to show an American flag.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<merge xmlns:android="http://schemas.android.com/apk/res/android"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="7" | |
android:orientation="horizontal" | |
> | |
<com.example.emptytemplate.StarBannerView | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="2" | |
/> | |
<LinearLayout | |
android:orientation="vertical" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="3"> | |
<View | |
android:background="#F00" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#FFF" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#F00" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#FFF" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#F00" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#FFF" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
</LinearLayout> | |
</LinearLayout> | |
<View | |
android:background="#F00" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#FFF" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#F00" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#FFF" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#F00" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#FFF" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
<View | |
android:background="#F00" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
/> | |
</merge> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2016 Eric Cochran | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.os.Build; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.view.LayoutInflater; | |
import android.widget.LinearLayout; | |
import static android.view.View.MeasureSpec.EXACTLY; | |
import static android.view.View.MeasureSpec.UNSPECIFIED; | |
public final class FlagView extends LinearLayout { | |
public FlagView(Context context) { | |
super(context); | |
} | |
public FlagView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public FlagView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public FlagView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
{ | |
Context context = getContext(); | |
setOrientation(VERTICAL); | |
setGravity(Gravity.CENTER); | |
LayoutInflater.from(context).inflate(R.layout.flag_children, this, true); | |
} | |
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
float widthToHeightRatio = 1.9f; | |
int width; | |
int height; | |
if (widthMode == UNSPECIFIED && heightMode == UNSPECIFIED | |
|| widthMode != UNSPECIFIED && heightMode != UNSPECIFIED) { | |
if (widthSize / widthToHeightRatio > heightSize) { | |
// Limited by height. | |
width = (int) (heightSize * widthToHeightRatio); | |
height = heightSize; | |
} else { | |
// Limited by width. | |
width = widthSize; | |
height = (int) (widthSize / widthToHeightRatio); | |
} | |
} else if (widthMode == UNSPECIFIED) { | |
// Limited by height. | |
width = (int) (heightSize * widthToHeightRatio); | |
height = heightSize; | |
} else { | |
// Limited by width. | |
width = widthSize; | |
height = (int) (widthSize / widthToHeightRatio); | |
} | |
super.onMeasure(MeasureSpec.makeMeasureSpec(width, EXACTLY), | |
MeasureSpec.makeMeasureSpec(height, EXACTLY)); | |
if (widthMode == EXACTLY && heightMode == EXACTLY) { | |
setMeasuredDimension(widthSize, heightSize); | |
} else if (widthMode == EXACTLY) { | |
setMeasuredDimension(widthSize, getMeasuredHeight()); | |
} else if (heightMode == EXACTLY) { | |
setMeasuredDimension(getMeasuredWidth(), heightSize); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment