Android:ImageView 扩展了 onTouchEvent 以进行拖动
我写了一个简单的纸牌游戏,用户可以玩他的纸牌 在三个图像视图之一(卡片)上进行 TAP 我想添加打牌的可能性 通过将 ImageView 拖动到“桌子”上 (表格是另一种布局或只是屏幕的另一部分)。
我尝试使用指定的技术 http://blahti.wordpress.com/2011/01/ 17/移动视图-第2部分/ 但由于它使用AbsoluteLayout,因此引入了很多限制 根据我当前的布局,更多需要根据 应用程序运行的设备屏幕分辨率。 我想避免这种情况继续使用 -如果可能-RelativeLayout。 也许起点是扩展 ImageView 添加 onTouch 支持,但我无法重现 想要的效果(拖动) 有什么想法或建议或示例代码吗? 谢谢!
只是提供一个想法,这是布局草案。 下面的 3 张卡片应通过拖动来移动。
<?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"
>
<RelativeLayout android:id="@+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="150dip"
android:gravity="center_horizontal"
android:background="#55335588"
android:layout_above="@+id/player_cards_layout"
>
<ImageView android:id="@+id/img_pc_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
/>
</RelativeLayout>
<RelativeLayout android:id="@+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#336699"
>
<ImageView android:id="@+id/img_user_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
/>
<ImageView android:id="@+id/img_user_card_2"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/img_user_card_1"
/>
<ImageView android:id="@+id/img_user_card_3"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/img_user_card_2"
/>
</RelativeLayout>
</RelativeLayout>
I wrote a simple cards game where the user plays his card
doing TAP on one of the three imageviews (the cards)
I'd like to add the possibility of playing the card
by dragging the ImageView on the "table"
(the table is another layout or simply another part of the screen).
I tryed to use the techinc indicated at
http://blahti.wordpress.com/2011/01/17/moving-views-part-2/
but as it uses AbsoluteLayout, it introduces a lot of limitations
on my current layout, more it requires adjustments depending
to the device screen resolution where the app runs.
I'd like to avoid this continue using
-if possibile- the RelativeLayout.
Maybe the starting point is extenting the ImageView
adding the onTouch support but i couldn't reproduce
the wished effect (drag)
Any idea or suggestion or sample code?
Thanks!
Just to give an idea, this is the draft of layout.
The 3 cards below should be moved via drag.
<?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"
>
<RelativeLayout android:id="@+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="150dip"
android:gravity="center_horizontal"
android:background="#55335588"
android:layout_above="@+id/player_cards_layout"
>
<ImageView android:id="@+id/img_pc_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
/>
</RelativeLayout>
<RelativeLayout android:id="@+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#336699"
>
<ImageView android:id="@+id/img_user_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
/>
<ImageView android:id="@+id/img_user_card_2"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/img_user_card_1"
/>
<ImageView android:id="@+id/img_user_card_3"
android:layout_width="100dip" android:layout_height="150dip"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/img_user_card_2"
/>
</RelativeLayout>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是链接中的一个很好的示例。我希望这会对你有所帮助。
res/layout/main.xml
src/com/beanie/example/Home.java
here is nice example from link. i hope this will help you.
res/layout/main.xml
src/com/beanie/example/Home.java