拖动imageview来互换俩个图片
来源:互联网 发布:ih5 知乎 编辑:程序博客网 时间:2024/04/28 19:57
直接上代码:
package xutao.myapplication;import android.content.ClipData;import android.graphics.drawable.Drawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.DragEvent;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.ImageView;import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity { private ImageView mIv1; private LinearLayout mLl1; private ImageView mIv2; private LinearLayout mLl2; private Drawable normalShape; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); normalShape = getResources().getDrawable(R.drawable.shape); initView(); initEvent(); } private void initEvent() { mIv1.setOnTouchListener(MyTouchEvent); mIv2.setOnTouchListener(MyTouchEvent); mLl1.setOnDragListener(MyDragListener); mLl2.setOnDragListener(MyDragListener); } private void initView() { mIv1 = (ImageView) findViewById(R.id.iv1); mLl1 = (LinearLayout) findViewById(R.id.ll1); mIv2 = (ImageView) findViewById(R.id.iv2); mLl2 = (LinearLayout) findViewById(R.id.ll2); } View.OnTouchListener MyTouchEvent = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { ClipData data = ClipData.newPlainText("", ""); View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); v.startDrag(data, shadowBuilder, v, 0); v.setAlpha((float) 0.5); return true; } else { return false; } } }; View.OnDragListener MyDragListener=new View.OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { View visitorView = (View) event.getLocalState(); ViewGroup visitorOwner = (ViewGroup) visitorView.getParent(); ViewGroup visitedOwner = (ViewGroup) v; View visitedImage = (View)visitedOwner.getChildAt(0); switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: break; case DragEvent.ACTION_DRAG_ENTERED: v.setAlpha((float)0.7); v.setScaleX(0.7f); v.setScaleY(0.7f); v.setBackground(normalShape); break; case DragEvent.ACTION_DRAG_EXITED: v.setAlpha((float)1.0); v.setScaleX(1); v.setScaleY(1); v.setBackground(normalShape); break; case DragEvent.ACTION_DROP: visitorView.setAlpha((float)1.0); visitedImage.setAlpha((float)1.0); if (visitorOwner != visitedOwner) { /* * swap the imageviews from the containers */ visitedOwner.removeView(visitedImage); visitorOwner.removeView(visitorView); visitorOwner.addView(visitedImage); LinearLayout container = (LinearLayout) v; container.addView(visitorView); } else { } break; case DragEvent.ACTION_DRAG_ENDED: v.setBackgroundDrawable(normalShape); v.setAlpha((float)1.0); visitedImage.setAlpha((float)1.0); default: break; } return true; } };}
layout.XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="xutao.myapplication.MainActivity"> <LinearLayout android:id="@+id/ll1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </LinearLayout> <LinearLayout android:id="@+id/ll2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="100dp"> <ImageView android:id="@+id/iv2" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="centerCrop" android:src="@drawable/logo" /> </LinearLayout></LinearLayout>
demo下载链接:http://download.csdn.net/download/xutaojxx/10036214
阅读全文
0 0
- 拖动imageview来互换俩个图片
- 在ImageView中拖动图片
- Android ImageView图片拖动效果
- 高仿微信图片放大拖动浏览,自定义ImageView
- 自定义ImageView实现图片的拖动、缩放和边界回弹
- 自定义ImageView来实现图片旋转
- 拖动 tableView 来缩放顶部图片
- [Android][ImageView]ImageView浏览图片的2个方式
- 08-ImageView使用RatingBar来进行对图片评分
- 通过拖动滑动块来改变图片透明度
- 自定义类继承ImageView 实现多点图片触碰的拖动和缩放
- 自定义ImageView实现图片的单指拖动和两指缩放
- 自定义类继承ImageView 实现多点图片触碰的拖动和缩放
- Android开发之ImageView通过matrix实现两点缩放和图片拖动
- Android自定义imageview可对图片进行多点缩放和拖动
- 自定义的ImageView控制,可对图片进行多点触控缩放和拖动
- Android可拖动的ImageView
- 判断ImageView当前的图片是哪个图片,然后根据当前图片来做一些操作
- 【BZOJ1015】【JSOI2008】星球大战 并查集
- 洛谷P3258 [JLOI2014]松鼠的新家
- Windows XP下如何清理日志文件
- MySQL技术内幕 InnoDB存储引擎 第2版
- CakePHP框架
- 拖动imageview来互换俩个图片
- Zookeep启动异常:Error contacting service. It is probably not running.
- PCL编译安装
- C#---将字符串按空格分隔
- 分冶--区间合并
- 购物车之 全选 反选 批量删除 包邮 总价
- Java单例模式的几种写法
- Windows 环境下使用 GCC
- django引入现有数据库