【问题标题】:slide images in gesture以手势滑动图像
【发布时间】:2014-07-30 10:05:12
【问题描述】:

我想创建这样的应用程序 http://www.edumobile.org/android/wp-content/uploads/2012/08/pathfinderexample2.png http://www.edumobile.org/android/wp-content/uploads/2012/08/pathfinderexample3.png 可以创建手势。在从左到右和从右到左的幻灯片图像中。

【问题讨论】:

标签: android animation gesture onfling


【解决方案1】:

【讨论】:

  • 但是手势法我差不多完成了
  • ViewPager Gallery 是一个示例,您只能使用 ImageView,而手势由 ViewPager 本身处理
【解决方案2】:

你也可以使用视图翻转器,我已经在我的代码中使用它来在我的画廊中滑动图像

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>

【讨论】:

  • 请添加动画 xml 文件,也供我参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多