gpt4 book ai didi

android - 自动更改 ImageView 中的图像

转载 作者:太空狗 更新时间:2023-10-29 15:53:30 25 4
gpt4 key购买 nike

我有一个 ImageView 和一堆图像,但问题是我想一个接一个地显示 ImageView 中的所有图像,图像应该像 gif 动画一样每秒或每 0.2 秒变化一次。

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

最佳答案

使用下面的代码。

创建图像数组。

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,则将 2000 替换为 200。

关于android - 自动更改 ImageView 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com