如何在我的Android应用程序中实现GIF?

时间:2014-02-27 07:56:53

标签: java android animation gif

嘿,我是Android编程的新手。 我想知道如何在Android应用中轻松实现GIF。

我必须做什么?

我试过这个,但我做错了。它不起作用。 >>THIS<<

1 个答案:

答案 0 :(得分:2)

基本上我给你的例子是从一些网站在线加载图片,并在一段时间后再改变它。

public class Gif_First extends Activity {

    WebView mWebView;
    int i = 0;
    CountDownTimer mTimer;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.animation);

        mTimer = new CountDownTimer(15000, 1000) {
            private String[] myArray = {
                    "http://i.share.pho.to/09d99399_o.gif",
                    "http://i.share.pho.to/220dd194_o.gif",
                    "http://i.share.pho.to/6f3a5ae4_o.gif",
                    "http://i.share.pho.to/1b264cb7_o.gif",
                    "http://i.share.pho.to/824b1aab_o.gif",
                    "http://i.share.pho.to/c6728fb8_o.gif",
                    "http://i.share.pho.to/5508a2e1_o.gif",
                    "http://i.share.pho.to/aa64f9a9_o.gif",
                    "http://i.share.pho.to/d4619e93_o.gif",
                    "http://i.share.pho.to/2d50de0a_o.gif",
                    "http://i.share.pho.to/23c030eb_o.gif",
                    "http://i.share.pho.to/d6e3ccad_o.gif",
                    "http://i.share.pho.to/eaaeaf88_o.gif",
                    "http://i.share.pho.to/9708dd88_o.gif",
                    "http://i.share.pho.to/7f879974_o.gif",
                    "http://i.share.pho.to/efe8afa3_o.gif",
                    "http://i.share.pho.to/259954ac_o.gif",
                    "http://i.share.pho.to/bdbc28f2_o.gif" };
            int currentIndex = 0;

            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                if (currentIndex < myArray.length) {
                    mWebView.loadUrl(myArray[currentIndex]);
                    currentIndex++;
                } else {
                    currentIndex = 0;
                    if (currentIndex < myArray.length)
                        mWebView.loadUrl(myArray[currentIndex]);
                    currentIndex++;
                    mTimer.start();
                }
                mTimer.start();
            }
            // code comment end
        };

        mTimer.start();
        mWebView = (WebView) findViewById(R.id.webviewActionView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://i.share.pho.to/49cf97de_o.gif");
        mWebView.setWebViewClient(new WebSliderWebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(mWebView, url);
                //Toast.makeText(getApplicationContext(), "Done!",
                    //  Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                //Toast.makeText(getApplicationContext(),
                    //  "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class WebSliderWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

这是XML文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DogView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webviewActionView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我认为所有链接都已死,但您可以在那里上传一些图片并更改链接,然后尝试。