문제

I made a live wallpaper that show images from drawable directory when the phone is refreshed if an app is opened the image change when we lock the phone than unlock the image will changeand everything is fine this is my code

public class CustomWallpaper extends WallpaperService {

@Override
public Engine onCreateEngine() {
    return new WallpaperEngine();
}

class WallpaperEngine extends Engine {
    private int[] mImagesArray;
    private int mImagesArrayIndex = 0;
    private boolean mVisible = true;
    private String mImageScale = "Stretch to screen";
    private CustomWallpaperHelper customWallpaperHelper;

    public WallpaperEngine() {
        customWallpaperHelper = new CustomWallpaperHelper(getApplicationContext(), getResources());
        mImagesArray = new int[] {R.drawable.image_1,R.drawable.image_2,R.drawable.image_3,R.drawable.image_4,R.drawable.image_5,R.drawable.image_6,R.drawable.image_7,R.drawable.image_8,R.drawable.image_9,R.drawable.image_10,R.drawable.image_11,R.drawable.image_12,R.drawable.image_13,R.drawable.image_14,R.drawable.image_15,R.drawable.image_16,R.drawable.image_17,R.drawable.image_18,R.drawable.image_19,R.drawable.image_20,R.drawable.image_21,R.drawable.image_22,R.drawable.image_23,R.drawable.image_24,R.drawable.image_25,R.drawable.image_26,R.drawable.image_27,R.drawable.image_28,R.drawable.image_29,R.drawable.image_30,R.drawable.image_31,R.drawable.image_32};
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);

        if (visible && mVisible != visible) {
            drawFrame();
        }

        mVisible = visible;
    }

    private void incrementCounter() {
        mImagesArrayIndex++;

        if (mImagesArrayIndex >= mImagesArray.length) {
            mImagesArrayIndex = 0;
        }
    }

    private void drawFrame() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas canvas = null;

        try {
            canvas = holder.lockCanvas();

            if (canvas != null) {
                drawImage(canvas);
                incrementCounter();
            }
        } finally {
            if (canvas != null) {
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }

    private void drawImage(Canvas canvas) {
        //Get the image and resize it
        Bitmap image = BitmapFactory.decodeResource(getResources(),
                mImagesArray[mImagesArrayIndex]);

        //Draw background
        customWallpaperHelper.setBackground(canvas);

        //Scale the canvas
        PointF mScale = customWallpaperHelper.getCanvasScale(mImageScale, image.getWidth(), image.getHeight());
        canvas.scale(mScale.x, mScale.y);

        //Draw the image on screen
        Point mPos = customWallpaperHelper.getImagePos(mScale, image.getWidth(), image.getHeight());
        canvas.drawBitmap(image, mPos.x, mPos.y, null);
    }
}
}

my question is how can I show the images randomly

not like 1.2.3.4.5.6.7...

but for example 9.15.20.4.35.1...

도움이 되었습니까?

해결책 2

Change

private void incrementCounter()
{
    mImagesArrayIndex++;

    if (mImagesArrayIndex >= mImagesArray.length) 
    {
        mImagesArrayIndex = 0;
    }
}

to

private void incrementCounter() 
{
    mImagesArrayIndex = new Random().nextInt(mImagesArray.length)
}

This will give you a random index within the size of mImagesArray

다른 팁

No need of extra method to increment count and check.Just simply use a random number with between 0 and (size-1).

Random rand=new Random();



     private void drawImage(Canvas canvas) {
//rand.nextInt(n) Returns a pseudo-random uniformly distributed int in 
//the half-open range [0, n). 

            //Get the image and resize it
            Bitmap image = BitmapFactory.decodeResource(getResources(),
                    mImagesArray[rand.nextInt(mImagesArray.length)]);

            //Draw background
            customWallpaperHelper.setBackground(canvas);

            //Scale the canvas
            PointF mScale = customWallpaperHelper.getCanvasScale(mImageScale, image.getWidth(), image.getHeight());
            canvas.scale(mScale.x, mScale.y);

            //Draw the image on screen
            Point mPos = customWallpaperHelper.getImagePos(mScale, image.getWidth(), image.getHeight());
            canvas.drawBitmap(image, mPos.x, mPos.y, null);
        }

This may also help you..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top