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.
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
Try this GifImageView
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();
}
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 :
@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();
}
}
I have customized this view as our requirement.
https://github.com/NihalPandya/demoUpload/blob/master/GifImageView.java
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 :)
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.
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 :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.