Abhi Jain
Abhi Jain

Reputation: 1

How to center GIF in Live Wallpaper

I am making a live wallpaper with a gif image that is put in the raw directory, how can I put the gif in the center of the screen, no matter what device I am using?? I understand why my code right now puts it in the left, because display.getWidth-display.getWidth =0, and same with height, hence (0,0) is in the top left. But what is center?! I cannot seem to figure it out for the life of me. This class is extending WallpaperService.

@Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);


        mScaleX = width / (1.5f*mNyan.width());
        mScaleY = height / (1.5f*mNyan.height());
        Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        mWidth = (display.getWidth()-display.getWidth());
        mHeight = (display.getHeight()-display.getHeight());





        nyan();
    }

and this is the end where I draw the canvas...

void nyanNyan(Canvas canvas) {

        canvas.save();
        canvas.scale(mScaleX, mScaleY);
        mNyan.setTime(mWhen);
        mNyan.draw(canvas, mWidth,mHeight);
        canvas.restore();
    }

Can anyone help!?! I'm stuck, and I can't move past this. I also tried, without any luck:

void nyanNyan(Canvas canvas) {

        canvas.save();
        canvas.scale(mScaleX, mScaleY);
        mNyan.setTime(mWhen);
        mNyan.draw(canvas, mCenterX - mNyan.width() , mCenterY - mNyan.height());
    }

Upvotes: 0

Views: 109

Answers (1)

Taig
Taig

Reputation: 7298

As I do not fully understand your code (e.g. where vars like mCenterX are coming from or how they are defined) I can not gurantee the code to run properly without further adjustments.

Assuming that you have access to nyan's width and height (I'll call it nyan.getWidth()) and the canvas' dimensions, then your last mNyan.draw() call should probably look like this:

mNyan.draw(
    canvas,
    ( canvas.getWidth() - nyan.getWidth() ) / 2,
    ( canvas.getHeight() - nyan.getHeight() ) / 2
);

Upvotes: 1

Related Questions