android拖动imageview实现复制效果
public class MyDragShadowBuilder extends DragShadowBuilder { // 拖动阴影的区域 private static Drawable shadow; // 储存绘制的拖动阴影图像 private static Bitmap newBitmap; public MyDragShadowBuilder(View arg0) { super(arg0); // TODO Auto-generated constructor stub shadow = new ColorDrawable(Color.LTGRAY);// 浅灰色 } // 在该方法中绘制拖动阴影图像 实例化newBitmap变量 @Override public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) { // TODO Auto-generated method stub super.onProvideShadowMetrics(shadowSize, shadowTouchPoint); int width, heigth; // 设置拖动阴影的宽度/高度为原宽/高度的1.5倍 width = (int) (getView().getWidth() * 1.5); heigth = (int) (getView().getHeight() * 1.5); // 设置拖动图像的绘制 区域 shadow.setBounds(0, 0, width, heigth); // 设置拖动阴影图像的宽度和高度 shadowSize.set(width, heigth); // 设置手指在拖动图像的位置 设置为中点 shadowTouchPoint.set(width / 2, heigth / 2); if (getView() instanceof ImageView) { // getView()方法返回的值就是构造方法传入的arg0 参数 ImageView imageView = (ImageView) getView(); // 获取drawable对象 Drawable drawable = imageView.getDrawable(); // 获取imageview的bitmap Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); // 创建一个新的bitmap newBitmap = bitmap.createBitmap(width, heigth, Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); // 将图像绘制在画布上,但现在还没有正式将图像绘制在阴影图像上,目前只是将bitmap放大并绘制在newbitmap上 canvas.drawBitmap(newBitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(0, 0, width, heigth), null); } } @Override public void onDrawShadow(Canvas canvas) { // TODO Auto-generated method stub super.onDrawShadow(canvas); // 将图像正式绘制在阴影图像上 canvas.drawBitmap(newBitmap, 0, 0, new Paint()); } }
用来实现的主窗口
public class MainActivity extends Activity implements OnDragListener { private FrameLayout dragdropRegin; // 拖动到图像 private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dragdropRegin = (FrameLayout) findViewById(R.id.fl_dragdrop_region); imageView = (ImageView) findViewById(R.id.imageview); // 为目标设置拖动监听器 imageView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub DragShadowBuilder mysBuilder = new MyDragShadowBuilder( imageView); // 开始拖动,方法中第一参数是ClipData类型的对象。用于传递剪切板数据,可以为null v.startDrag(null, mysBuilder, null, 0); return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onDrag(View v, DragEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { // 开始拖动 case DragEvent.ACTION_DRAG_STARTED: System.out.println("drag started"); break; // 进入目标区域 case DragEvent.ACTION_DRAG_ENTERED: System.out.println("drag entered"); break; // 在目标区域移动 case DragEvent.ACTION_DRAG_LOCATION: System.out.println("drag location x=" + event.getX() + " y =" + event.getY()); break; // 离开目标区域 case DragEvent.ACTION_DRAG_EXITED: System.out.println("drage exited"); break; // 在目标区域放下ImageView控件 case DragEvent.ACTION_DROP: System.out.println("drag drop"); ImageView imageView = (ImageView) getLayoutInflater().inflate( R.layout.image, null); LayoutParams layoutParams = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // 由于framelayout 不能直接使用坐标设置子视图的位置 这里用左边距和顶边距 layoutParams.leftMargin = (int) event.getX(); layoutParams.topMargin = (int) event.getY(); // 添加到视图中,完成复制 dragdropRegin.addView(imageView, layoutParams); break; case DragEvent.ACTION_DRAG_ENDED: System.out.println("drag ended"); default: return false; } return true; } }以及布局文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <FrameLayout android:id="@+id/fl_dragdrop_region" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ic_launcher" /> </FrameLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" android:src="@drawable/ic_launcher" />
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。