示例图:
重点在于 从写 了 TouchView,原理是一样的:
public class TouchView extends ImageView {
public TouchView(Context context) {
super(context);
this.setImageResource(R.drawable.ic_launcher);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(100, 100);
}
float downX, downY;
float moveX, moveY;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = event.getRawX();
downY = event.getRawY();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
moveX = event.getRawX();
moveY = event.getRawY();
this.setX(getX() + (moveX - downX));
this.setY(getY() + (moveY - downY));
downX = moveX;
downY = moveY;
}
return true;// 返回true 表示 处理Touch事件
// return super.onTouchEvent(event);
}
}
public class MainActivity extends Activity {
LinearLayout linear;
RelativeLayout relative;
FrameLayout frame;
AbsoluteLayout absolute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear = (LinearLayout) findViewById(R.id.linear);
relative = (RelativeLayout) findViewById(R.id.relative);
frame = (FrameLayout) findViewById(R.id.frame);
absolute = (AbsoluteLayout) findViewById(R.id.absolute);
linear.addView(new TouchView(this));
relative.addView(new TouchView(this));
frame.addView(new TouchView(this));
absolute.addView(new TouchView(this));
}
}
源代码下载地址:http://download.csdn.net/detail/angcyo/8953263
至此: 文章就结束了,如有疑问: QQ群:274306954 欢迎您的加入.