Returns a path with a given set of peeks in a 'randomly' hand drawn style - Android android.animation

Android examples for android.animation:Animation

Description

Returns a path with a given set of peeks in a 'randomly' hand drawn style

Demo Code


//package com.java2s;
import java.util.Random;
import android.graphics.Path;

public class Main {
    /**/*w  w  w . jav a  2  s  .  c  o  m*/
     * 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;
    }
}

Related Tutorials