今天从以上网站看了如何是实现控件拖动,对一些计算逻辑还不是太懂,先收藏到这里,以后慢慢再研究吧。

。。

(1)layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btn_hello" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

(2)


package com.roiding.sample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class Touch extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button btn = (Button) findViewById(R.id.btn_hello);

btn.setOnTouchListener(new OnTouchListener() {
int[] temp = new int[] { 0, 0 };

public boolean onTouch(View v, MotionEvent event) {

int eventaction = event.getAction();
Log.i("&&&", "onTouchEvent:" + eventaction);

int x = (int) event.getRawX();
int y = (int) event.getRawY();

switch (eventaction) {

case MotionEvent.ACTION_DOWN: // touch down so check if the
temp[0] = (int) event.getX();
temp[1] = y - v.getTop();
break;

case MotionEvent.ACTION_MOVE: // touch drag with the ball
v.layout(x - temp[0], y - temp[1], x + v.getWidth()
- temp[0], y - temp[1] + v.getHeight());

v.postInvalidate();
break;

case MotionEvent.ACTION_UP:
break;
}

return false;
}

});

}
}

原文:

http://www.roiding.com/index.php/archives/211

更多相关文章

  1. android 学习 ----控件学习 选项卡。
  2. Android拖动图片及多层图片叠加
  3. Android(安卓)前沿UI
  4. Android(安卓)自定义时钟控件
  5. [译]三分钟玩转Android(安卓)Bottom Sheet
  6. listView常用属性设置
  7. 安卓学习之--UI控件用法 单选 按钮 下拉框
  8. Android事件分发机制概括
  9. Android自定义控件之自定义进度条

随机推荐

  1. Android下实现一个Activity的全屏显示
  2. android selector状态详解
  3. Android系统自带样式(android:theme)详解
  4. android 资源引用
  5. android ImageView的属性android:scaleTy
  6. android在Gridview实现Textview跑马灯效
  7. android知识点记录
  8. Android实现LIstView条目单选和多选Radio
  9. Android(安卓)GridLayout 的 columnSpan
  10. android button 透明背景色