Android实现拖动效果
2010-04-16 16:23
387 查看
要实现的效果就是在界面上拖动这一个按钮到处跑步骤如下:
1. 布局文件
<?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;
}
});
}
}
1. 布局文件
<?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;
}
});
}
}
相关文章推荐
- Android实现GridView的item长按拖动删除完美实现(带动画效果)
- 实现android布局的左右拖动及动画效果的代码范例
- Android下图片或按钮等可拖动到任意位置的效果实现源码
- Android实现GridView中的item自由拖动效果
- Android编程实现图标拖动效果的方法
- Android使用ViewPager实现类似laucher左右拖动效果
- 如何实现android控件的拖动效果
- 在android中实现类似uc和墨迹天气的左右拖动效果
- Android中如何使用ViewPager实现类似laucher左右拖动效果 3ff8
- Android ScrollView实现横向和竖向拖动回弹效果
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- Android实现拖动效果的两种解法
- android实现uc和墨迹天气那样的左右拖动效果
- Android开发_拖动效果,旋转效果,两点放大缩小的实现
- Android实现拖动小球跟随手指移动效果
- Android下图片可拖动到任意位置的效果的实现源码
- 怎样在android实现uc和墨迹天气那样的左右拖动效果
- 让Android自带的Gallery实现多点缩放,拖动和边界回弹效果,效果流畅 .
- android实现拖动效果
- Android下图片可拖动到任意位置的效果的实现源码