如何从自定义手势监听器开始动画?

我写了一个使用ViewPager的片段来显示图像的小活动。

我还实现了一个自定义的GestureListener,以便捕捉向上滑动并向下滑动手势,而不会干扰ViewPager自己的手势处理。 我想要实现的是在用户向下滑动时显示布局。

问题是,我没有发现如何在手势监听器检测到向下滑动手势时执行动画。

编辑 :添加上下文和视图到MyGestureListener引用视图和上下文。 记录显示手势被正确检测,MyGestureListener中的视图是正确的,但没有任何动画。

代码如下。

活动

public class MainActivity extends FragmentActivity {

private GestureDetector gestureDetector;
private ViewPager vwpMain;
private PagerAdapter pgaMain;
private RelativeLayout layout;
private LinearLayout topLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout = (RelativeLayout) findViewById(R.id.container);
    topLayout = (LinearLayout) findViewById(R.id.topPanel);
    vwpMain = (ViewPager) findViewById(R.id.vwpMain);
    pgaMain = new MyPagerAdapter(getSupportFragmentManager());
    vwpMain.setAdapter(pgaMain);
    gestureDetector = new GestureDetector(this, new MyGestureListener(getApplicationContext(), topLayout));
    layout.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            return true;
        }
    });
}

public boolean dispatchTouchEvent(MotionEvent ev) {
    super.dispatchTouchEvent(ev);
    return gestureDetector.onTouchEvent(ev);
}

private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int pos) {
        return ImageFragment.create(pos);
    }

    @SuppressLint("SdCardPath")
    @Override
    public int getCount() {
        File f = new File("/mnt/sdcard/FragmentImages/");
        return f.listFiles().length;
    }
}

片段

public class ImageFragment extends Fragment {

private int pageNumber;
private ImageView i;

public static ImageFragment create(int pageNumber) {
    ImageFragment f = new ImageFragment();
    Bundle b = new Bundle();
    b.putInt("index", pageNumber);
    int indice = pageNumber + 1;
    String ruta = Environment.getExternalStorageDirectory().getPath()
            + "/FragmentImages/imagen_" + indice + ".jpg";
    b.putString("file", ruta);
    f.setArguments(b);
    return f;
}

public ImageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pageNumber = getArguments().getInt("index");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_image, container, false);
    i = (ImageView) rootView.findViewById(R.id.imgImagen);
    BitmapWorkerTask task = new BitmapWorkerTask(i);
    task.execute(getArguments().getString("file"));
    return rootView;
}

public int getPageNumber() {
    return pageNumber;
}

public class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {

    private final WeakReference<ImageView> imageViewReference;
    private String data;

    public BitmapWorkerTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        data = params[0];
        return BitmapFactory.decodeFile(data);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}
}

手势监听者

public class MyGestureListener extends SimpleOnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 75;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;

    public Context context;
public View view;

public MyGestureListener() {
    super();
            context = _context;
    view = _view;
}

@Override
public boolean onDown(MotionEvent event) {
    return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    float dX = e2.getX() - e1.getX();
    float dY = e1.getY() - e2.getY();
    if (Math.abs(dY) < SWIPE_MAX_OFF_PATH
            && Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY
            && Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
        if (dX > 0) {
            Log.d("Fragment", "Swiping right");
        } else {
            Log.d("Fragment", "Swiping left");
        }
    } else if (Math.abs(dX) < SWIPE_MAX_OFF_PATH
            && Math.abs(velocityY) >= SWIPE_THRESHOLD_VELOCITY
            && Math.abs(dY) >= SWIPE_MIN_DISTANCE) {
        if (dY > 0) {
            Log.d("Fragment", "Swiping up");
        } else {
            Log.d("Fragment", "Swiping down");
                            Animation a = AnimationUtils.loadAnimation(context, R.anim.show_top);
            Log.d("Fragment", view.toString());
            view.startAnimation(a);
        }
    }
    return false;
}
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
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=".MainActivity" >

<LinearLayout
    android:id="@+id/topPanel"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:visibility="gone" >

    <TextView
        android:id="@+id/topPanelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/topPanelText"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/black" />
</LinearLayout>

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vwpMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

动画

<?xml version="1.0" encoding="utf-8"?>

<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromYDelta="-100%"
    android:toYDelta="0%" >
</translate>


找到了解决方案。 我将刷卡检测代码更改为:

Log.d("Fragment", "Swiping down");
Animation showTopPanel = AnimationUtils.loadAnimation(context, R.anim.show_top);
topView.bringToFront();
topView.setVisibility(View.VISIBLE);
topView.startAnimation(showTopPanel);

然后动画显示正确。

链接地址: http://www.djcxy.com/p/91269.html

上一篇: How to start an animation from a custom Gesture Listener?

下一篇: Android list view Right / Left swipes like call logs