Created
February 20, 2017 07:06
-
-
Save yaraki/265f0eb0ca3b8019cc77d525433e225e to your computer and use it in GitHub Desktop.
Android View that shows a dummy checkered pattern
This file contains hidden or 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) 2017 The Android Open Source Project | |
* | |
* 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. | |
*/ | |
package com.example.android.pictureinpicture; | |
import android.animation.TimeAnimator; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.util.AttributeSet; | |
import android.view.View; | |
public class CheckerView extends View { | |
private TimeAnimator mAnimator; | |
private long mTotalTime; | |
private Paint mPaint; | |
private int mColorLight; | |
private int mColorDark; | |
private int mColorFloating; | |
private int mCheckerSize; | |
private final TimeAnimator.TimeListener mTimeListener = new TimeAnimator.TimeListener() { | |
@Override | |
public void onTimeUpdate(TimeAnimator animator, long totalTime, long deltaTime) { | |
mTotalTime = totalTime; | |
invalidate(); | |
} | |
}; | |
public CheckerView(Context context) { | |
this(context, null); | |
} | |
public CheckerView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public CheckerView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
mPaint = new Paint(); | |
mColorLight = context.getColor(R.color.checker_light); | |
mColorDark = context.getColor(R.color.checker_dark); | |
mColorFloating = context.getColor(R.color.checker_floating); | |
mCheckerSize = context.getResources().getDimensionPixelSize(R.dimen.checker_size); | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
start(); | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
stop(); | |
super.onDetachedFromWindow(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
final float offset = mTotalTime * 0.1f; | |
final int width = canvas.getWidth(); | |
final int height = canvas.getHeight(); | |
// Draw checkered pattern | |
final float startLeft = (offset % mCheckerSize) - mCheckerSize; | |
final float startTop = (-offset % mCheckerSize) - mCheckerSize; | |
boolean startLight = true; | |
for (float left = startLeft; left < width; left += mCheckerSize) { | |
boolean light = startLight; | |
for (float top = startTop; top < height; top += mCheckerSize) { | |
mPaint.setColor(light ? mColorLight : mColorDark); | |
canvas.drawRect(left, top, left + mCheckerSize, top + mCheckerSize, mPaint); | |
light = !light; | |
} | |
startLight = !startLight; | |
} | |
// Draw a floating box | |
final float absLeft = offset * 1.5f; | |
final float horizontal = width - mCheckerSize; | |
float left = absLeft % horizontal; | |
if ((int) ((absLeft / horizontal) % 2) == 1) { | |
left = horizontal - left; | |
} | |
final float absTop = offset * 1.3f; | |
final float vertical = height - mCheckerSize; | |
float top = absTop % vertical; | |
if ((int) ((absTop / vertical) % 2) == 1) { | |
top = vertical - top; | |
} | |
mPaint.setColor(mColorFloating); | |
canvas.drawRect(left, top, left + mCheckerSize, top + mCheckerSize, mPaint); | |
} | |
public void start() { | |
if (mAnimator == null) { | |
mAnimator = new TimeAnimator(); | |
mAnimator.setTimeListener(mTimeListener); | |
mAnimator.start(); | |
} | |
} | |
public void stop() { | |
if (mAnimator != null) { | |
mAnimator.end(); | |
mAnimator = null; | |
} | |
} | |
} |
This file contains hidden or 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"?> | |
<!-- | |
Copyright 2017 The Android Open Source Project | |
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. | |
--> | |
<resources> | |
<color name="checker_light">#9E9E9E</color> | |
<color name="checker_dark">#616161</color> | |
<color name="checker_floating">#FFEB3B</color> | |
</resources> |
This file contains hidden or 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"?> | |
<!-- | |
Copyright 2017 The Android Open Source Project | |
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. | |
--> | |
<resources> | |
<dimen name="checker_size">64dp</dimen> | |
</resources> |
This file contains hidden or 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) 2017 The Android Open Source Project | |
* | |
* 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. | |
*/ | |
package com.example.android.pictureinpicture; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.FrameLayout; | |
public class DummyVideoView extends FrameLayout { | |
private static final float ASPECT_RATIO = 9.f / 16.f; | |
private CheckerView mCheckerView; | |
public DummyVideoView(Context context) { | |
super(context, null); | |
} | |
public DummyVideoView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public DummyVideoView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
inflate(context, R.layout.view_dummy_video, this); | |
mCheckerView = (CheckerView) findViewById(R.id.checker); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
final int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
final int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
final int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
final int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) { | |
super.onMeasure(widthMeasureSpec, | |
MeasureSpec.makeMeasureSpec((int) (widthSize * ASPECT_RATIO), | |
MeasureSpec.EXACTLY)); | |
} else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) { | |
super.onMeasure(MeasureSpec.makeMeasureSpec((int) (heightSize / ASPECT_RATIO), | |
MeasureSpec.EXACTLY), heightMeasureSpec); | |
} else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) { | |
final int height = (int) (widthSize * ASPECT_RATIO); | |
if (height < heightSize) { | |
super.onMeasure(widthMeasureSpec, | |
MeasureSpec.makeMeasureSpec((int) (widthSize * ASPECT_RATIO), | |
MeasureSpec.EXACTLY)); | |
} else { | |
super.onMeasure(MeasureSpec.makeMeasureSpec((int) (heightSize / ASPECT_RATIO), | |
MeasureSpec.EXACTLY), heightMeasureSpec); | |
} | |
} else { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} | |
public void start() { | |
mCheckerView.start(); | |
} | |
public void stop() { | |
mCheckerView.stop(); | |
} | |
} |
This file contains hidden or 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"?> | |
<!-- | |
Copyright 2017 The Android Open Source Project | |
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. | |
--> | |
<merge xmlns:android="http://schemas.android.com/apk/res/android"> | |
<com.example.android.pictureinpicture.CheckerView | |
android:id="@+id/checker" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="bottom" | |
android:orientation="horizontal" | |
android:padding="8dp"> | |
<Space | |
android:layout_width="0dp" | |
android:layout_height="1dp" | |
android:layout_weight="1"/> | |
<ImageButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="?android:attr/selectableItemBackgroundBorderless" | |
android:src="@drawable/ic_picture_in_picture_alt"/> | |
</LinearLayout> | |
</merge> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment