【问题标题】:Android Api 8. Get x and y from a View, and Set x and y on a ButtonAndroid Api 8. 从 View 中获取 x 和 y,并在 Button 上设置 x 和 y
【发布时间】:2011-10-23 22:11:43
【问题描述】:

我正在使用 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;
    }

【问题讨论】:

  • 只是好奇,你为什么需要这样做?我认为像这样设置 View 的位置在 Android 中通常是不受欢迎的,因为很难让它适用于所有屏幕。
  • 我需要模拟 api 8 未提供的拖放监听器
  • 我仍然需要一个解决方案,可能没有自定义布局,或者如果这是唯一的方法,请告诉我如何

标签: android button view set coordinates


【解决方案1】:

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

【讨论】:

  • 我将如何在按钮上设置这个值?
【解决方案2】:

我还没有尝试过,但我认为它会起作用:

int buttonX = 50; // Arbitrary values - use whatever you want
int buttonY = 100;
int viewX = myView.getLeft();
int viewY = myView.getTop();

Button newButton = new Button();
newButton.setPadding(buttonX - viewX, buttonY - viewY, 0, 0);
// Other button setup

这会将其设置为屏幕上 (50,100) 的绝对位置。如果您希望它相对于布局的角为 (50,100),请使用:

int buttonX = 50; // Arbitrary values - use whatever you want
int buttonY = 100;

Button newButton = new Button();
newButton.setPadding(buttonX, buttonY, 0, 0);
// Other button setup

这仅适用于不会自动定位其子内容的布局,例如 RelativeLayout。

【讨论】:

  • 我遇到了这个错误“方法 setTop(int) 未定义按钮类型”
  • 哦,你是对的。在 setX 和 setY 出现之前,这也是不可用的。对不起。
  • 我已经更新了我认为对你有用的技术的答案。
  • 我使用框架布局,并使用您的方法,按钮出现在上角。我做过类似t= image.getTop(); l= image.getLeft(); bt2.setPadding(l, t, 0, 0); System.out.println("**************** T: "+t +"--- L: "+l); 的事情,其中​​“bt2”是一个按钮,“image”是一个图像视图。但系统输出总是返回等于 0 的值。我将在上面添加整个方法。
  • 在您到达顶部和左侧时,它们可能尚未计算。您可能必须创建一个扩展 FrameLayout 的自定义布局。在您的自定义布局中,您可以覆盖 onLayout() 来设置子视图的位置。
【解决方案3】:

我已经使用 andengine 重新创建了我的拖放操作。效果更好,比以前更好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多