概述
packagecom.example.viewanimationtest;
/**
* @author maria
* 2013-2-16
*/
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnTouchListener;
importandroid.view.ViewGroup;
importandroid.widget.Button;
importandroid.widget.RelativeLayout;
publicclassMainActivityextendsActivityimplementsOnTouchListener {
Button _view;
ViewGroup _root;
privateint_xDelta;
privateint_yDelta;
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_root = (ViewGroup) findViewById(R.id.root);
_view = (Button) findViewById(R.id.id_text);
RelativeLayout.LayoutParams layoutParams = newRelativeLayout.LayoutParams(
150,50);
layoutParams.leftMargin = 50;
layoutParams.topMargin = 50;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
_view.setLayoutParams(layoutParams);
_view.setOnTouchListener(this);
}
publicbooleanonTouch(View view, MotionEvent event) {
finalintX = (int) event.getRawX();
finalintY = (int) event.getRawY();
switch(event.getAction() & MotionEvent.ACTION_MASK) {
caseMotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
caseMotionEvent.ACTION_UP:
break;
caseMotionEvent.ACTION_POINTER_DOWN:
break;
caseMotionEvent.ACTION_POINTER_UP:
break;
caseMotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
returntrue;
}
}
最后
以上就是真实柚子为你收集整理的android手指移动按钮,android 控件跟随手指移动的全部内容,希望文章能够帮你解决android手指移动按钮,android 控件跟随手指移动所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复