Android下图片或按钮等可拖动到任意位置的效果实现源码

本文介绍了一种在Android应用中实现图片拖动至任意位置的方法。通过在Activity中使用Button并添加触摸事件监听,可以实时调整图片的位置,确保其在屏幕范围内自由移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from: http://www.2cto.com/kf/201207/140218.html


Android下图片可拖动到任意位置的效果
下面为Activity的代码:

01
public class DraftTest extends Activity {
02
 /** Called when the activity is first created. */
03
 @Override
04
 public void onCreate(Bundle savedInstanceState) { 
05
  super.onCreate(savedInstanceState); 
06
  setContentView(R.layout.sign);
07
  DisplayMetrics dm = getResources().getDisplayMetrics();
08
  final int screenWidth = dm.widthPixels; 
09
  final int screenHeight = dm.heightPixels - 50; 
10
  //拖动的按钮
11
  final Button b=(Button)findViewById(R.id.startBtn);
12
 
13
  //添加触摸事件
14
 
15
  b.setOnTouchListener(new OnTouchListener(){
16
   int lastX, lastY; //记录移动的最后的位置
17
   public boolean onTouch(View v, MotionEvent event) { 
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) event.getRawX(); 
26
     lastY = (int) event.getRawY(); 
27
     break; 
28
     /**
29
      * layout(l,t,r,b)
30
      * l  Left position, relative to parent 
31
                    t  Top position, relative to parent 
32
                    r  Right position, relative to parent 
33
                    b  Bottom position, relative to parent  
34
      * */
35
    case MotionEvent.ACTION_MOVE:  //移动
36
     //移动中动态设置位置
37
     int dx =(int)event.getRawX() - lastX; 
38
     int dy =(int)event.getRawY() - lastY; 
39
     int left = v.getLeft() + dx; 
40
     int top = v.getTop() + dy; 
41
     int right = v.getRight() + dx; 
42
     int bottom = v.getBottom() + dy; 
43
     if(left < 0){ 
44
      left = 0; 
45
      right = left + v.getWidth(); 
46
     } 
47
     if(right > screenWidth){ 
48
      right = screenWidth; 
49
      left = right - v.getWidth(); 
50
     } 
51
     if(top < 0){ 
52
      top = 0; 
53
      bottom = top + v.getHeight(); 
54
     } 
55
     if(bottom > screenHeight){ 
56
      bottom = screenHeight; 
57
      top = bottom - v.getHeight(); 
58
     } 
59
     v.layout(left, top, right, bottom); 
60
     Log.i("", "position:" + left +", " + top + ", " + right + ", " + bottom);
61
     //将当前的位置再次设置
62
     lastX = (int) event.getRawX(); 
63
     lastY = (int) event.getRawY(); 
64
     break; 
65
    case MotionEvent.ACTION_UP:   //脱离
66
     break;               
67
    } 
68
    return false; 
69
   }}); 
70     www.2cto.com
 }
71
}
XML配置文件Sign.xml的代码:

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值