实现拖拽ImageView

 

  
  
  
  
  1. //实现 OnClickListener接口。。。 
  2. public class SettingCenterActivity extends Activity implements OnClickListener { 
  3.     private RelativeLayout rl_enter; 
  4.  
  5.     @Override 
  6.     public void onCreate(Bundle savedInstanceState) { 
  7.         super.onCreate(savedInstanceState); 
  8.         setContentView(R.layout.main); 
  9.         rl_enter = (RelativeLayout) findViewById(R.id.rl_enter); 
  10.         rl_enter.setOnClickListener(this); 
  11.     } 
  12.  
  13.     public void onClick(View v) { 
  14.         switch (v.getId()) { 
  15.         case R.id.rl_enter: 
  16.             Intent intent = new Intent(this, DragViewActivity.class); 
  17.             startActivity(intent); 
  18.             overridePendingTransition(R.anim.trans_in, R.anim.trans_out); 
  19.             break
  20.         } 
  21.     } 
  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <TextView 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="设置中心" 
  11.          android:gravity="center_horizontal" 
  12.         android:textColor="#66ff00" 
  13.         android:textSize="28sp" /> 
  14.  
  15.     <RelativeLayout 
  16.         android:id="@+id/rl_enter" 
  17.         android:layout_width="fill_parent" 
  18.         android:layout_height="wrap_content" > 
  19.  
  20.  
  21.         <LinearLayout 
  22.             android:layout_width="wrap_content" 
  23.             android:layout_height="wrap_content"  
  24.             android:orientation="vertical" 
  25.             > 
  26.             <TextView 
  27.                 android:layout_width="fill_parent" 
  28.                 android:layout_height="wrap_content" 
  29.                 android:text="归属地提示框位置"  
  30.                 android:textColor="#66ff00" 
  31.                 android:textSize="25sp"/> 
  32.               <TextView 
  33.                 android:layout_width="fill_parent" 
  34.                 android:layout_height="wrap_content" 
  35.                 android:text="提示归属地提示框位置" 
  36.                  android:textColor="#99ffffff" 
  37.                 android:textSize="18sp" /> 
  38.         </LinearLayout> 
  39.            <ImageView 
  40.             android:scaleType="center" 
  41.             android:paddingLeft="5dip" 
  42.             android:paddingTop="8dip" 
  43.             android:layout_width="40dip" 
  44.             android:layout_height="40dip" 
  45.             android:layout_alignParentRight="true" 
  46.             android:src="@drawable/director_icon" /> 
  47.              
  48.     </RelativeLayout> 
  49.      <View 
  50.         android:layout_width="fill_parent" 
  51.         android:layout_height="1dip" 
  52.         android:layout_marginTop="5dip" 
  53.         android:background="@drawable/list_devide" 
  54.         android:paddingTop="5dip" > 
  55.     </View> 
  56.  
  57. </LinearLayout> 
  
  
  
  
  1. public class DragViewActivity extends Activity { 
  2.     protected static final String TAG = "DragViewActivity"
  3.     private TextView tv; 
  4.     private ImageView iv; 
  5.     private SharedPreferences sp; 
  6.     private int windowHeight; 
  7.     private int windowWidth; 
  8.     private long clicktime; 
  9.  
  10.     @Override 
  11.     protected void onCreate(Bundle savedInstanceState) { 
  12.         setContentView(R.layout.main2); 
  13.         super.onCreate(savedInstanceState); 
  14.         tv = (TextView) findViewById(R.id.tv_drag_view); 
  15.         iv = (ImageView) findViewById(R.id.iv_drag_view); 
  16.  
  17.         sp = getSharedPreferences("config", MODE_PRIVATE); 
  18.         windowHeight = getWindow().getWindowManager().getDefaultDisplay() 
  19.                 .getHeight(); 
  20.         windowWidth = getWindowManager().getDefaultDisplay().getWidth(); 
  21.         // 初始化 上次记录的imagview在窗体中的位置. 
  22.         int endleft = sp.getInt("endleft"0); 
  23.         int endtop = sp.getInt("endtop"0); 
  24.  
  25.         // 更改imageview初始化的时候 在窗体中的位置 
  26.         RelativeLayout.LayoutParams params = (LayoutParams) iv 
  27.                 .getLayoutParams(); 
  28.         params.leftMargin = endleft; 
  29.         params.topMargin = endtop; 
  30.         iv.setLayoutParams(params); 
  31.  
  32.         iv.setOnTouchListener(new OnTouchListener() { 
  33.             int startx; 
  34.             int starty; 
  35.  
  36.             public boolean onTouch(View v, MotionEvent event) { 
  37.                 switch (event.getAction()) { 
  38.                 case MotionEvent.ACTION_DOWN: // 手指第一次触摸到屏幕对应的事件 
  39.                     Log.i(TAG, "手指触摸"); 
  40.                     startx = (int) event.getRawX(); 
  41.                     starty = (int) event.getRawY(); 
  42.                     break
  43.  
  44.                 case MotionEvent.ACTION_MOVE: // 手指在屏幕上移动的时候对应的事件 
  45.                     Log.i(TAG, "手指移动"); 
  46.                     int x = (int) event.getRawX(); 
  47.                     int y = (int) event.getRawY(); 
  48.                     int dx = x - startx; 
  49.                     int dy = y - starty; 
  50.                     Log.i(TAG, "更改imageview在窗体中的位置"); 
  51.                     int l = iv.getLeft(); 
  52.                     int r = iv.getRight(); 
  53.                     int t = iv.getTop(); 
  54.                     int b = iv.getBottom(); 
  55.                     int tvbottom = tv.getBottom(); 
  56.                     int tvtop = tv.getTop(); 
  57.                     int tvheight = tvbottom - tvtop; 
  58.                     if (t > (windowHeight / 2)) {// imageview 在窗体的下面 
  59.                         tv.layout(tv.getLeft(), 10, tv.getRight(), 
  60.                                 10 + tvheight); // 设置tv在窗体的上面 
  61.                     } else { 
  62.                         // 设置tv在窗体的下面 
  63.                         tv.layout(tv.getLeft(), windowHeight - 25 - tvheight, 
  64.                                 tv.getRight(), windowHeight - 25); 
  65.                     } 
  66.  
  67.                     l += dx; 
  68.                     r += dx; 
  69.                     t += dy; 
  70.                     b += dy; 
  71.  
  72.                     if (l < 0 || t < 0 || r > windowWidth || b > windowHeight) { 
  73.                         break
  74.                     } 
  75.  
  76.                     iv.layout(l, t, r, b); 
  77.                     // 重新设置初始化图片移动后的位置. 
  78.                     startx = (int) event.getRawX(); 
  79.                     starty = (int) event.getRawY(); 
  80.                     break
  81.  
  82.                 case MotionEvent.ACTION_UP: // 手指离开屏幕一瞬间对应的事件 
  83.                     Log.i(TAG, "手指离开"); 
  84.                     int endleft = iv.getLeft(); 
  85.                     int endtop = iv.getTop(); 
  86.                     Editor editor = sp.edit(); 
  87.                     editor.putInt("endleft", endleft); 
  88.                     editor.putInt("endtop", endtop); 
  89.                     editor.commit(); 
  90.                     break
  91.                 } 
  92.                 return false;// True if the listener has consumed the event, 
  93.                                 // false otherwise. 
  94.                 // true会消费掉触摸事件 flase话不会消费掉触摸事件. 
  95.             } 
  96.         }); 
  97.  
  98.         iv.setOnClickListener(new OnClickListener() { 
  99.  
  100.             public void onClick(View v) { 
  101.                 Log.i(TAG, "我被点击了"); 
  102.                 if (clicktime > 0) { 
  103.                     // 第二次的点击 
  104.                     long secondtime = System.currentTimeMillis(); 
  105.                     if (secondtime - clicktime < 500) { 
  106.                         Log.i(TAG, "双击"); 
  107.                         int left = iv.getLeft(); 
  108.                         int right = iv.getRight(); 
  109.                         int width = right - left; 
  110.                         iv.layout(windowWidth / 2 - width / 2, iv.getTop(), 
  111.                                 windowWidth / 2 + width / 2, iv.getBottom()); 
  112.                         int endleft = iv.getLeft(); 
  113.                         int endtop = iv.getTop(); 
  114.                         Editor editor = sp.edit(); 
  115.                         editor.putInt("endleft", endleft); 
  116.                         editor.putInt("endtop", endtop); 
  117.                         editor.commit(); 
  118.                     } 
  119.                     clicktime = 0// 两次点击后 数据清零 
  120.                     return
  121.                 } 
  122.  
  123.                 clicktime = System.currentTimeMillis(); 
  124.                 new Thread() { 
  125.                     public void run() { 
  126.                         try { 
  127.                             Thread.sleep(500); 
  128.                             clicktime = 0
  129.                         } catch (InterruptedException e) { 
  130.                             e.printStackTrace(); 
  131.                         } 
  132.                     }; 
  133.                 }.start(); 
  134.  
  135.             } 
  136.         }); 
  137.     } 
  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" > 
  5.  
  6.     <ImageView 
  7.         android:id="@+id/iv_drag_view" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:src="@drawable/location" /> 
  11.  
  12.     <TextView 
  13.         android:id="@+id/tv_drag_view" 
  14.         android:layout_width="wrap_content" 
  15.         android:layout_height="wrap_content" 
  16.         android:layout_marginTop="60dip" 
  17.         android:text="拖动更改位置\n返回立刻生效" 
  18.         android:textSize="25sp" > 
  19.     </TextView> 
  20.  
  21. </RelativeLayout> 

 

你可能感兴趣的:(拖拽ImageView)