如何使拖放放大器;下拉按钮中的Andr​​oid放大器、拖放、按钮、oid

2023-09-12 07:46:47 作者:你是年少的欢喜

我想打一个拖放按钮。你希望它做的,它就留在那里拖动。下面code仅缩放按钮,不改变其立场。

 包com.dynamic;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.Button;
进口android.widget.FrameLayout;
进口android.widget.ImageView;
进口android.widget.TextView;

进口com.dynamic.R;
进口com.dynamic.R.layout;


公共类DY延伸活动{
     INT状态;
     私人的FrameLayout布局;
     ImageView的形象;
     按钮B;
     的LayoutParams PARAMS;
     公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.frm);
            最终按钮电视=(按钮)findViewById(R.id.txt_birth);
            tv.setDrawingCacheEnabled(真正的);

            布局=(的FrameLayout)findViewById(R.id.frm);
            PARAMS =新的LayoutParams(100,100);


            PARAMS =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            tv.setOnTouchListener(新View.OnTouchListener(){

        @覆盖
        公共布尔onTouch(视图V,MotionEvent我){
            // TODO自动生成方法存根

            如果(me.getAction()== MotionEvent.ACTION_DOWN){
                的System.out.println(上);
                状态= 0;

            }
            如果(me.getAction()== MotionEvent.ACTION_UP){
                状态= 1;
                //Log.i("Drag,停止拖动);
            }否则,如果(me.getAction()== MotionEvent.ACTION_MOVE){
                如果(状态== 0){
                    的System.out.println(拖);

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

                }
            }
            返回false;
        }

    });
}
}
 

解决方案

我正在做类似的事情。下面是我使用的OnTouchListener:

  myOnTouchListener =新OnTouchListener(){
         公共布尔onTouch(视图V,MotionEvent我){
             如果(me.getAction()== MotionEvent.ACTION_DOWN){
                 oldXvalue = me.getX();
                 oldYvalue = me.getY();
                 Log.i(MyTag的操作下+ oldXvalue +,+ oldYvalue);
             }否则,如果(me.getAction()== MotionEvent.ACTION_MOVE){
                的LayoutParams PARAMS =新的LayoutParams(v.getWidth(),v.getHeight(),(int)的(me.getRawX() - (v.getWidth()/ 2)),(int)的(me.getRawY() - ( v.getHeight())));
                v.setLayoutParams(PARAMS);
             }
             返回true;
         }
     };
 

v是,你是想动,你的情况,你会替换V配合您的按钮的观点。还要注意的是,为了得到这个工作,我不得不使用一个AbsoluteLayout在我的XML文件中的父视图。我知道,这是pcated德$ P $,但它似乎更符合逻辑的使用则RelativeLayout的,并试图动态地设置页边距来移动视图。我用了新的x和y坐标公式试图让这个观点是集中在你的手指,而你是移动它。但它不是相当完美,这取决于视图的大小它仍然会从你的手指稍微偏离中心。

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;
        }

    });
}
}

解决方案

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 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.