图片可以拖到任意位置
2012-12-26 14:48
218 查看
下面为Activity的代码:
XML配置文件Sign.xml的代码:
这样就可以实现了~
01 | public class DraftTest extends Activity { |
02 | /** |
03 | @Override |
04 | public void onCreate(Bundle |
05 | super .onCreate(savedInstanceState); |
06 | setContentView(R.layout.sign); |
07 | DisplayMetrics |
08 | final int screenWidth |
09 | final int screenHeight 50 ; |
10 | //拖动的按钮 |
11 | final Button |
12 |
13 | //添加触摸事件 |
14 |
15 | b.setOnTouchListener( new OnTouchListener(){ |
16 | int lastX, //记录移动的最后的位置 |
17 | public boolean onTouch(View |
18 | //获取Action |
19 |
20 | int ea=event.getAction(); |
21 |
22 | Log.i( "TAG" , "Touch:" +ea); |
23 | switch (ea){ |
24 | case MotionEvent.ACTION_DOWN: //按下 |
25 | lastX int ) |
26 | lastY int ) |
27 | break ; |
28 | /** |
29 | * |
30 | * |
31 | t |
32 | r |
33 | b |
34 | * */ |
35 | case MotionEvent.ACTION_MOVE: //移动 |
36 | //移动中动态设置位置 |
37 | int dx =( int )event.getRawX() |
38 | int dy =( int )event.getRawY() |
39 | int left |
40 | int top |
41 | int right |
42 | int bottom |
43 | if (left 0 ){ |
44 | left 0 ; |
45 | right |
46 | } |
47 | if (right |
48 | right |
49 | left |
50 | } |
51 | if (top 0 ){ |
52 | top 0 ; |
53 | bottom |
54 | } |
55 | if (bottom |
56 | bottom |
57 | top |
58 | } |
59 | v.layout(left, |
60 | Log.i( "" , "position:" + ", " + ", " + ", " + bottom); |
61 | //将当前的位置再次设置 |
62 | lastX int ) |
63 | lastY int ) |
64 | break ; |
65 | case MotionEvent.ACTION_UP: //脱离 |
66 | break ; |
67 | } |
68 | return false ; |
69 | }}); |
70 | } |
71 | } |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:id = "@+id/layoutRacingNeedForSpeed" android:padding = "0px" |
04 | android:layout_width = "fill_parent" android:layout_height = "fill_parent" > |
05 | < Button |
06 | android:id = "@+id/startBtn" |
07 | android:text = "tuodongdeanniu" |
08 | android:layout_centerInParent = "true" |
09 | android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> |
10 | </ RelativeLayout > |
相关文章推荐
- GridViewRow可以任意位置单击引发事件的方法
- 基于文字识别---实现点击图片任意位置文字即可翻译的功能
- 自定义的symbian图片控件,可在任意的位置放图片
- 双链表排序--可以在任意位置正确插入节点
- 微信公众号点击分享生成分享的图片,可以调整二维码,头像,昵称到背景图指定位置
- 让您的电脑在任意目录可以支持图片的粘贴,试试看呗~
- GridViewRow可以任意位置单击引发事件的方法!
- 在图片任意位置上添加文本
- [自用门户]jeecms 三级页面内容模板,重点是实现<hr />标签分割线可以参考,css任意位置及长短
- c# 添加图片水印,可以指定水印位置+生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图
- C#创建可以随意拖动的控件,拖动窗体上的任意控件到指定位置
- 不喜欢让Latex自动安排图片位置,可以使用float包
- c# 添加图片水印,可以指定水印位置+生成缩略图
- Android下图片或按钮等可拖动到任意位置的效果实现源码
- 在控件的任意位置显示图片
- 图片任意位置添加水印
- 如何在html中把一个图片或者表格覆盖在一张已有图片上的任意位置
- 在控件的任意位置显示图片
- iPhone在任意位置显示图片代码