imageSwitcher

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

ImageSwitcher imageSwitcher;

// ImageView imageView;

int[] images = {R.drawable.timg_1,R.drawable.timg_2,R.drawable.timg_3,R.drawable.timg_4,R.drawable.timg_5,R.drawable.timg_6,R.drawable.timg_7,R.drawable.timg_8};
int index = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageSwitcher = findViewById(R.id.imageSwitcher);

// imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
// @Override
// public View makeView() {
// ImageView imageView = new ImageView(MainActivity.this);
// Log.d(TAG, "makeView: ");
// return imageView;
// }
// });

    imageSwitcher.setImageResource(R.drawable.timg_1);

}

public void prev(View v){
    index--;
    if (index < 0) index = images.length - 1;

    Animation in = AnimationUtils.loadAnimation(this,R.anim.slide_in_left);
    Animation out = AnimationUtils.loadAnimation(this,R.anim.slide_out_right);

    imageSwitcher.setInAnimation(in);
    imageSwitcher.setOutAnimation(out);

    imageSwitcher.setImageResource(images[index]);
}

public void next(View v){
    index++;
    if (index > images.length - 1) index = 0;

    Animation in = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);
    Animation out = AnimationUtils.loadAnimation(this,R.anim.slide_out_left);

    imageSwitcher.setInAnimation(in);
    imageSwitcher.setOutAnimation(out);

    imageSwitcher.setImageResource(images[index]);
}

}

slide_in_left


android:duration="@android:integer/config_mediumAnimTime"/>
android:duration="@android:integer/config_mediumAnimTime" />

slide_in_right

android:duration="@android:integer/config_mediumAnimTime"/>
android:duration="@android:integer/config_mediumAnimTime" />

slide_out_left

android:duration="@android:integer/config_mediumAnimTime"/>
android:duration="@android:integer/config_mediumAnimTime" />

slide_out_ringht


android:duration="@android:integer/config_mediumAnimTime"/>
android:duration="@android:integer/config_mediumAnimTime" />

你可能感兴趣的:(imageSwitcher)