2013-08-17 38 views
1

我想用onTouchListener()在Android中一起移动的imageview的手指......如何在Android中使用onTouchListener()与手指一起移动ImageView?

我已经创建了一个XML - 在我实现onTouchListener到活动文件>

 <?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> 

现在这个ImageView的: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; 
     } 
      }); 

但是这导致的ImageView显示2 flikering同时可能是因为的LayoutParams的非常快速的设定时ACTION_MOVE叫..... plz帮助

回答

1

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

[新] 我也做了把戏。我想做一个滑动来解锁功能,所以我使用了一个seekbar并对其进行了自定义。它工作得很好或我。

0

尝试的一件事从

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

删除

android:src="@drawable/ic_launcher" 

,并从类本身上运行时设置的图像。 我希望这可以帮助你一点。

+0

无法通过从类添加....仍然flikering –

相关问题