Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage of Lollipop's PathParser #6

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "21.1.1"

defaultConfig {
minSdkVersion 17
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* Copyright 2013 Romain Guy
*
* 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 org.curiouscreature.android.roadtrip;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public final class AlternateStateView extends View {
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private SVGPath svgPath;
private int mSvgResource;

private float mPhase;
private float mFadeFactor;
private int mDuration;
private float mParallax = 1.0f;
private float mOffsetY;

private ObjectAnimator mSvgAnimator;

public AlternateStateView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public AlternateStateView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

mPaint.setStyle(Paint.Style.STROKE);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IntroView, defStyle, 0);
try {
if (a != null) {
mPaint.setStrokeWidth(a.getFloat(R.styleable.StateView_strokeWidth, 1.0f));
mPaint.setColor(a.getColor(R.styleable.StateView_strokeColor, 0xff000000));
mPhase = a.getFloat(R.styleable.StateView_phase, 1.0f);
mDuration = a.getInt(R.styleable.StateView_duration, 4000);
mFadeFactor = a.getFloat(R.styleable.StateView_fadeFactor, 10.0f);
}
} finally {
if (a != null) a.recycle();
}
}

private void updatePathsPhaseLocked() {
final int count = svgPath.pathList.size();
for (int i = 0; i < count; i++) {
// SvgHelper.SvgPath svgPath = mPaths.get(i);
final Path path = svgPath.pathList.get(i);
path.reset();
svgPath.measureList.get(i).getSegment(0.0f, svgPath.lengthList.get(i) * mPhase, path, true);
path.rLineTo(0.0f, 0.0f);
// svgPath.measure.getSegment(0.0f, svgPath.length * mPhase, svgPath.renderPath, true);
// Required only for Android 4.4 and earlier
// svgPath.renderPath.rLineTo(0.0f, 0.0f);
}
}

public float getParallax() {
return mParallax;
}

public void setParallax(float parallax) {
mParallax = parallax;
invalidate();
}

public float getPhase() {
return mPhase;
}

public void setPhase(float phase) {
mPhase = phase;
updatePathsPhaseLocked();
invalidate();
}

public int getSvgResource() {
return mSvgResource;
}

public void setSvgResource(int svgResource) {
mSvgResource = svgResource;
}

@Override
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
svgPath = PathParserHelper.loadPathList(getContext(), mSvgResource,
w - getPaddingLeft() - getPaddingRight(), h - getPaddingTop() - getPaddingBottom());
updatePathsPhaseLocked();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (svgPath == null) {
return;
}

canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop() + mOffsetY);
final int count = svgPath.pathList.size();
for (int i = 0; i < count; i++) {
final Path path = svgPath.pathList.get(i);
int alpha = (int) (Math.min(mPhase * mFadeFactor, 1.0f) * 255.0f);
mPaint.setAlpha(alpha);
canvas.drawPath(path, mPaint);
}
canvas.restore();
}

public void reveal(View scroller, int parentBottom) {
if (mSvgAnimator == null) {
mSvgAnimator = ObjectAnimator.ofFloat(this, "phase", 0.0f, 1.0f);
mSvgAnimator.setDuration(mDuration);
mSvgAnimator.start();
}

float previousOffset = mOffsetY;
mOffsetY = Math.min(0, scroller.getHeight() - (parentBottom - scroller.getScrollY()));
if (previousOffset != mOffsetY) invalidate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("ConstantConditions")
@SuppressWarnings({"ConstantConditions", "deprecation"})
public class MainActivity extends Activity {
private static final boolean ANIMATE_BACKGROUND = false;

Expand All @@ -49,17 +49,17 @@ private static class State {
}

private final State[] mStates = {
new State(R.color.az, R.raw.map_az, new int[] {
new State(R.color.az, R.array.map_az, new int[] {
R.drawable.photo_01_antelope,
R.drawable.photo_09_horseshoe,
R.drawable.photo_10_sky
}),
new State(R.color.ut, R.raw.map_ut, new int[] {
new State(R.color.ut, R.array.map_ut, new int[] {
R.drawable.photo_08_arches,
R.drawable.photo_03_bryce,
R.drawable.photo_04_powell,
}),
new State(R.color.ca, R.raw.map_ca, new int[] {
new State(R.color.ca, R.array.map_ca, new int[] {
R.drawable.photo_07_san_francisco,
R.drawable.photo_02_tahoe,
R.drawable.photo_05_sierra,
Expand Down Expand Up @@ -125,7 +125,7 @@ private void handleScroll(ViewGroup source, int top) {
View item = container.getChildAt(i);
View v = item.findViewById(R.id.state);
if (v != null && v.getGlobalVisibleRect(mTempRect)) {
((StateView) v).reveal(source, item.getBottom());
((AlternateStateView) v).reveal(source, item.getBottom());
}
}
}
Expand Down Expand Up @@ -162,9 +162,9 @@ private void removeOverdraw(View decorView, float alpha) {
}
if (alpha >= 1.0f && decorView.getBackground() != null) {
mWindowBackground = decorView.getBackground();
decorView.setBackground(null);
decorView.setBackgroundDrawable(null);
} else if (alpha < 1.0f && decorView.getBackground() == null) {
decorView.setBackground(mWindowBackground);
decorView.setBackgroundDrawable(mWindowBackground);
mWindowBackground = null;
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ private void addState(LayoutInflater inflater, LinearLayout container, final Sta
final int margin = getResources().getDimensionPixelSize(R.dimen.activity_peek_margin);

final View view = inflater.inflate(R.layout.item_state, container, false);
final StateView stateView = (StateView) view.findViewById(R.id.state);
final AlternateStateView stateView = (AlternateStateView) view.findViewById(R.id.state);
stateView.setSvgResource(state.map);
view.setBackgroundResource(state.background);

Expand Down Expand Up @@ -265,7 +265,7 @@ public void onScrollChanged(TrackingHorizontalScrollView source,

private void removeStateOverdraw(View stateView, State state, float alpha) {
if (alpha >= 1.0f && stateView.getBackground() != null) {
stateView.setBackground(null);
stateView.setBackgroundDrawable(null);
stateView.findViewById(R.id.state).setVisibility(View.INVISIBLE);
} else if (alpha < 1.0f && stateView.getBackground() == null) {
stateView.setBackgroundResource(state.background);
Expand Down
Loading