0

I've a AbsoluteLayout and I wanna move my ImageView around this Layout trough my touch points , Look at my code , Everything is fine when I touch any points, Points are saved in a variable , but ImageView was disappear , Any Ideas?

My XML Layout :

    <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>

My Codes:

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;
    }
    });
Mr.James
  • 376
  • 2
  • 8
  • 25

2 Answers2

1

That's a behavior to be expected, given that you just scroll the window instead of actually moving the view. The right way will be to override the onTouchEvent method of your layout.

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);
    }
}
asenovm
  • 6,397
  • 2
  • 41
  • 52
  • what's the error? In order to implement this you shoud create your own class that extends AbsoluteLayout and override its onTouch method – asenovm Dec 10 '11 at 13:57
  • We must implement onTouch() or onTouchEvent() ?! because onTouch methods need an object to implementation. – Mr.James Dec 10 '11 at 14:04
  • Yes, should be onTouchEvent, but you should implement onTouchEvent of the absolute layout. The implementation you provided below is correct in case it is in the representing you AbsoluteLayout - i.e `public class MyAbsoluteLayout extends AbsoluteLayout{ ... onTouchEvent ...}` – asenovm Dec 10 '11 at 15:26
  • I've edited my answer. The provided implementation should work, although I haven't really tested it as i don't have a testing device and the emulator will take too much time to load. – asenovm Dec 10 '11 at 15:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5739/discussion-between-mr-james-and-ilate) – Mr.James Dec 10 '11 at 15:42
0
@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;
}
Mr.James
  • 376
  • 2
  • 8
  • 25