我希望我的图片视图会在4秒内定期更改,我跟着this link但代码不能正常工作。
图像视图出现在模拟器中,但图像不会间歇性地更改。我附加了我的Java代码和XML文件。
/* Java Code*/
package com.serverphone.healthyfoods;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class Menu1 extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
ImageView iv;
TextView tv;
int i = 0;
int imageArray[] = {R.drawable.chicken_curry1,R.drawable.chicken_biriyani,R.drawable.paneer_butter};
String textArray[] = {"Chicken curry","Chicken Biriyani","Paneer Butter Masala"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Main Menu");
tv=(TextView)findViewById(R.id.info_text);
iv=(ImageView)findViewById(R.id.image);
Runnable r = new Runnable() {
@Override
public void run() {
iv.setImageResource(imageArray[i]);
i++;
if(i >= imageArray.length)
i=0;
//iv.postDelayed(r,4000);
}
};
iv.postDelayed(r,4000);
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.iov) {
// Handle the camera action
Intent i=new Intent(Menu1.this,InstantOrderVeg.class);
startActivity(i);
} else if (id == R.id.ionv) {
Intent i=new Intent(Menu1.this,InstantOrderNveg.class);
startActivity(i);
} else if (id == R.id.alaca) {
Intent i=new Intent(Menu1.this,AlaCarte.class);
startActivity(i);
} else if (id == R.id.cater) {
Intent i=new Intent(Menu1.this,Catering.class);
startActivity(i);
} else if (id == R.id.cont) {
Intent i=new Intent(Menu1.this,ContactUs.class);
startActivity(i);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
<!-- XML file-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_menu1" tools:context="com.serverphone.healthyfoods.Menu1">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
card_view:cardElevation="10dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#E8EAF6"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="\t\t\t\t\t\t\t\t\t\tChicken Curry"
android:textSize="30dp"
android:textStyle="italic"
/>
<ImageView
android:id="@+id/image"
android:layout_below="@id/info_text"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:scaleType="fitXY"
android:src="@drawable/chicken_curry1"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
答案 0 :(得分:0)
从oncreate()开始第一次
public void onCreate(Bundle b){
handler.postDelayed(changeImage, 1000);
}`
Handler handler = new Handler();
Runnable changeImage = new Runnable(){
@Override
public void run(){
if(flag>1)
handler.removeCallbacks(changeImage);
else{
iv.setImageResource(v[flag++]);
handler.postDelayed(changeImage, 1000);
}
}
};
答案 1 :(得分:0)
尝试使用AsyncTask。它允许轻松地在线程中进行UI更改。这可能对你有帮助。