View 可以实现拖拽效果(Drag),根据参考API 文档,实现也简单。

(Api 文档: https://tool.oschina.net/uploads/apidocs/android/guide/topics/ui/drag-drop.html)

以下简单实现了拖拽效果,在拖拽的过程中,根据拖拽事件 更新ImageView 的背景颜色,方便理解拖拽事件的处理。

但是  ACTION_DROP 没有执行,需要检查一下原因。

此外,下一步进行研究 拖拽到指定目标View区域, 考虑能拖拽的范围等等

package com.example.dragtest;import androidx.appcompat.app.AppCompatActivity;import androidx.constraintlayout.widget.ConstraintLayout;import androidx.constraintlayout.widget.ConstraintSet;import android.content.ClipData;import android.content.ClipDescription;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.view.DragEvent;import android.view.View;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    // Create a string for the ImageView label    private static final String IMAGEVIEW_TAG = "icon bitmap";    private Context mContext;    // Creates a new ImageView    ImageView mImageView;    Bitmap mIconBitmap;    private View.OnDragListener mDragListener;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mContext = this;        ConstraintLayout layout = findViewById(R.id.main_layout);        // 动态添加 ImageView, 设置Tag, 拖拽时可以判断时哪个view        mImageView = new ImageView(this);        mIconBitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);        mImageView.setImageBitmap(mIconBitmap);        mImageView.setTag(IMAGEVIEW_TAG);        mImageView.setBackgroundColor(getResources().getColor(R.color.colorAccent));        // 使用ConstraintLayout 方式动态添加ImageView 到布局中,与Drag 无关,可以直接使用图片资源代替        int imageViewId = mImageView.generateViewId();        ConstraintSet constraintSet = new ConstraintSet();        constraintSet.connect(imageViewId, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);        constraintSet.connect(imageViewId, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);        constraintSet.applyTo(layout);        layout.addView(mImageView);        //1. 简单实现拖拽效果        mImageView.setOnLongClickListener(new View.OnLongClickListener() {            @Override            public boolean onLongClick(View v) {                //API 文档使用 ClipData.MIMETYPE_TEXT_PLAIN, 但是无法使用。(MIMETYPES_TEXT_PLAIN)                String[] textPlain = new String[]{"text/plain"};                //1.1 ClipData 跟剪贴板有关??                ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());                ClipData dragData = new ClipData((CharSequence) v.getTag(), textPlain, item);                //1.2 拖拽动画效果有默认实现  DragShadowBuilder                //开始执行拖拽                v.startDrag(dragData, new View.DragShadowBuilder(v), null, 0);                return true;            }        });        //2. 自定义拖拽监听器,响应各种拖拽事件 (start/enter/location/drop/end)        mDragListener = new MyDragEventListener();        mImageView.setOnDragListener(mDragListener);    }    protected class MyDragEventListener implements View.OnDragListener {        @Override        public boolean onDrag(View v, DragEvent event) {            final int action = event.getAction();            switch (action) {                case DragEvent.ACTION_DRAG_STARTED:                    if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {                        v.setBackgroundColor(Color.BLUE); // 设置图片背景颜色                        v.invalidate();                        // 返回true才能收到后面的时间,否则只能收到end                        return (true);                    } else {                        return (false);                    }                    //break;                case DragEvent.ACTION_DRAG_ENTERED:                    v.setBackgroundColor(Color.RED); // 设置图片背景颜色                    v.invalidate();                    return true;                //break;                case DragEvent.ACTION_DRAG_LOCATION:                    return true;                case DragEvent.ACTION_DRAG_EXITED:                    v.setBackgroundColor(Color.BLACK); // 设置图片背景颜色                    v.invalidate();                    return true;                //并没有执行,需要检查                case DragEvent.ACTION_DROP:                    ClipData.Item item = event.getClipData().getItemAt(0);                    CharSequence dragData = item.getText();                    Toast.makeText(mContext, "Dragged data is " + dragData, Toast.LENGTH_LONG).show();                    v.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));                    v.invalidate();                    return true;                case DragEvent.ACTION_DRAG_ENDED:                    v.setBackgroundColor(Color.YELLOW); // 设置图片背景颜色                    v.invalidate();                    if (event.getResult()) {                        Toast.makeText(mContext, "The drop was handled.", Toast.LENGTH_LONG).show();                    } else {                        Toast.makeText(mContext, "The drop didn't work.", Toast.LENGTH_LONG).show();                    }                    return true;                default:                    Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");                    break;            }            return false;        }    }}

 

 

更多相关文章

  1. andorid 将布局文件(layout)转换为图片(Bitmap)简单使用详解
  2. Android layer-list 圆角阴影背景
  3. Android画各种圆,饼图,环图,圆形图片
  4. android TextView 结合SpannableString对部分内容设置颜色、字体
  5. android 图片压缩的几种方法
  6. 2011.10.14——— android 仿照微信的图片展示功能 之 基本功能
  7. 流媒体开发之-服务器图片的加载
  8. Android开发教程--设置ImageView图片的显示比例

随机推荐

  1. 谷歌Android与Chrome合一你怎么看 搭载An
  2. 如何在 Android(安卓)Studio 中引用不在
  3. Android启动App时白屏的解决方法
  4. Android(安卓)Fragment基础
  5. Android开发笔记(一百七十一)使用Glide加载
  6. 魅族note(电信版)缩略图显示不正确问题
  7. 某android平板项目开发笔记----aChartEng
  8. android基本组件介绍
  9. 摩托罗拉发布最新款Android(安卓)3.0平板
  10. 内存使用总结篇 -- Android(安卓)内存优