2017-01-02 56 views
0

我希望我的圖像視圖在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

嘗試調用'requestLayout' – deathangel908

+0

在runnable()內部,您評論語句「iv.postDelated(r,4000);」因此,從評論中刪除它可能會有所幫助。 –

+0

當我刪除該註釋行時,它顯示一個錯誤,說「變量r是從內部類聲明的,需要聲明爲最終的」,並且當我聲明它爲final時,它說它必須被初始化。 –

回答

0

從OnCreate中開始第一次()

public void onCreate(Bundle b){ 

handler.postDelayed(changeImage, 1000); 

}`

Handler handler = new Handler(); 

可運行changeImage =新的Runnable(){

@Override 
public void run(){ 
    if(flag>1) 
     handler.removeCallbacks(changeImage); 
    else{ 
     iv.setImageResource(v[flag++]); 
     handler.postDelayed(changeImage, 1000); 
    } 
} 

};

0

嘗試使用AsyncTask。它可以輕鬆地在線程中進行UI更改。這可能會幫助你。