如何制作 Drag &Android 中的下拉按钮 [英] How to make Drag & Drop Button in Android

查看:14
本文介绍了如何制作 Drag &Android 中的下拉按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个拖放按钮.将它拖到你想要它做的地方,它就呆在那里.下面的代码只缩放按钮,不改变它的位置.

I want to make a drag and drop button. Drag it where you want it to do and it stays there. Below code only scales the button, doesn't change its position.

package com.dynamic;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dynamic.R;
import com.dynamic.R.layout;


public class dy extends Activity {
     int status;
     private FrameLayout layout;
     ImageView image;
     Button b;
     LayoutParams params;
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.frm);
            final Button tv=(Button)findViewById(R.id.txt_birth);
            tv.setDrawingCacheEnabled(true);

            layout = (FrameLayout) findViewById(R.id.frm);
            params = new LayoutParams(100,100);


            params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            tv.setOnTouchListener(new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent me) {
            // TODO Auto-generated method stub

            if (me.getAction() == MotionEvent.ACTION_DOWN) {
                System.out.println("up");
                status = 0;

            }
            if (me.getAction() == MotionEvent.ACTION_UP) {
                status = 1;
                //Log.i("Drag", "Stopped Dragging");
            } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
                if (status == 0) {
                    System.out.println("Dragging");

                    tv.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
                //  b.setPadding(0,50,0,0);
                    tv.invalidate();

                }
            }
            return false;
        }

    });
}
}

推荐答案

我正在做类似的事情.这是我正在使用的 OnTouchListener:

I am working on something similar to this. Here is the OnTouchListener that I am using:

         myOnTouchListener = new OnTouchListener() {
         public boolean onTouch(View v, MotionEvent me){
             if (me.getAction() == MotionEvent.ACTION_DOWN){
                 oldXvalue = me.getX();
                 oldYvalue = me.getY();
                 Log.i(myTag, "Action Down " + oldXvalue + "," + oldYvalue);
             }else if (me.getAction() == MotionEvent.ACTION_MOVE  ){
                LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight(),(int)(me.getRawX() - (v.getWidth() / 2)), (int)(me.getRawY() - (v.getHeight())));
                v.setLayoutParams(params);
             }
             return true;
         }
     };

v 是您要移动的视图,在您的情况下,您可以将 v 替换为您的按钮.另请注意,为了使其正常工作,我必须在我的 xml 文件中使用 AbsoluteLayout 作为父视图.我知道它已被弃用,但使用它似乎更合乎逻辑,然后使用 RelativeLayout 并尝试动态设置边距以移动视图.我用于新的 x 和 y 位置的公式试图使视图在您移动时以您的手指为中心.但它不是很完美,根据视图的大小,它仍然会偏离您的手指的中心.

v is the view that you are wanting to move, in your case it you'd replace v with your button. Also note that in order to get this to work I had to use an AbsoluteLayout as the parent view in my xml file. I know that it is deprecated but it seemed more logical to use that then a RelativeLayout and trying to set the margins dynamically to move the view around. The formula that I used for the new x and y positions tries to make it so that the view is centered on your finger while you are moving it. But its not quite perfect, depending on the size of the view it will still be a little off center from your finger.

这篇关于如何制作 Drag &Android 中的下拉按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆