- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个按钮:1 和 2。第一个按钮将位于布局的中心,第二个将使用 setTranslationX 和 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 内,然后移到按钮 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 内并移到按钮 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
最佳答案
像 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;
}
}
关于android - setTranslationX 和 setTranslationY 使 Button 在 Android 中的位置错误。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39494045/
正如标题所说,我想了解该方法到底在做什么。 首先我已经仔细检查过 android 的坐标系统是这样工作的: coordinate system 次要 - 请花一点时间检查我的 android stud
setX()、setY() 和 setTranslationX()、setTranslationY() 有什么区别?他们是在现有坐标上设置偏移量还是设置绝对位置? 如果他们设置绝对坐标,这不会与父布局
本文整理了Java中com.ufreedom.floatingview.transition.YumFloating.setTranslationY()方法的一些代码示例,展示了YumFloating
有谁知道为什么 androidx.constraintlayout.widget.Group不适用于 setTranslationY。预先感谢您的帮助。 Group groupAppList = fr
我在 ViewPager.pageTransformer 中使用 view.setTranslationY() 时遇到问题 当软键盘打开时页面转换到其他页面时,会出现两个问题: 1- 焦点停留在旧页面
我有两个按钮:1 和 2。第一个按钮将位于布局的中心,第二个将使用 setTranslationX 和 setTranslationY 进行翻译 btn1 = (Button) findVie
我是一名优秀的程序员,十分优秀!