Android Api 8. 从视图中获取 x 和 y,并在按钮上设置 x 和 y

2023-12-14

我正在使用 API 8 进行编码。
我需要从视图中获取坐标 X 和 Y,并将它们设置为新按钮的坐标。
我尝试了不同的方法但没有任何作用......

setX 和 getX 方法仅适用于 api 级别 11,我需要一种方法在 API8 上执行此操作。

这是我的方法,允许创建按钮的可拖动副本(此副本是一个图像视图,当我放下它时,它成为一个新按钮)

public boolean dragCopy(View view, MotionEvent me, Button bt){ 

        int x,y,w,h,id,t,l;
        int cont=-1;
        int coord[] = new int[2];


        bt2=new Button(this);


        id = bt.getId();
        bt2.setId(id);

        Resources res = getResources();

        cont = FindCont(bt, vector); 

        dr = getLetter(bt, dr,  res, vector, cont);
        w=dr.getIntrinsicWidth();
        h=dr.getIntrinsicHeight();


        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            //clicked on the button. DRAG START
            status = START_DRAGGING;
            image = new ImageView(this);

            //set image drawable
            image.setImageDrawable(dr);
            image.setPadding((int)bt.getLeft(),(int)bt.getTop(), 0, 0);
            layout.addView(image, params);


        }
        if (me.getAction() == MotionEvent.ACTION_UP) {

            //button released. DROP
                        status = STOP_DRAGGING;
            x = (int) me.getRawX();
            y = (int) me.getRawY();

                        //create button compy from image
            bt2.setBackgroundDrawable(dr);
            bt2.setVisibility(View.VISIBLE);

                        //PROBLEMS  
            t= image.getTop();
            l= image.getLeft();
            bt2.setPadding(l, t, 0, 0);
            System.out.println("**************** T: "+t +"--- L: "+l);
            image.setVisibility(View.GONE);
                bt2.setLayoutParams(image.getLayoutParams());

                bt2.setWidth(w);
            bt2.setHeight(h);

            layout.addView(bt2);
                bt2.setDrawingCacheEnabled(true);
            bt2.setOnTouchListener(this);
            bt2.invalidate(); 
            image.invalidate();


        } else if (me.getAction() == MotionEvent.ACTION_MOVE) {

                        //i'm moving the image
            if (status == START_DRAGGING) {

                image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
                image.invalidate();

            }
        }
        return false;
    }

你可以尝试 getTop() 和 getLeft() 吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Api 8. 从视图中获取 x 和 y,并在按钮上设置 x 和 y 的相关文章

随机推荐