今天给大家实现的功能是类似于ViewFlipper的图片滑动的效果,供大家参考,具体内容如下
现在就直接上代码吧!
代码实例:
1、xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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.zking.laci.android20_shou.MainActivity">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vf_main_image"
></ViewFlipper>
</LinearLayout>
2、activity类
package com.zking.laci.android20_shou;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity{
private ViewFlipper vf_main_image;
private int images[]={R.drawable.s10,R.drawable.s1,R.drawable.s7};
private GestureDetector ges;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到控件
vf_main_image = (ViewFlipper) findViewById(R.id.vf_main_image);
for (int i = 0; i < images.length; i++) {
ImageView iv=new ImageView(this);
iv.setImageResource(images[i]);
//将图片防区ViewFlipper中
vf_main_image.addView(iv);
}
//实例化一个手势检测器的类
ges = new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
/**
* 按下
*/
public boolean onDown(MotionEvent e) {
return false;
}
/**
* 按下但是没有抬起
* @param e
*/
@Override
public void onShowPress(MotionEvent e) {
}
/**
* 按一下 短按
* @param e
* @return
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
/**
* 滑动
* @param e1
* @param e2
* @param distanceX
* @param distanceY
* @return
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
/**
* 长按
* @param e
*/
@Override
public void onLongPress(MotionEvent e) {
}
/**
* 拖动
* @param e1 起点
* @param e2 终点
* @param velocityX x值
* @param velocityY Y值
* @return
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getX()-e2.getX()>200){
//显示下一张
vf_main_image.showNext();
//设置出去的动画
vf_main_image.setOutAnimation(MainActivity.this,R.anim.left_out);
//设置进来的动画
vf_main_image.setInAnimation(MainActivity.this,R.anim.right_in);
}else if(e2.getX()-e1.getX()>200){
//显示上一张
vf_main_image.showPrevious();
//设置出去的动画
vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_out);
//设置进来的动画
vf_main_image.setInAnimation(MainActivity.this,R.anim.left_in);
}
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//让触摸继续传递下去
return ges.onTouchEvent(event);
}
}
3、进入和出去的四个动画
left_out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
>
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:fromYDelta="0"
android:toYDelta="-100%p"
></translate>
</set>
right_in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
>
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:fromYDelta="-100%p"
android:toYDelta="0"
></translate>
</set>
right_out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
>
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:fromYDelta="0"
android:toYDelta="-100%p"
></translate>
</set>
left_in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
>
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:fromYDelta="-100%p"
android:toYDelta="0"
></translate>
</set>