안드로이드에서 이미지/버튼 드래그하기
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnTouchListener { private final int START_DRAG = 0; private final int END_DRAG = 1; private int isMoving; private float offset_x, offset_y; private boolean start_yn = true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) this.findViewById(R.id.button1); button.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (start_yn) { offset_x = event.getRawX(); offset_y = event.getRawY(); start_yn = false; } Toast.makeText(MainActivity.this, "Drag Start", Toast.LENGTH_SHORT).show(); isMoving = START_DRAG; } else if (event.getAction() == MotionEvent.ACTION_UP) { Toast.makeText(MainActivity.this, "Drag End", Toast.LENGTH_SHORT) .show(); isMoving = END_DRAG; } else if (event.getAction() == MotionEvent.ACTION_MOVE) { if (isMoving == START_DRAG) { v.setX((int) event.getRawX() - offset_x); v.setY((int) event.getRawY() - offset_y); } } return false; } } |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:id="@+id/frameLayout"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:text="드롭버튼"/>
</FrameLayout> |
'IT 연구회' 카테고리의 다른 글
listview 2 (0) | 2013.09.17 |
---|---|
listview3 (0) | 2013.09.17 |
listview layout xml (0) | 2013.09.17 |
WifiConfiguration (0) | 2013.08.24 |
WifiManager 메소드 (1) | 2013.08.23 |
CSMA/CA (0) | 2013.06.17 |
circuit switching vs packet switching (1) | 2013.06.17 |
ALOHA protocal (0) | 2013.06.17 |
암호화 요소 (0) | 2013.05.08 |
암호화 요소 (0) | 2013.05.06 |