【问题标题】:setTranslationX and setTranslationY make wrong position of Button in Android. Why?setTranslationX 和 setTranslationY 使 Button 在 Android 中的位置错误。为什么?
【发布时间】:2016-09-14 15:14:07
【问题描述】:

我有两个按钮:1 和 2。第一个按钮将位于布局中心,第二个按钮将使用 setTranslationX and setTranslationY 进行翻译,代码就是这样做的

    btn1 = (Button) findViewById(R.id.btn1);
    setRotateButton(btn1,2,1);
    btn2 = (Button) findViewById(R.id.btn2);
    setRotateButton(btn2,2,2);

setRotateButton 在哪里

public void setRotateButton(Button btn,int numViews,int index)
{
    //Locate button in center
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(130, 130);
    lp.gravity = Gravity.CENTER;
    btn.setLayoutParams(lp);
    //Translation
    if (index>1) //To avoid first case
    {
        float angleDeg = index * 360.0f / numViews - 90.0f;
        float angleRad = (float)(angleDeg * Math.PI / 180.0f);
        btn.setTranslationX(400 * (float)Math.cos(angleRad));
        btn.setTranslationY(400 * (float)Math.sin(angleRad));
    }    
}

我发现一个问题,当我使用setTranslationX,setTranslationY 时,按钮 2 的位置可以转换到另一个位置,但我得到的按钮 2 位置的数字错误。它总是返回相同的值。检查它。我使用onTouch 方法(mainFrame 是一个 FrameLayout,其中包含这两个按钮)。你能帮我修复setRotateButton中的功能吗?非常感谢。

 @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        mainFrame.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int x = (int) event.getX();
                int y = (int) event.getY();

                for (int i = 0; i < mainFrame.getChildCount(); i++) {
                    View current = mainFrame.getChildAt(i);
                    if (current instanceof Button) {
                        Button b = (Button) current;
                        Log.d("TAG","Btn pos:"+String.valueOf(b.getLeft())+";"+String.valueOf(b.getRight())+";"+String.valueOf(b.getTop())+";"+String.valueOf(b.getBottom()));
                        if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(defaultStates);
                            b.getBackground().setAlpha(255);

                        }

                        if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(STATE_PRESSED);
                            b.getBackground().setAlpha(150);
                            b.performClick();

                            if (b != mLastButton) {
                                mLastButton = b;

                            }
                        }

                    }

                }
                return true;
            }

        });

    }

这是日志结果

当我将手指移到按钮 1 内并移到按钮外时

09-15 00:15:50.981 20547-20547/com.example.circlelayout D/TAG: Btn pos:0;150;300;450
09-15 00:01:40.891 20547-20547/com.example.circlelayout D/TAG: Btn pos:397;527;485;615

当我将手指移入按钮 2 并移出时。它们是相同的值

09-15 00:01:40.891 20547-20547/com.example.circlelayout D/TAG: Btn pos:397;527;485;615
09-15 00:01:40.891 20547-20547/com.example.circlelayout D/TAG: Btn pos:397;527;485;615
09-15 00:01:40.891 20547-20547/com.example.circlelayout D/TAG: Btn pos:397;527;485;615

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    getLeft() 之类的方法没有考虑到 View 已被翻译。它们总是返回在布局过程中计算的初始值。

    因此,您需要改用 getX()getY() 并计算新的底角和右上角。例如:

    int bX = b.getX();
    intbY = b.getY();
    
    if (isPointWithin(x, y,
                 bX, bX + (b.getRight() - b.getLeft()),
                 bY, bY + (b.getBottom() - b.getTop()) ) )
    {
         b.getBackground().setState(STATE_PRESSED);
         b.getBackground().setAlpha(150);
         b.performClick();
    
         if (b != mLastButton) {
             mLastButton = b;
         }
    }       
    

    【讨论】:

      猜你喜欢
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多