2

I have an imageview and a bunch of images, but the problem is i want to show all the images in the image view one after another and the images should change every second or every 0.2 seconds like a gif animation.

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="97dp"
    android:layout_marginTop="108dp"
    android:src="@drawable/ic_launcher" />



import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {


private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies

    mWebView = (WebView) findViewById(R.id.webView1);
    mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.loadUrl("http://www.google.com/"); //Set Home page
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
    mWebView.getSettings().setDefaultFontSize(12);//Set Font Size
    mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
    mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash
    mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback     on touch
    mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
  //mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls 

    mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
    String appCachePath =     getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
    mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
    mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
    mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)

    mWebView.setWebViewClient(new WebViewClient() {//Show Image/Page/HTML on Error
    public void onReceivedError(WebView view, int errorCode, String description,     String failingUrl) {//Show Image/Page/HTML on Error
    mWebView.loadUrl("http://www.google.com");//Show WebPage on Error, to show     Offline file, use ("file:///android_asset/error_404.jpg");
        }
    });


}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{


webview.loadUrl(url);
return true;
}
}

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
  {

 if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())

{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Apk
  • 153
  • 1
  • 2
  • 18

3 Answers3

8

Use below code.

Create Image array.

int[] imageArray = { R.drawable.progress_one, R.drawable.progress_two,
            R.drawable.progress_three, R.drawable.progress_four,
            R.drawable.progress_five, R.drawable.progress_six,
            R.drawable.progress_seven, R.drawable.progress_eight,
            R.drawable.progress_nine, R.drawable.progress_ten,
            R.drawable.progress_ele, R.drawable.progress_twe };

and then put this array inside handler.

Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                int i = 0;

                public void run() {
                    imageView1.setImageResource(imageArray[i]);
                    i++;
                    if (i > imageArray.length - 1) {
                        i = 0;
                    }
                    handler.postDelayed(this, 2000);
                }
            };
            handler.postDelayed(runnable, 2000);

This will change image after every 2 seconds. if you want 0.2 then replace 2000 with 200.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
0

Below is the link... You can implement this using View Pager concept

Link 1

Community
  • 1
  • 1
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
0

I think you may do something like this

public class ImageChangerThread extends Thread {

    private int  index = 0;
    private boolean mRun = false;

    long changeInterval;
    ArrayList<Drawable> images;
    ImageView mImageView;

    public ImageChangerThread(ImageView mImageView,ArrayList<Drawable> images, long changeInterval) {
        this.mImageView = mImageView;
        this.images = images;
        this.changeInterval = changeInterval;
    }

    @Override
    public void run() {
        while (mRun) {
            if (index >= images.size()) {
                index = 0;
            }

            synchronized(mImageView) {
                mImageView.setImageDrawable(images.get(index++));
            }

            sleep(changeInterval);
        }
    }

    public void startShow(){
        mRun = true;
    }

    public void stopShow() {
        mRun = false;
    }

}

or better is to implement ImageView subclass which implements runnable and do all of this in subclass of ImageView

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54