如何在Android中玩GIF

如何在Android中玩GIF,第1张

概述你好stackoverflow我正在尝试开发一个 Android应用程序来播放我自己的GIF,这里是代码片段 MainActivity.java public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate 你好stackoverflow我正在尝试开发一个 Android应用程序来播放我自己的GIF,这里是代码片段

MainActivity.java

public class MainActivity extends Activity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);}}

AnimationVIEw.java

import java.io.ByteArrayOutputStream;import java.io.inputStream;import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.MovIE;import androID.util.AttributeSet;import androID.vIEw.VIEw;public class AnimationVIEw extends VIEw {private MovIE mMovIE;private long mMovIEStart;private static final boolean DECODE_STREAM = true;private int mDrawleftPos;private int mHeight,mWIDth;private static byte[] streamToBytes(inputStream is) {    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);    byte[] buffer = new byte[1024];    int len;    try {        while ((len = is.read(buffer)) >= 0) {            os.write(buffer,len);        }    } catch (java.io.IOException e) {    }    return os.toByteArray();}public AnimationVIEw(Context context,AttributeSet attrs) {    super(context,attrs);    setFocusable(true);    java.io.inputStream is;    is = context.getResources().openRawResource(R.drawable.scanning);    if (DECODE_STREAM) {        mMovIE = MovIE.decodeStream(is);    } else {        byte[] array = streamToBytes(is);        mMovIE = MovIE.decodeByteArray(array,array.length);    }}@OverrIDeprotected voID onMeasure( int wIDthMeasureSpec,int heightmeasureSpec ){       int p_top = this.getpaddingtop(),p_bottom = this.getpaddingBottom();    mWIDth = mMovIE.wIDth();    mHeight = mMovIE.height();    // Calculate new desired height    final int desiredHSpec = MeasureSpec.makeMeasureSpec( mHeight + p_top + p_bottom,MeasureSpec.EXACTLY );    setMeasuredDimension( wIDthMeasureSpec,desiredHSpec );    super.onMeasure( wIDthMeasureSpec,desiredHSpec );    // Update the draw left position    mDrawleftPos = Math.max( ( this.getWIDth() - mWIDth ) / 2,0) ;}@OverrIDepublic voID onDraw(Canvas canvas) {    long Now = androID.os.SystemClock.uptimeMillis();    if (mMovIEStart == 0) { // first time        mMovIEStart = Now;    }    if (mMovIE != null) {        int dur = mMovIE.duration();        if (dur == 0) {            dur = 3000;        }        int relTime = (int) ((Now - mMovIEStart) % dur);        // Log.d("","real time :: " +relTime);        mMovIE.setTime(relTime);        mMovIE.draw(canvas,mDrawleftPos,this.getpaddingtop());        invalIDate();    }}}

main.xml中

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:background="#FFFFFF"androID:orIEntation="vertical" ><com.example.androIDgifwork.AnimationVIEw                    androID:layout_wIDth="wrap_content"                    androID:layout_height="wrap_content"                    androID:layout_centerInParent="true" /></linearLayout>

的Manifest.xml

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"package="com.example.androIDgifwork"androID:versionCode="1"androID:versionname="1.0" ><uses-sdk    androID:minSdkVersion="8"    androID:targetSdkVersion="19" /><application    androID:icon="@drawable/ic_launcher"    androID:label="@string/app_name" >    <activity        androID:name="com.example.androIDgifwork.MainActivity"        androID:label="@string/app_name" >        <intent-filter>            <action androID:name="androID.intent.action.MAIN" />            <category androID:name="androID.intent.category.LAUNCHER" />        </intent-filter>    </activity></application>

当我运行上面的代码片段GIF不是一直在运行,但是当我删除androID:targetSdkVersion =“19”GIF运行,请帮我解决这个谜语.

谢谢

解决方法 2017更新答案
要在AndroID中玩GIF,请使用 Glide库加载任何图像或GIF.
GlIDe.with(context).load(YOUR_GIF).asgif().into(YOUR_IMAGE_VIEW);

使用GlIDe来加载正常图像,来自服务器的图像甚至加载GIF.还有看看Picasso androID图像加载库,它类似于GlIDe,但截至目前(2017年4月16日)Picasso不支持在AndroID中的GIF加载.

################################################## ####################

旧答案

对于所有想要在您的应用程序中播放GIF的用户,请查找以下代码
PlayGifVIEw.java

import androID.annotation.Suppresslint; import androID.content.Context; import androID.graphics.Canvas; import androID.graphics.MovIE; import androID.os.Build; import androID.util.AttributeSet; import androID.vIEw.VIEw; public class PlayGifVIEw extends VIEw{     private static final int DEFAulT_MOVIEW_DURATION = 1000;     private int mMovIEResourceID;     private MovIE mMovIE;     private long mMovIEStart = 0;     private int mCurrentAnimationTime = 0;     @Suppresslint("NewAPI")     public PlayGifVIEw(Context context,AttributeSet attrs) {         super(context,attrs);        /**         * Starting from HONEYCOMB have to turn off HarDWare acceleration to draw         * MovIE on Canvas.         */        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {            setLayerType(VIEw.LAYER_TYPE_SOFTWARE,null);        }    }     public voID setimageResource(int mvID){         this.mMovIEResourceID = mvID;    mMovIE = MovIE.decodeStream(getResources().openRawResource(mMovIEResourceID));    requestLayout();}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {    if(mMovIE != null){        setMeasuredDimension(mMovIE.wIDth(),mMovIE.height());    }else{        setMeasuredDimension(getSuggestedMinimumWIDth(),getSuggestedMinimumHeight());    }}@OverrIDeprotected voID onDraw(Canvas canvas) {    if (mMovIE != null){        updateAnimtionTime();        drawGif(canvas);        invalIDate();    }else{        drawGif(canvas);    }}private voID updateAnimtionTime() {    long Now = androID.os.SystemClock.uptimeMillis();    if (mMovIEStart == 0) {        mMovIEStart = Now;    }    int dur = mMovIE.duration();    if (dur == 0) {        dur = DEFAulT_MOVIEW_DURATION;    }    mCurrentAnimationTime = (int) ((Now - mMovIEStart) % dur);}private voID drawGif(Canvas canvas) {    mMovIE.setTime(mCurrentAnimationTime);    mMovIE.draw(canvas,0);    canvas.restore();}}

在您的活动类中,使用以下代码播放GIF

PlayGifVIEw pGif = (PlayGifVIEw) findVIEwByID(R.ID.vIEwGif);pGif.setimageResource(<Your GIF file name Eg: R.drawable.infinity_gif>);

XML布局

<yourPacckagename.PlayGifVIEw            androID:ID="@+ID/vIEwGif"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_gravity="center" />
总结

以上是内存溢出为你收集整理的如何在Android中玩GIF全部内容,希望文章能够帮你解决如何在Android中玩GIF所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1132273.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存