在imageview中更改图像

问题描述:

我制作了一个android应用程序,可在5秒内更改imageview中的图像。但图像叠加在创建imageview时选择的图像上。我不想要该图像。只是帮助,这里是代码在imageview中更改图像

MainActivity.java

public class MainActivity extends Activity { 
    Handler handler = new Handler(); 
    int count; 
    int images[] = { R.drawable.c1, 
      R.drawable.c12, 
      R.drawable.c3, 
      R.drawable.c4, 
      R.drawable.c5, 
      R.drawable.c6, 
      R.drawable.c7 }; 
    ImageView imageView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     imageView = (ImageView) findViewById(R.id.imageView1); 

     handler.postDelayed(changeImage, 5000); 
    } 
    Runnable changeImage = new Runnable() { 

     @Override 
     public void run() { 

      if (count >6) { 
       handler.removeCallbacks(changeImage); 
      } else { 

       if (count >= 6) 
        count = 0; 
       AlphaAnimation animation1 = new AlphaAnimation(0.5f, 1.0f); //here is a bit of animation for ya ;) 
       animation1.setDuration(5000); 
       animation1.setStartOffset(1300); //time for that color effect 
       animation1.setFillAfter(true); 
       imageView.startAnimation(animation1); 
       imageView.setBackgroundResource(images[count++]); 
       handler.postDelayed(changeImage, 5000); 
       } 
     } 
    }; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.change.MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="206dp" 
     android:layout_marginLeft="44dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

而不是使用

imageView.setBackgroundResource(images[count++]); 

尝试

imageView.setImageResource(images[count++]); 
+0

工作,thnxx哥们。 – Cr7

+0

非常好。谢谢。 – QuantumTiger

我加入imageView.setImageDrawable(空)和它的工作。