1

我想在android中使用onTouchListener()移动一个imageview和手指......

我创建了一个 xml -->

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/rel_slide"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/background_dark" >

        <ImageView
            android:id="@+id/img_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"/>"
    </RelativeLayout>

</RelativeLayout>

现在在 Activity 文件中,我在这个 imageView 上实现了 onTouchListener:img_arrow

这是代码:

((ImageView) findViewById(R.id.img_arrow)).setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
            switch (arg1.getAction()) {
              case MotionEvent.ACTION_MOVE:
                     LayoutParams layoutParams = (LayoutParams)v.getLayoutParams();
                 layoutParams.leftMargin=(int)arg1.getX();
             layoutParams.topMargin=(int)arg1.getY();
             v.setLayoutParams(layoutParams);
                  break;
            }
        return true;
        }
            });

但这会导致一次显示 2 个的 imageView 闪烁,这可能是因为调用 ACTION_MOVE 时layoutParams的设置非常快.....请帮助

4

2 回答 2

1

嘿,我建议你不要玩视图。最好在视图上设置动画并使用该动画对象来执行所有操作。只是看不见 onTouchEvent 上的视图,让动画对象为你完成工作。您可以在列表视图https://github.com/47deg/android-swipelistview中查看 swipeToDismiss 的示例或顶级开发人员的任何其他滑动示例。编码快乐!!

【新】我也做过绝招。我想通过滑动来解锁功能,所以我使用了搜索栏并对其进行了自定义。它工作得很好,或者我。

于 2013-08-18T16:11:14.800 回答
0

尝试一件事删除

android:src="@drawable/ic_launcher"

<ImageView
        android:id="@+id/img_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

并在运行时从类本身设置图像。我希望这对你有所帮助。

于 2013-08-18T18:14:33.830 回答