Back to project page unknown-pleasures.
The source code is released under:
Creative Commons Attribution NonCommercial NoDerivs (CC-NC-ND) THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTE...
If you think the Android project unknown-pleasures listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * AnimatorUtil.java/*from w ww.j a v a 2s.c o m*/ * Author: marek.brodziak@gmail.com * Created: May 21, 2014 * Copyright 2014 by miniti */ package pl.miniti.android.pleasures.animation; import java.util.Random; import android.graphics.Path; /** * Utility class to be used by all the {@see Animator} implementations */ public class AnimatorUtil { /** * To make sure the handdrawn style is rather minor */ private static final float MAX = 0.008f; /** * Base resolution for calculating the 'random' values */ public static final float RESOLUTION = .02f; /** * Random number generator */ private static final Random randomizer = new Random( System.currentTimeMillis()); /** * Returns a path with a given set of peeks in a 'randomly' handrawn style * * @param peek_x * array of X locations of the peeks * @param peek_y * array of Y locations of the peeks * @return constructed path */ public static Path peeks(float[] peek_x, float[] peek_y, float offset) { final Path p = new Path(); p.moveTo(-offset, 0); float diff = 0; float cdiff = 0; int cp = 0; for (float x = -offset + RESOLUTION; x < 1 + offset; x += RESOLUTION) { if (x >= peek_x[cp]) { if (cp == peek_x.length - 1) { diff = 0; cdiff = 0; } else { diff = (peek_y[cp] - peek_y[cp + 1]) / ((peek_x[cp + 1] - peek_x[cp]) / RESOLUTION); if (diff == 0) { cdiff = 0; } ++cp; } } cdiff += diff; float to_y = cdiff + (randomizer.nextBoolean() ? 1 : -1) * randomizer.nextFloat() * (diff == 0 ? 0.003f : 0.01f); if (to_y < -MAX) { to_y = 0; } p.lineTo(x, -to_y); } p.lineTo(1 + offset, 0); return p; } /** * Returns a random array of peeks in a 'randomly' handrawn style * * @return array of y values */ public static float[] randomPeeks() { final float[] peeks = new float[(int) (1 / RESOLUTION) + 2]; peeks[0] = 0f; float diff = 0; float cdiff = 0; int counter = 1; for (float x = RESOLUTION; x < 1; x += RESOLUTION) { float to_y = cdiff + (randomizer.nextBoolean() ? 1 : -1) * randomizer.nextFloat() * (diff == 0 ? 0.003f : 0.01f); if (to_y < -MAX) { to_y = 0; } peeks[counter] = -to_y; ++counter; } peeks[counter] = 0f; return peeks; } }