0

I am making an android application in which i want to move the image from one cell to another cell. I am following the link here. the problem is I am making the class extends activity while its extending the view and there its implementing. see below

my class

  public class GameActivity extends Activity
    implements OnTouchListener

class in example

  public class TouchExampleView extends View    

its showing error on OnDraw() method. I could understand its a defined in view class but how could I implement it.

karan421
  • 863
  • 17
  • 43

1 Answers1

0

Activity File.

windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();


tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {         

@Override
public boolean onTouch(View v, MotionEvent event) {
    layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
    switch(event.getActionMasked())
    {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            int x_cord = (int) event.getRawX();
            int y_cord = (int) event.getRawY();
            if (x_cord > windowwidth) {
                x_cord = windowwidth;
            }
            if (y_cord > windowheight) {
                y_cord = windowheight;
            }
            layoutParams1.leftMargin = x_cord - 25;
            layoutParams1.topMargin = y_cord - 75;
            tv1.setLayoutParams(layoutParams1);
            break;
        default:
            break;
    }
    return true;
}
});

tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {         

@Override
public boolean onTouch(View v, MotionEvent event) {
    layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
    switch(event.getActionMasked())
    {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            int x_cord = (int) event.getRawX();
            int y_cord = (int) event.getRawY();
            if (x_cord > windowwidth) {
                x_cord = windowwidth;
            }
            if (y_cord > windowheight) {
                y_cord = windowheight;
            }
            layoutParams2.leftMargin = x_cord - 25;
            layoutParams2.topMargin = y_cord - 75;
            tv2.setLayoutParams(layoutParams2);
            break;
        default:
            break;
     }
     return true;
  }
});

XML File:-

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 <ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp"
    android:id="@+id/imageview1" 
    android:src="@drawable/image1"  />    
 <ImageView
    android:layout_width="100sp" 
    android:layout_height="100sp" 
    android:id="@+id/imageview2"
    android:src="@drawable/image2"   />             
</RelativeLayout>

see this one

Android Drag and drop images on the Screen?

Community
  • 1
  • 1
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98