Android拖拽图片的实现
16lz
2021-01-26
拖拽图片效果的实现其实也很简单,只要对需要拖拽的图片设置一个TouchListener即可,在Listener的方法里边对图片的位置进行计算更改就可以达到效果了。具体实现如下:
1.Activity
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- importandroid.view.View.OnTouchListener;
- importandroid.widget.ImageView;
- publicclassDragPicActivityextendsActivity{
- privateImageViewimageView;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.drag);
- imageView=(ImageView)findViewById(R.id.drag_pic);
- imageView.setOnTouchListener(newOnTouchListener(){
- privatefloatx,y;
- privateintmx,my;
- @Override
- publicbooleanonTouch(Viewv,MotionEventevent){
- switch(event.getAction()){
- caseMotionEvent.ACTION_DOWN:
- x=event.getX();
- y=event.getY();
- caseMotionEvent.ACTION_MOVE:
- mx=(int)(event.getRawX()-x);
- //减去的50是title和statusbar的高度
- my=(int)(event.getRawY()-y-50);
- v.layout(mx,my,mx+v.getWidth(),my+v.getHeight());
- break;
- }
- returntrue;
- }
- });
- }
- }
2.layout
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:orientation="vertical">
- <ImageView
- android:id="@+id/drag_pic"
- android:layout_width="50dip"
- android:layout_height="50dip"
- android:src="@drawable/ic_launcher"/>
- </LinearLayout>
这样拖拽图片的效果就出来了,同样的拖拽其他控件应该也是一样的做法,没测试,但拖拽图片其实就是拖拽ImageView嘛,so easy是吧!
记录学习的点点滴滴,让知识掌握得更牢固。也为回忆,嘿嘿~~Come on,Iceshow!
更多相关文章
- Android通过url下载文件到手机本地
- Android版得HelloWorld
- android中手势操作图片的平移、缩放、旋转
- Android(安卓)图片处理之图片叠加--Bitmap
- 实现图片浏览,改变透明度的方法
- Android(安卓)-------- BouncingJellyView 果冻视图(阻尼效果)
- Android动画特效实例
- Android(安卓)Volley使用之二:Volley请求网络图片
- 【android】为PopupWindow设置动画效果