安卓学习UI组件-ViewFlipper-实现屏幕切换动画效果

安卓学习UI组件-ViewFlipper-实现屏幕切换动画效果_第1张图片

安卓学习UI组件-ViewFlipper-实现屏幕切换动画效果_第2张图片

安卓学习UI组件-ViewFlipper-实现屏幕切换动画效果_第3张图片安卓学习UI组件-ViewFlipper-实现屏幕切换动画效果_第4张图片

安卓学习UI组件-ViewFlipper-实现屏幕切换动画效果_第5张图片触屏滑动再滑滑

 

动画效果的XML文件放在 res目录下的anim目录下

in_leftright.xml从左往右进入屏幕的动画效果

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

out_leftright.xml从左往右出去屏幕的动画效果

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

in_rightleft.xml从右到左进入屏幕的动画效果

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

out_rightleft.xml从右到左出去屏幕的动画效果

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

 

布局文件

<?xml version="1.0" encoding="utf-8"?>
<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.administrator.myapplication.MainActivity">

<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/pic12" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/pic13" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/pic5" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/pic6" />

</LinearLayout>


</ViewFlipper>
</RelativeLayout>
 
Activity.java文件
package com.example.administrator.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

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

float startX=0.0f;
float endX=0.0f;
@Override
public boolean onTouchEvent(MotionEvent event) {
int action=event.getAction();

if(action==MotionEvent.ACTION_DOWN)
{
startX=event.getX();
return true;
}
else if(action==MotionEvent.ACTION_UP){
endX=event.getX();
if(startX-endX>20)//设置下一个
{
viewFlipper.setInAnimation(this,R.anim.in_rightleft);
viewFlipper.setOutAnimation(this, R.anim.out_rightleft);
viewFlipper.showNext();
}
else if(endX-startX>20)//previous
{
viewFlipper.setInAnimation(this,R.anim.in_leftright);
viewFlipper.setOutAnimation(this,R.anim.out_leftright);
viewFlipper.showPrevious();
}

}
return super.onTouchEvent(event);
}
}

你可能感兴趣的:(安卓学习UI组件-ViewFlipper-实现屏幕切换动画效果)