android手势左右滑动

android滑动手势

忘了从哪学的了,感谢原作者写的很清楚


代码

package com.sl.quwei.ui.manage;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;
import com.sl.quwei.R;


/**
 * Created by IntelliJ IDEA.
 * User: wulong
 * Date: 11-10-29
 * Time: 上午6:23
 * To change this template use File | Settings | File Templates.
 */
public class AggTestActivity extends Activity implements OnGestureListener {

    private GestureDetector detector;
    private ViewFlipper flipper;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.agg_test);

        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);

        flipper.addView(addButtonByText("按钮"), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        detector = new GestureDetector(this);
    }

    public View addButtonByText(String text) {
        Button btn = new Button(this);
        btn.setText(text);
        return btn;
    }

    public View addTextByText(String text) {
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setGravity(1);
        return tv;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("Fling", "Activity onTouchEvent!");
        return this.detector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 监听滑动
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        // TODO Auto-generated method stub
        Log.i("Fling", "Fling Happened!");
        if (e1.getX() - e2.getX() > 120) {
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
            this.flipper.addView(addTextByText("文本框"), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            this.flipper.showNext();
            return true;
        } else if (e1.getX() - e2.getX() < -120) {
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
            this.flipper.showPrevious();
            return true;
        }
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

配置文件

agg_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <ViewFlipper android:id="@+id/ViewFlipper01"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
    </ViewFlipper>
</LinearLayout>

res\anim文件夹下放置配置文件push_left_in.xml、push_left_out.xml、push_right_in.xml、push_right_out.xml

写法:push_left_in.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="500" />
	<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
		android:duration="500" />
</set>

push_left_out.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="500" />
	<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
		android:duration="500" />
</set>

push_right_in.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="500" />
	<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
		android:duration="500" />
</set>

push_right_out.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="500" />
	<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
		android:duration="500" />
</set>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值