- //实现 OnClickListener接口。。。
- public class SettingCenterActivity extends Activity implements OnClickListener {
- private RelativeLayout rl_enter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- rl_enter = (RelativeLayout) findViewById(R.id.rl_enter);
- rl_enter.setOnClickListener(this);
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.rl_enter:
- Intent intent = new Intent(this, DragViewActivity.class);
- startActivity(intent);
- overridePendingTransition(R.anim.trans_in, R.anim.trans_out);
- break;
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="设置中心"
- android:gravity="center_horizontal"
- android:textColor="#66ff00"
- android:textSize="28sp" />
- <RelativeLayout
- android:id="@+id/rl_enter"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="归属地提示框位置"
- android:textColor="#66ff00"
- android:textSize="25sp"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="提示归属地提示框位置"
- android:textColor="#99ffffff"
- android:textSize="18sp" />
- </LinearLayout>
- <ImageView
- android:scaleType="center"
- android:paddingLeft="5dip"
- android:paddingTop="8dip"
- android:layout_width="40dip"
- android:layout_height="40dip"
- android:layout_alignParentRight="true"
- android:src="@drawable/director_icon" />
- </RelativeLayout>
- <View
- android:layout_width="fill_parent"
- android:layout_height="1dip"
- android:layout_marginTop="5dip"
- android:background="@drawable/list_devide"
- android:paddingTop="5dip" >
- </View>
- </LinearLayout>
- public class DragViewActivity extends Activity {
- protected static final String TAG = "DragViewActivity";
- private TextView tv;
- private ImageView iv;
- private SharedPreferences sp;
- private int windowHeight;
- private int windowWidth;
- private long clicktime;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- setContentView(R.layout.main2);
- super.onCreate(savedInstanceState);
- tv = (TextView) findViewById(R.id.tv_drag_view);
- iv = (ImageView) findViewById(R.id.iv_drag_view);
- sp = getSharedPreferences("config", MODE_PRIVATE);
- windowHeight = getWindow().getWindowManager().getDefaultDisplay()
- .getHeight();
- windowWidth = getWindowManager().getDefaultDisplay().getWidth();
- // 初始化 上次记录的imagview在窗体中的位置.
- int endleft = sp.getInt("endleft", 0);
- int endtop = sp.getInt("endtop", 0);
- // 更改p_w_picpathview初始化的时候 在窗体中的位置
- RelativeLayout.LayoutParams params = (LayoutParams) iv
- .getLayoutParams();
- params.leftMargin = endleft;
- params.topMargin = endtop;
- iv.setLayoutParams(params);
- iv.setOnTouchListener(new OnTouchListener() {
- int startx;
- int starty;
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN: // 手指第一次触摸到屏幕对应的事件
- Log.i(TAG, "手指触摸");
- startx = (int) event.getRawX();
- starty = (int) event.getRawY();
- break;
- case MotionEvent.ACTION_MOVE: // 手指在屏幕上移动的时候对应的事件
- Log.i(TAG, "手指移动");
- int x = (int) event.getRawX();
- int y = (int) event.getRawY();
- int dx = x - startx;
- int dy = y - starty;
- Log.i(TAG, "更改p_w_picpathview在窗体中的位置");
- int l = iv.getLeft();
- int r = iv.getRight();
- int t = iv.getTop();
- int b = iv.getBottom();
- int tvbottom = tv.getBottom();
- int tvtop = tv.getTop();
- int tvheight = tvbottom - tvtop;
- if (t > (windowHeight / 2)) { // p_w_picpathview 在窗体的下面
- tv.layout(tv.getLeft(), 10, tv.getRight(),
- 10 + tvheight); // 设置tv在窗体的上面
- } else {
- // 设置tv在窗体的下面
- tv.layout(tv.getLeft(), windowHeight - 25 - tvheight,
- tv.getRight(), windowHeight - 25);
- }
- l += dx;
- r += dx;
- t += dy;
- b += dy;
- if (l < 0 || t < 0 || r > windowWidth || b > windowHeight) {
- break;
- }
- iv.layout(l, t, r, b);
- // 重新设置初始化图片移动后的位置.
- startx = (int) event.getRawX();
- starty = (int) event.getRawY();
- break;
- case MotionEvent.ACTION_UP: // 手指离开屏幕一瞬间对应的事件
- Log.i(TAG, "手指离开");
- int endleft = iv.getLeft();
- int endtop = iv.getTop();
- Editor editor = sp.edit();
- editor.putInt("endleft", endleft);
- editor.putInt("endtop", endtop);
- editor.commit();
- break;
- }
- return false;// True if the listener has consumed the event,
- // false otherwise.
- // true会消费掉触摸事件 flase话不会消费掉触摸事件.
- }
- });
- iv.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Log.i(TAG, "我被点击了");
- if (clicktime > 0) {
- // 第二次的点击
- long secondtime = System.currentTimeMillis();
- if (secondtime - clicktime < 500) {
- Log.i(TAG, "双击");
- int left = iv.getLeft();
- int right = iv.getRight();
- int width = right - left;
- iv.layout(windowWidth / 2 - width / 2, iv.getTop(),
- windowWidth / 2 + width / 2, iv.getBottom());
- int endleft = iv.getLeft();
- int endtop = iv.getTop();
- Editor editor = sp.edit();
- editor.putInt("endleft", endleft);
- editor.putInt("endtop", endtop);
- editor.commit();
- }
- clicktime = 0; // 两次点击后 数据清零
- return;
- }
- clicktime = System.currentTimeMillis();
- new Thread() {
- public void run() {
- try {
- Thread.sleep(500);
- clicktime = 0;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- };
- }.start();
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <ImageView
- android:id="@+id/iv_drag_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/location" />
- <TextView
- android:id="@+id/tv_drag_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="60dip"
- android:text="拖动更改位置\n返回立刻生效"
- android:textSize="25sp" >
- </TextView>
- </RelativeLayout>