我的工作在Android中,我使用的是按钮。现在我想执行拖放该按钮。
I am working in android, I am using a button. Now I want to perform drag and drop of this button.
这是我的main.xml
This is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_first"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="drag me"
/>
</LinearLayout>
这是我的code键移动鼠标拖动这个按钮: -
And this is my code to move this button on mouse drag:-
public class DragdropActivity extends Activity implements OnTouchListener {
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
private Button btn;
private int status;
private ImageView image;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
Log.v("***Drag and drop **",
"me.getRawX and Y = " + me.getRawX() + " "
+ me.getRawY());
Log.v("***Drag and drop **",
"image position = " + image.getLeft() + " "
+ image.getRight());
btn.setPadding((int) me.getRawX(), (int) me.getRawY(), 0,0); //this is not working fine.
btn.invalidate();
}
}
return false;
}
}
我觉得 btn.setPadding()
不能正常工作,请建议我该怎么办,这样按钮可以方便地移动到鼠标或手势触摸的位置?
I think btn.setPadding()
is not working correctly, please suggest me what should I do so that the button can easily move to the position of mouse or gesture touch ?
我解决了这样的我的问题。
I resolved my problem like this.
@Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
50, 50);
layoutParams.setMargins((int) me.getRawX() - 25,
(int) me.getRawY() - 50, 0, 0);
layout.removeView(btn);
layout.addView(btn, layoutParams);
btn.invalidate();
}
}
return false;
}
现在它的工作的罚款。
上一篇:StructureMap:如何注册的所有接口相同的实例实例、接口、StructureMap
下一篇:如果HTML文件无结束&QUOT; / TR&QUOT;标签或&QUOT; / TD&QUOT;标签然后HTML敏捷包不阅读这些信息完美标签、敏捷、结束、完美