美文网首页
imageSwitcher

imageSwitcher

作者: _弓长_大人 | 来源:发表于2018-09-25 12:43 被阅读10次

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

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-100%p" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />

</set>

slide_in_right
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_left
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_ringht

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

相关文章

网友评论

      本文标题:imageSwitcher

      本文链接:https://www.haomeiwen.com/subject/ikcukftx.html