Android 成长笔记

Android ViewFlipper 使用示例

2017-03-21  本文已影响44人  赵者也

自定义动画资源 res\anim\slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"
        />
</set>

自定义动画资源 res\anim\slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        android:duration="@android:integer/config_mediumAnimTime"
        />
</set>

主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGray"
    android:orientation="vertical"
    >

    <ViewFlipper
        android:id="@+id/details"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:flipInterval="1000"
        >

        <ImageView
            android:src="@drawable/girl03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:src="@drawable/dog_003"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:src="@drawable/img03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ViewFlipper>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/prev"
        android:onClick="prev"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:onClick="auto"
        android:text="@string/auto"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:onClick="next"
        android:text="@string/next"
        />

</RelativeLayout>

主程序文件内容:

package com.toby.personal.testlistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";

    private ViewFlipper viewFlipper = null;

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

        viewFlipper = (ViewFlipper) findViewById(R.id.details);
    }

    public void prev(View view) {
        viewFlipper.stopFlipping();
        viewFlipper.setInAnimation(this, R.anim.slide_in_right);
        viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
        viewFlipper.showPrevious();
    }

    public void next(View view) {
        viewFlipper.stopFlipping();
        viewFlipper.setInAnimation(this, android.R.anim.slide_in_left);
        viewFlipper.setOutAnimation(this, android.R.anim.slide_out_right);
        viewFlipper.showNext();
    }

    public void auto(View view) {
        viewFlipper.setInAnimation(this, android.R.anim.slide_in_left);
        viewFlipper.setOutAnimation(this, android.R.anim.slide_out_right);
        viewFlipper.startFlipping();
    }
}

本示例运行效果图:

ViewFlipper 示例

参考文献:《疯狂Android讲义(第2版)》

上一篇下一篇

猜你喜欢

热点阅读