Android 如何用手指在目标点上拖动图像
可能重复:Android 如何用手指在目标点上拖动图像,android,Android,可能重复: 如何在目标点边界上用手指拖动位图向左和向右移动 插图 B--------o1-----------o2-----------o3 B=位图 o1=我的目的地1 ---------B---------o2-----------o3 ----------B--------o2-------------o3 o2=我的目的地点2 ----------o1--------B-------------o3 o3=我的目的地3 如果位图拖动到右侧,则位图位于目标点1上 --------
如何在目标点边界上用手指拖动位图向左和向右移动 插图
B--------o1-----------o2-----------o3
B=位图
o1=我的目的地1
---------B---------o2-----------o3
----------B--------o2-------------o3
o2=我的目的地点2
----------o1--------B-------------o3
o3=我的目的地3
如果位图拖动到右侧,则位图位于目标点1上
---------B---------o2-----------o3
----------B--------o2-------------o3
如果目标点1上的位图向右拖动,则位图将更改为目标点2
----------o1--------B-------------o3
如果目标点2上的位图向左拖动,则位图将返回目标点1
---------B---------o2-----------o3
----------B--------o2-------------o3
等等
如何在android代码中实现我的想法
请给我示例代码,因为我是android新手。XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/vg"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:src="@drawable/image" />
</LinearLayout>
您为什么不尝试使用onTouchEvent
将根据该示例教程链接提供您想要的任何内容。样本代码-
@Override
public boolean onTouchEvent(MotionEvent event) {
mX = (int) event.getX() - mBitmap.getWidth() / 2;
mY = (int) event.getY() - mBitmap.getHeight() / 2;
return super.onTouchEvent(event);
}
我想你在期待@SpK+1不错的教程:)