我正在创建一个应用程序,其中有可以通过触摸移动的按钮。一旦将它们放入特定区域,它们的位置就会变为特定位置。我的问题是在模拟器中运行我的代码按原样运行但是一旦我在设备上尝试它,似乎这些位置没有正确计算。
最初我通过坐标使用了特定位置,但正如您在下面的代码中所看到的,我从布局宽度和高度中获取区域,以便从任何使用的布局中计算出特定区域。
我已经阅读了一些关于AbsoluteLayout被弃用的文章,但我真的不明白为什么它不起作用。
代码:
OnTouchListener m_onTouchListenerOP = new OnTouchListener(){
@Override
public boolean onTouch(View p_v, MotionEvent p_event)
{
switch (p_event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
m_lastTouchX = p_event.getX();
m_lastTouchY = p_event.getY();
leftInicialOP = p_v.getLeft();
topInicialOP = p_v.getTop();
if(leftOrigemOP == 0 && topOrigemOP == 0){
leftOrigemOP = leftInicialOP;
topOrigemOP = topInicialOP;
}
break;
}
case MotionEvent.ACTION_MOVE:
{
m_dx = p_event.getX() - m_lastTouchX;
m_dy = p_event.getY() - m_lastTouchY;
m_posX = m_prevXop1 + m_dx;
m_posY = m_prevYop1 + m_dy;
if (m_posX > 0 && m_posY > 0 && (m_posX + p_v.getWidth()) < m_alTop.getWidth() && (m_posY + p_v.getHeight()) < m_alTop.getHeight())
{
p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), (int) m_posX, (int) m_posY));
m_prevXop1 = m_posX;
m_prevYop1 = m_posY;
}
break;
}
case MotionEvent.ACTION_UP:
{
m_dx = p_event.getX() - m_lastTouchX;
m_dy = p_event.getY() - m_lastTouchY;
m_posX = m_prevXop1 + m_dx;
m_posY = m_prevYop1 + m_dy;
if (m_posX > 0 && m_posY > 0 && (m_posX + p_v.getWidth()) < m_alTop.getWidth() && (m_posY + p_v.getHeight()) < m_alTop.getHeight() && (m_posX +(p_v.getWidth()/2) - (m_alTop.getWidth()/2)) < 30 && (m_posX + (p_v.getWidth()/2) - (m_alTop.getWidth()/2)) > -30 && (m_posY - ((3*m_alTop.getHeight() / 4))) < 30 && (m_posY - ((3*m_alTop.getHeight() / 4))) > -30){
if(operador2.getLeft() < 90 && operador2.getTop() < 194){
p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), ((m_alTop.getWidth()/2) - (p_v.getWidth()/2)), (3*m_alTop.getHeight() / 4)));
verificaRsp();
verificaInsercaoNovoPol(p_v,p_event);
}
else{
p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), leftOrigemOP, topOrigemOP));
m_prevXop1 = leftOrigemOP;
m_prevYop1 = topOrigemOP;
leftOrigemOP = 0;
topOrigemOP = 0;
}
}
else{
p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), leftOrigemOP, topOrigemOP));
m_prevXop1 = leftOrigemOP;
m_prevYop1 = topOrigemOP;
leftOrigemOP = 0;
topOrigemOP = 0;
}
希望你们能帮助我解决这些问题,或者给我一些消息。
此致