Android 拖拽效果实现代码分享 - ORIETech

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;

}

});

}

}

更多相关文章

  1. android图片转换代码
  2. 一个android通信录的源代码
  3. 7个Android应用程序源代码
  4. 介绍本人的一个Android项目(附源代码)
  5. 安卓隐藏标题栏状态栏 ,实现全屏效果
  6. Android安卓51个开源代码
  7. android界面模糊到清晰,效果去除
  8. Android毛玻璃效果
  9. AndroidTextView的跑马灯效果,解决复杂布局不能获取焦点的问题

随机推荐

  1. android 夜间模式
  2. android AMS
  3. Android程序结构解析
  4. android animation
  5. AndroidMainfest.xml详解
  6. Android高手进阶教程(七)之----Android(
  7. Android培训班(48)
  8. 在Android中查看和管理sqlite数据库
  9. Android(安卓)AIDL接口定义语言
  10. ReactNative与Android(安卓)集成踩坑