[英]How to fit GIF file to screen size in android
I am playing gif animation file using movie class, but could not fit within the screen. 我正在使用电影类播放gif动画文件,但无法适应屏幕。
How to scale or fit gif file to screen size ? 如何将gif文件缩放或适合屏幕尺寸?
I have taken GIF view custom view and playing animation using Movie class. 我已经使用GIF视图自定义视图并使用Movie类播放动画。
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 试试这个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 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. 如果您使用画布(@surface视图),请在游戏循环中获取其高度和宽度,并使用其值绘制背景图像。 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. 现在tmp将包含显示高度的值。 Same guese for width. 同样的宽度猜测。 Use those properties to stretch the gif. 使用这些属性来拉伸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. 您可以使用逐帧动画而不是动画gif来获得类似的最终结果和缩放。 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. 您需要从gif中提取每个帧,并将单个帧作为png(或其他支持的格式)文件放在res / drawable文件夹中。 Create and use the animation drawable: 创建和使用动画drawable:
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html 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. 您可以使用此方法缩放动画,使用各种scaleType 。
I know this does not directly answers the question but it will give the desired end result. 我知道这并没有直接回答这个问题,但它会给出理想的最终结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.