android 拖拽图片

拖拽图片效果

方法一:

import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class DragSample01 extends Activity { ImageView img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drag_sample01); img = (ImageView)findViewById(R.id.img_view); img.setOnTouchListener(new OnTouchListener(){ private int mx, my; public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_MOVE: mx = (int)(event.getRawX()); my = (int)(event.getRawY() - 50); v.layout(mx - img.getWidth()/2, my - img.getHeight()/2, mx + img.getWidth()/2, my + img.getHeight()/2); break; } return true; }}); } }

 

方法二:

import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class DragSample01 extends Activity { ImageView img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drag_sample01); img = (ImageView)findViewById(R.id.img_view); img.setOnTouchListener(new OnTouchListener(){ private float x, y; private int mx, my; public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: x = event.getX(); y = event.getY(); case MotionEvent.ACTION_MOVE: mx = (int)(event.getRawX() - x); my = (int)(event.getRawY() - 50 - y); v.layout(mx, my, mx + v.getWidth(), my + v.getHeight()); break; } return true; }}); } }

你可能感兴趣的:(android,Class,import,float)