java - Android 初学者 - 在 DragEvent 中放置图像

标签 java android

我意识到已经发布了类似的问题,并且我查看了它们以及许多其他主题等以找到解决方案 - 我显然错过了显而易见的 - 因为我仍在学习基础知识!

目标:简单的拖放。 用户在屏幕上移动图像,然后将其放在另一个图像之上或屏幕上的任何位置。

API:>11

完成:

  • 可以拖动图像并将其放在另一个图像之上并获得响应(使用 Toast 进行确认)。
  • 可以将图像拖动到屏幕上的任何位置并获得响应(使用 Toast 进行确认)。

不工作:

  • 不能将图像拖到屏幕上的任何位置,也不能将图像放置在手指抬起的位置

我尝试了很多不同的方法,但总是编译错误。看看我的代码,谁能推荐一种干净简单的方法来将图像放在 ACTION_DRAG_ENDED(请记住我是初学者)

这是我的java代码:

public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

boolean okToDrop;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.oval).setOnTouchListener(new theTouchListener());
    findViewById(R.id.square).setOnTouchListener(new theTouchListener());
    findViewById(R.id.robot).setOnTouchListener(new theTouchListener());
    findViewById(R.id.oval).setOnDragListener(new theDragListener());
    findViewById(R.id.square).setOnDragListener(new theDragListener());
    findViewById(R.id.robot).setOnDragListener(new theDragListener());
}

private final class theTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.VISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

class theDragListener implements OnDragListener {
    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        int dragAction = dragEvent.getAction();
        View dragView = (View) dragEvent.getLocalState();
        if (dragAction == DragEvent.ACTION_DRAG_EXITED) {
            okToDrop = false;
        } else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) {
            okToDrop = true;
        } else if (dragAction == DragEvent.ACTION_DRAG_ENDED) {
            if (dropEventNotHandled(dragEvent) == true) {

                // Code to generate image goes here ***********************

                Context context = getApplicationContext();
                CharSequence text = "Action Dropped Not In Box!";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

                dragView.setVisibility(View.VISIBLE);
            }
        } else if (dragAction == DragEvent.ACTION_DROP && okToDrop) {
            ImageView i = (ImageView) findViewById(R.id.square);
            i.setImageResource(R.drawable.oval);

            dragView.setVisibility(View.INVISIBLE);

            Context context = getApplicationContext();
            CharSequence text = "Action Resulted In It Being Dropped In The Box";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }
        return true;
    }

    private boolean dropEventNotHandled(DragEvent dragEvent) {
        return !dragEvent.getResult();
    }

}

还有我的 xml:

<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"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/square"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/square_box" />

    <ImageView
        android:id="@+id/oval"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="58dp"
        android:layout_marginTop="58dp"
        android:src="@drawable/oval" />

    <ImageView
        android:id="@+id/robot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/oval"
        android:layout_below="@+id/square"
        android:layout_marginTop="70dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

最佳答案

虽然我不确定我的实现是否是最好的方法,但它确实有效。

在您的 MainActivity 中添加这些成员:

View root; // references root activity view
float dropX, dropY; // records position of where finger was lifted on the root

在你的 onCreate() 中:

root = findViewById(android.R.id.content); // get reference to root
// add a Draglistener to your root view
root.setOnDragListener(new OnDragListener() {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        if(action == DragEvent.ACTION_DROP) {
            // record position of finger lift
            dropX = event.getX();
            dropY = event.getY();
        }
        return false;
    }
});

最后,将这些行放在 //Code to generate image goes here 下:

dragView.setX(dropX - dragView.getWidth() / 2);
dragView.setY(dropY - dragView.getHeight() / 2);

希望这对您有所帮助。

关于java - Android 初学者 - 在 DragEvent 中放置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14150170/

相关文章:

java - 单击按钮将电子邮件发送到某个地址

android - 我可以 System.load 包含在不同 apk 中的 native 库吗?

android - Android 中特定于 fragment 的选项和上下文菜单

java - 用于安装 blitz javaSpaces 的 jini 入门套件 2.1

java - 当并非所有属性都在同一个类中时填充 JavaFX TableView

java - Android:无法从给定的 url 播放 mp3 文件

java - H2 数据库 - 主键冲突替换

android - 在不使用 native 操作调用 Intent 的情况下从 Android 应用程序调用号码?

java - fragment 添加事务后, Activity 中的小部件布局仍然出现

java - 为什么我的一个类的自定义成员变量在其他类中没有更新?