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

Android examples for java.util:Random

Description

Return a random number between 0 and screen Width.

Demo Code


//package com.java2s;

import android.content.Context;

import android.graphics.Point;

import android.view.WindowManager;

public class Main {
    /**//w  w w  .  j a  v a2  s.c  o m
     * Return a random number between 0 and screenW.
     */
    public static int randomX(Context c) {
        return (int) (Math.random() * screenW(c));
    }

    /**
     * Return the screen width in pixel.
     */
    public static int screenW(Context c) {
        return screenSize(c).x;
    }

    /**
     * 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