How to fit GIF file to screen size in android

你。 提交于 2019-12-06 07:12:17

问题


I am playing gif animation file using movie class, but could not fit within the screen.

How to scale or fit gif file to screen size ?

I have taken GIF view custom view and playing animation using Movie class.


回答1:


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



回答2:


Try this GifImageView

Set Height Width of Movie as screen Height 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();
    }

This method will be called when you call setGifImageResource() from Activity.

Activity will look like below :

@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);
    }

Again in our custom layout we need following :

Determine the measured width and height of view.

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

Important code while drawing on canvas.

@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();
        }
}

I have customized this view as our requirement.

check the link below.

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




回答3:


If you are using a canvas (@surface view) take its height and width in the gameloop and draw the 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 value of your display height. Same guese for width. Use those properties to stretch the gif.

However I must say this function is deprecated and there must be a better way, this will work just fine though :)




回答4:


You can use a frame by frame animation instead of animated gif to get similar end result and scaling. You need to extract each frame from the gif and place individual frames as png(or other supported format) files in the res/drawable folder. Create and use the animation drawable:

http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

To scale the animation, check this.

There are various scaleTypes you can use to scale your animation using this method.

I know this does not directly answers the question but it will give the desired end result.




回答5:


I would take a look over here. Full tutorial that covers 3 different ways to display/play GIF's. Apparently it's not very easy and there are still open issue reports with google. Good luck :)



来源:https://stackoverflow.com/questions/10587333/how-to-fit-gif-file-to-screen-size-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!