0

Hey I'm new to android programming. I want to know how I can easily implement a GIF in my Android app.

What I have to do?

I tried this, but I do something wrong. It's not working. >>THIS<<

1 Answers1

2

Basically i am giving you following example which loads image online from some web-site and change it each after some time duration.

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

and here is XML File.

<?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>

I think all link is dead but you can upload some image over there and change the link and then try.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • but I have no website were I load my gif. –  Feb 27 '14 at 08:05
  • You can also try to put images inside assets folder and then give the path of assets folder also. – InnocentKiller Feb 27 '14 at 08:06
  • Or else if you have gif image upload it here http://share.pho.to/ and then use the link. In my case also i did the same thing i don't have my web-site so i have uploaded it here and then i was just grabbing the link. – InnocentKiller Feb 27 '14 at 08:38