本来以为在代码中实现按钮的移动只要给这个按钮设置touch监听,然后在MotionEvent.ACTION_MOVE中 获取当前的坐标x y,直接设置给控件就行呢...结果可像而知...显然失败
然后就有了下面的这些代码:
布局很简单,直接一个相对布局,放一个按钮:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_move"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="HELLO" />
</RelativeLayout>
程序代码,只要是实现了ontouch监听,然后在move中进行操作:
package com.example.testmovebutton;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity {
private Button moveButton;
// 控件上一次所处的坐标
private float lastX = 0;
private float lastY = 0;
// 屏幕的宽度和高度
private int screenWidth;
private int screenHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
// 获取屏幕的宽度和高度
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
screenWidth = windowManager.getDefaultDisplay().getWidth();
screenHeight = windowManager.getDefaultDisplay().getHeight();
moveButton = (Button) findViewById(R.id.btn_move);
moveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main.this, "hello click", Toast.LENGTH_SHORT)
.show();
}
});
moveButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//获取点击时x y 轴的数据
lastX = event.getRawX();
lastY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
Log.d("test", "up" + event.getX() + ":" + event.getY());
//触摸弹起的时候来一个小动画
startAnimation();
break;
case MotionEvent.ACTION_MOVE:
//在move中直接把得到的坐标设置为控件的坐标..果然天真单纯
// v.setX(event.getX());
// v.setY(event.getY());
//获得x y轴的偏移量
int dx = (int) (event.getRawX() - lastX);
int dy = (int) (event.getRawY() - lastY);
//获得控件上下左右的位置信息,加上我们的偏移量,新得到的位置就是我们
//控件将要出现的位置
int l = v.getLeft() + dx;
int b = v.getBottom() + dy;
int r = v.getRight() + dx;
int t = v.getTop() + dy;
//判断四个实际位置,如果有一边已经划出屏幕,那就把这边位置设置为0
//然后相反的边的位置就设置成控件的高度或者宽度
if (l < 0) {
l = 0;
r = l + v.getWidth();
}
if (t < 0) {
t = 0;
b = t + v.getHeight();
}
if (r > screenWidth) {
r = screenWidth;
l = r - v.getWidth();
}
if (b > screenHeight) {
b = screenHeight;
l = b - v.getHeight();
}
//然后使用我们view的layout重新在布局中把我们的控件画出来
v.layout(l, t, r, b);
//并把现在的x y设置给lastx lasty
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
v.postInvalidate();//绘画
break;
default:
break;
}
return false;
}
});
}
/**
* 启动一个动画
*/
public void startAnimation() {
//先获取当前控件的x坐标
//然后向右五次,向左一次回来
float x = moveButton.getX();
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(moveButton,
"X", x-100,x+100);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(moveButton,
"X", x+100,x);
objectAnimator1.setRepeatCount(4);
objectAnimator2.setRepeatCount(0);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator2).after(objectAnimator1);
animatorSet.setDuration(100);
animatorSet.start();
}
}
到现在,,就结束了..程序很简单