拖拽图片效果的实现其实也很简单,只要对需要拖拽的图片设置一个TouchListener即可,在Listener的方法里边对图片的位置进行计算更改就可以达到效果了。具体实现如下:

1.Activity

        
  1. importandroid.app.Activity;
  2. importandroid.os.Bundle;
  3. importandroid.view.MotionEvent;
  4. importandroid.view.View;
  5. importandroid.view.View.OnTouchListener;
  6. importandroid.widget.ImageView;
  7. publicclassDragPicActivityextendsActivity{
  8. privateImageViewimageView;
  9. @Override
  10. protectedvoidonCreate(BundlesavedInstanceState){
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.drag);
  13. imageView=(ImageView)findViewById(R.id.drag_pic);
  14. imageView.setOnTouchListener(newOnTouchListener(){
  15. privatefloatx,y;
  16. privateintmx,my;
  17. @Override
  18. publicbooleanonTouch(Viewv,MotionEventevent){
  19. switch(event.getAction()){
  20. caseMotionEvent.ACTION_DOWN:
  21. x=event.getX();
  22. y=event.getY();
  23. caseMotionEvent.ACTION_MOVE:
  24. mx=(int)(event.getRawX()-x);
  25. //减去的50是title和statusbar的高度
  26. my=(int)(event.getRawY()-y-50);
  27. v.layout(mx,my,mx+v.getWidth(),my+v.getHeight());
  28. break;
  29. }
  30. returntrue;
  31. }
  32. });
  33. }
  34. }

2.layout

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:gravity="center"
  6. android:orientation="vertical">
  7. <ImageView
  8. android:id="@+id/drag_pic"
  9. android:layout_width="50dip"
  10. android:layout_height="50dip"
  11. android:src="@drawable/ic_launcher"/>
  12. </LinearLayout>

这样拖拽图片的效果就出来了,同样的拖拽其他控件应该也是一样的做法,没测试,但拖拽图片其实就是拖拽ImageView嘛,so easy是吧!

记录学习的点点滴滴,让知识掌握得更牢固。也为回忆,嘿嘿~~Come on,Iceshow!

更多相关文章

  1. Android通过url下载文件到手机本地
  2. Android版得HelloWorld
  3. android中手势操作图片的平移、缩放、旋转
  4. Android(安卓)图片处理之图片叠加--Bitmap
  5. 实现图片浏览,改变透明度的方法
  6. Android(安卓)-------- BouncingJellyView 果冻视图(阻尼效果)
  7. Android动画特效实例
  8. Android(安卓)Volley使用之二:Volley请求网络图片
  9. 【android】为PopupWindow设置动画效果

随机推荐

  1. 发送短信之分割短信 SMSManager
  2. android recycler添加分割线 点击事件等
  3. android 完全关闭进程
  4. RecyclerView Adapter点击事件最佳实践
  5. android的activity再按一次退出程序
  6. Toast的基本用法
  7. Eclipse Indigo - Cannot install Androi
  8. Android(安卓)目录选择器
  9. Android(安卓)drawBitmapMesh扭曲触摸点
  10. 单选菜单