Skip to content

Instantly share code, notes, and snippets.

@NightlyNexus
Created August 28, 2016 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NightlyNexus/615e1f2c48ac255f07bc478b295da930 to your computer and use it in GitHub Desktop.
Save NightlyNexus/615e1f2c48ac255f07bc478b295da930 to your computer and use it in GitHub Desktop.
A quickly made Android View to show an American flag.
<?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>
/*
* 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);
}
}
}
/*
* 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.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
public final class StarBannerView extends View {
private final Paint paint;
private final float[] points = new float[100];
{
paint = new Paint();
paint.setStrokeWidth(8);
paint.setColor(Color.WHITE);
}
public StarBannerView(Context context) {
super(context);
}
public StarBannerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StarBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public StarBannerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = canvas.getWidth();
int height = canvas.getHeight();
canvas.drawColor(Color.BLUE);
int row = 1;
int column = 1;
int rowSpaceCount = 10;
int columnSpaceCount = 12;
boolean insetRow = false;
for (int i = 0; i < 100; i += 2) {
points[i] = width * column / columnSpaceCount;
points[i + 1] = height * row / rowSpaceCount;
if (insetRow) {
if (column == 10) {
row++;
column = 1;
insetRow = false;
} else {
column += 2;
}
} else {
if (column == 11) {
row++;
column = 2;
insetRow = true;
} else {
column += 2;
}
}
}
canvas.drawPoints(points, paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment