【问题标题】:Move ImageView through onTouch()通过 onTouch() 移动 ImageView
【发布时间】:2012-01-17 09:41:27
【问题描述】:

我有一个AbsoluteLayout,我想通过我的接触点围绕这个布局移动我的ImageView,看看我的代码,当我接触任何点时一切都很好,点保存在一个变量中,但是@987654324 @ 消失了,有什么想法吗?

我的 XML 布局:

    <ImageView
        android:id="@+id/FirstBall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="59dp"
        android:layout_y="446dp"
        android:src="@drawable/redball" />

</AbsoluteLayout>

我的代码:

FstBall=(ImageView)findViewById(R.id.FirstBall);
FstBall.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    int action = event.getAction();
    float FnlResX = 0;
    float FnlResY = 0;
    if (action == MotionEvent.ACTION_MOVE){
    FnlResX = x;
    FnlResY = y;
    int tst1 = (int) FnlResX;
    int tst2= (int) FnlResY;
    FstBall.scrollBy(tst1, tst2);
    }
    return true;
    }
    });

【问题讨论】:

标签: android


【解决方案1】:

这是意料之中的行为,因为您只是滚动窗口而不是实际移动视图。正确的方法是覆盖布局的 onTouchEvent 方法。

public class MyAbsoluteLayout extends AbsoluteLayout{

   private static final String TAG = MyAbsoluteLayout.class.getSimpleName();

   private int mDepth;

   @Override
   public boolean onTouchEvent(MotionEvent event){
        final View child = getChildAt(mDepth);
        if(child == null){
            Log.e(TAG, "no child at selected depth");
            return super.onTouchEvent(event);
        }
        final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)   child.getLayoutParams();
        params.leftMargin = event.getX();
        params.topMargin = event.getY();
        child.setLayoutParams(params);
        return super.onTouchEvent(event);
    }

    public void setMovingChildDepth(final int depth){
       mDepth = depth;
    }
}

public class MyActivity extends Activity{

    public void onCreate(Bundle data){
        super.onCreate(data);

        final MyAbsoluteLayout layout = new MyAbsoluteLayout(this);
        final ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(FILL_PARENT,FILL_PARENT);
        layout.setLayoutParams(layoutParams);

        final ImageView imageView = new ImageView(this);
        final ViewGroup.MarginLayoutParams viewParams = new ViewGroup.MarginLayoutParams(100,100);
        params.leftMargin = 100;
        params.topMargin = 100;
        imageView.setBackgroundDrawable(yourDrawable);
        layout.addView(imageView,params);

        layout.setMovingChildDepth(layout.indexOfChild(imageView));

        setContentView(layout);
    }
}

【讨论】:

  • 错误是什么?为了实现这一点,您应该创建自己的扩展 AbsoluteLayout 并覆盖其 onTouch 方法的类
  • 我们必须实现 onTouch() 或 onTouchEvent() ?!因为 onTouch 方法需要一个对象来实现。
  • 是的,应该是onTouchEvent,但是你应该实现绝对布局的onTouchEvent。您在下面提供的实现是正确的,以防它代表您 AbsoluteLayout - 即 public class MyAbsoluteLayout extends AbsoluteLayout{ ... onTouchEvent ...}
  • 我已经编辑了我的答案。提供的实现应该可以工作,虽然我没有真正测试过它,因为我没有测试设备,并且模拟器将花费太多时间来加载。
【解决方案2】:
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(FstBall.getLayoutParams());
params.x = (int) event.getX();
params.y = (int) event.getY();
FstBall.setLayoutParams(params);
return true;
}

【讨论】:

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