我制作了一个Android应用程序,在5秒后更改了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>
答案 0 :(得分:1)
而不是使用
imageView.setBackgroundResource(images[count++]);
试
imageView.setImageResource(images[count++]);
答案 1 :(得分:0)
我添加了imageView.setImageDrawable(null)并且它可以工作。