Return a random number between 0 and screen Height. - Android java.util

Android examples for java.util:Random

Description

Return a random number between 0 and screen Height.

Demo Code


//package com.java2s;

import android.content.Context;

import android.graphics.Point;

import android.view.WindowManager;

public class Main {
    /**/* w  ww .  j ava2 s  .c  o m*/
     * Return a random number between 0 and screenH.
     */
    public static int randomY(Context c) {
        return (int) (Math.random() * screenH(c));
    }

    /**
     * Return the screen height in pixel.
     */
    public static int screenH(Context c) {
        return screenSize(c).y;
    }

    /**
     * Return the screen size in pixel.
     */
    public static Point screenSize(Context c) {
        WindowManager wm = (WindowManager) c
                .getSystemService(Context.WINDOW_SERVICE);
        Point p = new Point(0, 0);
        wm.getDefaultDisplay().getSize(p);
        return p;
    }
}

Related Tutorials