手势滑动图像

时间:2014-07-30 10:05:13

标签: android animation gesture onfling

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您也可以使用视图鳍状肢,我在我的代码中使用它来在我的图库中滑动图像

public class Viewflip extends Activity {



private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

private ViewFlipper vf;
private Context mContext;
private final GestureDetector detector = new GestureDetector(new MyGestureDetector());

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
mContext = this;
vf = (ViewFlipper) this.findViewById(R.id.view_flipper);
vf.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(final View view, final MotionEvent event)
{
detector.onTouchEvent(event);
return true;
}


    addListenerOnButton();
}

}


public void addListenerOnButton() {
    // TODO Auto-generated method stub
Spinner mySpinner=(Spinner)findViewById(R.id.spinnergallery);
addListenerOnSpinnerItemSelection();
}
View addImageView(int resId)
{
ImageView iv = new ImageView(this);
iv.setImageResource(resId);

return iv;
}

class MyGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
try
{

// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
vf.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.in_from_left));
vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.out_to_left));
vf.showNext();
return true;
}
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) >SWIPE_THRESHOLD_VELOCITY)
{
vf.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.in_from_right));
vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.out_to_right));
vf.showPrevious();
return true;
}

}
catch (Exception e)
{
e.printStackTrace();
}

return false;
}
}
}

<强> in_from_left.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" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />

</set>

<强> in_from_right.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" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />

</set>

<强> out_to_left.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" />

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>

<强> out_to_right.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" />

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>