yahya
yahya

Reputation: 4860

Dragging Image?

I'm try to do some app which has to unlock when user moved image to a certain place... But the problem is when the user touch somewhere else except the image, it still tries to move and it gives this error: java.lang.ClassCastException: android.widget.AbsoluteLayout$LayoutParams

Here's my codes:

public class Main extends Activity {

private View selected_item;
private int offset_x = 0;
private int offset_y = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ViewGroup vg = (ViewGroup) findViewById(R.id.lout);

    vg.setOnTouchListener(new View.OnTouchListener() {

        @SuppressWarnings("deprecation")
        public boolean onTouch(View v, MotionEvent event) {
                      switch (event.getAction()) {                          
                      case MotionEvent.ACTION_MOVE:
                          int x = (int) event.getX() - offset_x;
                          int y = (int) event.getY() - offset_y;
                          Log.e("SONUC", "SONUC1: " + x + ", " + y);
                          int w = getWindowManager().getDefaultDisplay().getWidth() - 25;
                          int h = getWindowManager().getDefaultDisplay().getHeight() - 25;
                          if (x > w)
                              x = w;
                          if (y > h)
                              y = h;
                          Log.e("SONUC", "SONUC2: " + x + ", " + y);
                          AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(
                                  new ViewGroup.MarginLayoutParams(
                                          AbsoluteLayout.LayoutParams.WRAP_CONTENT,
                                          AbsoluteLayout.LayoutParams.WRAP_CONTENT));                              
                          lp.x = x;
                          lp.y = y;
                          Log.e("SONUC", "SONUC3: " + lp);
                          selected_item.setLayoutParams(lp);       

                          break;

                      case MotionEvent.ACTION_UP:
                          offset_x = (int) event.getX() - offset_x;
                          offset_y = (int) event.getY() - offset_y;
                          Log.e("SONUC", "SONUC5: " + offset_x + ", " + offset_y);
                          selected_item = v;
                          if (offset_x < 220 && offset_x > 150 && offset_y < 330 && offset_y > 300)
                              startActivity(new Intent("com.yahya.GeneralTraining.UNLOCKED"));
                          break;

            default:
                break;
            }
            return true;
        }
    });

    ImageView img = (ImageView) findViewById(R.id.imageView1);
    img.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                offset_x = (int) event.getX();
                offset_y = (int) event.getY();
                Log.e("SONUC", "SONUC4: " + offset_x + ", " + offset_y);                    
                selected_item = v;
                break;


            default:
                break;
            }
            return false;

        }
    });

}

}

Upvotes: 2

Views: 391

Answers (2)

user936414
user936414

Reputation: 7634

You can provide a check in the onTouchListener for viewgroup to confirm that the image is clicked initially.

vg.setOnTouchListener(new View.OnTouchListener() {

    @SuppressWarnings("deprecation")
    public boolean onTouch(View v, MotionEvent event) {
                if(selected_item.getId() == R.id.image){
                    switch (event.getActionMasked()) {
                      //your code
                    }
                }
        }
        return true;
    }
});

Upvotes: 2

Pratik
Pratik

Reputation: 30855

1st thing don't create new object of the LayoutParam just get LayoutParam object from the container like image or your layout

suppose getting for ImageView then

LayoutParam lp = imageView.getLayoutParam();

lp.width = newWidth;
lp.height = newHeight;

now change the size and set it in imageView.setLayoutParam()

imageView.setLayoutParam(lp);

and another thing is you also set the touch listener for ViewGroup just set for the ImageView not set the touch listener for the ViewGroup and in the touch listener implementation return true to indicate the touch listener was implemented

Upvotes: 0

Related Questions