I am trying to drag View on a SurfaceView
This is the Code I am Using
What I am trying to do is Design a Puzzle game In which I should be able
to drag the Views to their correct positions like a Jigsaw Puzzle I have
created a View Called PuzzleView Overrided the onDraw() method to draw the
Image on its canvas. I have also implemented the OnTouchListener . I think
there is a problem in the onTouch() method . I am creating a SurfaceView
and I am adding the View to the Surfaceview by using addContentView()
method in onSurfaceCreate() method
public class TouchBall extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int w=getWindowManager().getDefaultDisplay().getWidth()-25;
int h=getWindowManager().getDefaultDisplay().getHeight()-25;
BallView ballView=new BallView(this,w,h);
setContentView(ballView);
}
class BallView extends SurfaceView implements SurfaceHolder.Callback {
private Bitmap bitmap ;
private Bitmap bitmap2 ;
private int Surfacewidth,Surfaceheight;
public BallView(Context context,int w,int h) {
super(context);
this.Surfacewidth=w;
this.Surfaceheight=h;
getHolder().addCallback(this);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int
height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
bitmap =BitmapFactory.decodeResource(getResources(),
R.drawable.puzzle1easy1);
bitmap2 =BitmapFactory.decodeResource(getResources(),
R.drawable.puzzle1easy2);
PuzzlePiece p1=new PuzzlePiece(TouchBall.this,bitmap,10,10,1);
PuzzlePiece p2=new PuzzlePiece(TouchBall.this,bitmap2,40,40,2);
addContentView(p1,new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
addContentView(p2,new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Canvas c = holder.lockCanvas(null);
onDraw(c);
holder.unlockCanvasAndPost(c);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
class PuzzlePiece extends View implements OnTouchListener
{
Bitmap bmp;
int id;
int Xpos;
int Ypos;
int width;
int height;
Canvas c;
int winPositionx;
int winPositiony;
public PuzzlePiece(Context context,Bitmap bmp,int Xpos,int
Ypos,int id) {
super(context);
this.bmp=bmp;
this.id=id;
this.Xpos=Xpos;
this.Ypos=Ypos;
width=bmp.getWidth();
height=bmp.getHeight();
setOnTouchListener(this);
// TODO Auto-generated constructor stub
}
public Bitmap getBitmap()
{
return bmp;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
c=canvas;
canvas.drawBitmap(bmp,Xpos,Ypos,null);
}
public boolean onTouch(View v, MotionEvent event) {
int _xDelta = 0;
int _yDelta = 0;
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams lParams =
(FrameLayout.LayoutParams) v.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
v.setLayoutParams(layoutParams);
break;
}
return true;
}
}
}
}
The Problem is when I am touching a View that displays the Picture It is
moving down by some distance I am in a deep trouble of meeting the
deadline plz help me Thanks in advance
No comments:
Post a Comment