相关疑难解决方法(0)

如何创建自定义拖动阴影?

我想要实现的目标:

我想在Android中创建拖放功能.我想使用特定的布局(与拖动的对象本身不同)作为拖动阴影.

我得到了什么结果:

我的方法都没有按预期工作 - 我最终没有可见的拖动阴影(虽然目标确实接收到了掉落).

我尝试了什么:

我试过了

  • drag_item在活动中展开布局,然后将其作为参数传递给阴影构建器的构造函数

  • drag_item在阴影生成器的onDrawShadow方法中膨胀布局,然后在其上绘制canvas

布局:

我的活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/container"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context="com.example.app.DragDropTestActivity"
              tools:ignore="MergeRootFrame">
    <TextView
        android:id="@+id/tvReceiver"
        android:text="Drop here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btnDragged"
        android:layout_height="wrap_content"
        android:text="Drag me"
        android:layout_width="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想用作拖动阴影的布局:

dragged_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dragged Item"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

源代码:

下面是用两种方法的代码(由下式表示1,BuilderOne并且2,BuilderTwo分别为):

package com.example.app;

import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button; …
Run Code Online (Sandbox Code Playgroud)

android drag-and-drop

7
推荐指数
1
解决办法
6459
查看次数

标签 统计

android ×1

drag-and-drop ×1