How to set GIF file to screen size in android

I play a gif-animation file using the movie class, but cannot fit into the screen.

How to scale or embed gif file in screen size?

I used GIF to view and view animations using the Movie class.

+3
source share
5 answers

You can do this with this code:

    float scaleWidth = (float) ((screenwidth / (1f*gifwidth)));//add 1f does the trick
    float scaleHeight = (float) ((screenheight / (1f*gifheight)));
    canvas.scale(scaleWidth, scaleHeight);
    mMovie.draw(canvas, 0, 0);//mMovie is my gif picture
+3
source

If you are using a canvas (@surface view), take its height and width in the gameloop and draw a background image with its values. If not,

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
float tmp = display.getHeight;

Now tmp will contain the height value of your display. Same for width. Use these properties to stretch the gif.

, , , , :)

+2

GifImageView

Widht.

   init()
    {
        WindowManager wManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wManager.getDefaultDisplay();
        float screenwidth = display.getWidth();
        float screenheight = display.getHeight();

        mWidth = (int) screenwidth;
        mHeight = (int) screenheight;

        //Then force invalidated the layout of this view.

        requestLayout();
    }

setGifImageResource() Activity.

:

@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myactivity);

        GifImageView gifImageView = (GifImageView) findViewById(R.id.GifImageView);
        gifImageView.setGifImageResource(R.drawable.my_gif);
    }

:

.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     
        setMeasuredDimension(mWidth, mHeight);
}

.

@Override
protected void onDraw(Canvas canvas) {

        //Other general code (included in given link)...

        if (mMovie != null) {

            //Other general code (included in given link)...

            canvas.scale((float) this.getWidth() / (float) mMovie.width(),
                    (float) this.getHeight() / (float) mMovie.height());
            mMovie.draw(canvas, 0, 0);          
            invalidate();
        }
}

.

.

https://github.com/NihalPandya/demoUpload/blob/master/GifImageView.java

+1

I would consider it here . A complete tutorial that covers 3 different ways to display / play GIFs. Apparently, this is not very simple, and there are still open reports of problems with Google. Good luck :)

0
source

All Articles