如何通过在android中触摸来拖动图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3669239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:16:49  来源:igfitidea点击:

how to drag an image by touching in android?

android

提问by James

how to drag an image by touching in android? Please Help me with a sample code.

如何通过在android中触摸来拖动图像?请帮助我提供示例代码。

回答by Prateek Raj

package com.examples.Touchmoveimage;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class Touchmoveimage extends Activity {

    int windowwidth;
    int windowheight;

    private LayoutParams layoutParams;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        windowheight = getWindowManager().getDefaultDisplay().getHeight();
        final ImageView balls = (ImageView) findViewById(R.id.ball);

        balls.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LayoutParams layoutParams = (LayoutParams) balls.getLayoutParams();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int x_cord = (int) event.getRawX();
                        int y_cord = (int) event.getRawY();

                        if (x_cord > windowwidth) {
                            x_cord = windowwidth;
                        }
                        if (y_cord > windowheight) {
                            y_cord = windowheight;
                        }

                        layoutParams.leftMargin = x_cord - 25;
                        layoutParams.topMargin = y_cord - 75;

                        balls.setLayoutParams(layoutParams);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
}

works good!!

效果很好!!

回答by M.ES

You need to read about Gestures especially Scroll - which is drag in Android - and Fling.

您需要阅读有关手势的信息,尤其是 Scroll(在 Android 中是拖动)和 Fling。

I recommend to you thistutorial. Its exactly what you are looking for.

我向您推荐教程。它正是您正在寻找的。

回答by Vinay

There is no direct way to do drag and drop in Android. You have to write some classes. Look into DragController.java, DragLayer.java in Launcher project.

在 Android 中没有直接的方法可以进行拖放操作。你必须写一些类。查看 Launcher 项目中的 DragController.java、DragLayer.java。