在imageview中自动更改图像

时间:2014-02-07 10:28:08

标签: android imageview

我有一个imageview和一堆图像,但问题是我想一个接一个地显示图像视图中的所有图像,图像应该每隔一秒或每0.2秒更改一次,就像gif动画一样。

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

3 个答案:

答案 0 :(得分:8)

使用以下代码。

创建图像数组。

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

然后将此数组放在处理程序中。

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

这将在每2秒后更改图像。如果你想要0.2然后用200替换2000。

答案 1 :(得分:0)

以下是链接...您可以使用View Pager概念

来实现此目的

Link 1

答案 2 :(得分:0)

我想你可能会做这样的事情

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

}

或者更好的是实现实现runnable的ImageView子类,并在ImageView的子类中完成所有这些操作