标题Viewflipper
上代码
java文件
package com.example.mac.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity {
ViewFlipper v_flipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int images[] = {R.drawable.img2, R.drawable.img3, R.drawable.img4};
v_flipper = findViewById(R.id.v_flipper);
//for loop
/*for (int i= 0; i < image.length;i++){
flipperImages(images[i]);
}*/
//but I prefer foreach
for (int image: images)
flipperImages(image);
}
public void flipperImages(int image) {
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(image);
v_flipper.addView(imageView);
v_flipper.setFlipInterval(4000); //4 sec
v_flipper.setAutoStart(true);
//animation
v_flipper.setInAnimation(this,android.R.anim.slide_in_left);
v_flipper.setOutAnimation(this,android.R.anim.slide_in_left);
}
}
main.xml文件
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mac.myapplication.MainActivity">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:id="@+id/v_flipper"/>
</RelativeLayout>