how to click on bitmapdrawable from the canvas - android

i have image(ball) this of type BitmapDrawable and it moves all directions on the screen for that i am taking ondraw(canvas) in animatedview.java its fine . my requirement is i want click on that image only but unfortunately the total screen acts as a event.so plese help me, how to click on that image only so that i am proceeding farther development.i will drop my total code here
MainActivity
public class MainActivity extends Activity {
Context context;
int count=0;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AnimatedView animatedView=(AnimatedView) findViewById(R.id.anim_view);
animatedView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText text=(EditText) findViewById(R.id.editText1);
ImageView imageView=(ImageView) findViewById(R.drawable.ball);
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation));
}
});
animatedview.java:
public class AnimatedView extends ImageView{
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
private final int FRAME_RATE = 30;
BitmapDrawable ball;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
invalidate();
}
};
#Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
x = this.getWidth()/2;
y = this.getHeight()/2;
} else {
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
h.postDelayed(r, FRAME_RATE);
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<com.example.example.AnimatedView
android:id="#+id/anim_view"
android:layout_width="fill_parent"
android:layout_height="420dp"
android:onClick="imageClicked" />
</LinearLayout>

You need to implement onTouch(MotionEvent event), Then you need to check your current touch coordinate matches with the coordinate of bitmap if yes then you need to implement what you want to do.. here is an example of how to do that..
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int touchType = event.getAction();
float x,y;
switch(touchType){
case MotionEvent.ACTION_DOWN:
//x and y give you your touch coordinates
x = event.getX();
y = event.getY();
/*you just have to chek that your touch coordinates matches
with current coordinates of bitmap*/
if(your match is true)
//do what you want to do..
break;
case MotionEvent.ACTION_UP :
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}

Related

How to pause the ball when I click pause button?

I have ball image,when i click on image, it moves all direction on the screen, my requirement is I want to pause ball whenever I click on pause button as shown below image and where should I need to apply event handling on ImageButton
Activity_main.xml gives like following image
mainactivity:
public class MainActivity extends Activity {
static TextView editText;
static int score=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(TextView) findViewById(R.id.editText1);
//editText.setText(""+score);
Button button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AnimatedView(MainActivity.this).pauseClicked();
}
});
}
public static void setCount(int count) {
//new MainActivity().onCreate(null);
score=count;
editText.setText(""+score);
}
Animatedview.java
public class AnimatedView extends ImageView{
String name;
static int count=0;
private Context mContext;
int x = 130;
int y = 450;
private float a,b;
private int xVelocity = 20;
private int yVelocity = 20;
private Handler h;
private final int FRAME_RATE = 25;
BitmapDrawable ball;
boolean touching;
boolean dm_touched = false;
float move=3;
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h;
boolean paused;
private Paint line, ball1, background;
static int click=0;
private static final int TEXT_ID = 0;
DBCeation dbCeation;
public AnimatedView(Context context) {
super(context);
}
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
//Log.e("game","run called");
if(touching = true)
invalidate();
}
};
#Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
//Log.d("s",""+xVelocity);
Log.d("s",""+yVelocity);
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||y <0) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
//Log.e("sarat",""+touching);
if(click>=2){
if(bm_h+y>630){
final EditText input=new EditText(mContext);
Toast.makeText(getContext(),"game over",Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Game Is Over");
builder.setMessage("Enter your Name");
// Use an EditText view to get user input.
input.setId(TEXT_ID);
builder.setView(input);
builder.setPositiveButton("save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
name=input.getText().toString();
Log.d("name",""+name);
int score=MainActivity.score;
dbCeation=new DBCeation(mContext);
dbCeation.insertValues(name, score);
Intent intent=new Intent(mContext,Home.class);
click=0;
mContext.startActivity(intent);
}
});
builder.setIcon(R.drawable.ic_launcher);
builder.show();
x=150;
y=200;
xVelocity=0;
yVelocity=0;
}}
if(touching){
// Log.e("game","iftouch called called");
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}
public void pauseClicked()
{
xVelocity=0;
yVelocity=0;
invalidate(xVelocity, yVelocity, 0, 0);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Log.d("game","ontouch called");
int touchType = event.getAction();
switch(touchType){
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
/* if (dm_touched) {
x = (int) a - bm_offsetx;
y = (int) b - bm_offsety;
}*/
//invalidate();
break;
case MotionEvent.ACTION_DOWN:
//x and y give you your touch coordinates
a = event.getX();
b = event.getY();
touching = true;
if(a>x+20&&a<330&&b<=y+320&&b>y){
click++;
Log.d("click",""+click);
invalidate();
touching=true;}
/* if (touching) {
// paused = true;
h.removeCallbacks(r);
touching = false;
} else {
touching = true;
}*/
//Log.d("game","action_down called");
Log.e("s",""+ a);
Log.e("s",""+ b);
Log.e("s",""+ x);
Log.e("s",""+ y);
Log.e("s",""+ bm_w);
Log.e("s",""+ bm_h);
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
count++;
MainActivity.setCount(count);
//invalidate();
Log.i("score",""+count);
}
if (dm_touched) {
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
move+=2;
//x = (int) a - bm_offsetx;
y = (int) (yVelocity*-1);
//invalidate();
}}
// dm_touched = true;
case MotionEvent.ACTION_UP:
a = event.getX();
b = event.getY();
/* if(a>x+20&&a<330&&b<=y+320&&b>y){
click++;
Log.d("click",""+click);
invalidate();
touching=true;}*/
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
Log.e("game","clicked");
}
default:
dm_touched = true;
}
return true;
}
#Override
public void destroyDrawingCache() {
count=0;
click=0;
super.destroyDrawingCache();
}

Click event on moving a image

I have an image(ball) its a BitmapDrawable and it moves in all directions on the screen for that i am taking onDraw(canvas) in AnimatedView.java its working fine.
My Requirement:
I want to click on the image, but unfortunately the whole screen takes the event.
Please help me.
How to get click event only on the image?
So that i can proceed with further development.
Here is the code:
MainActivity
public class MainActivity extends Activity {
Context context;
int count=0;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AnimatedView animatedView=(AnimatedView) findViewById(R.id.anim_view);
animatedView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText text=(EditText) findViewById(R.id.editText1);
ImageView imageView=(ImageView) findViewById(R.drawable.ball);
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation));
}
});
animatedview.java:
public class AnimatedView extends ImageView{
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
private final int FRAME_RATE = 30;
BitmapDrawable ball;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
invalidate();
}
};
#Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
x = this.getWidth()/2;
y = this.getHeight()/2;
} else {
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
h.postDelayed(r, FRAME_RATE);
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<com.example.example.AnimatedView
android:id="#+id/anim_view"
android:layout_width="fill_parent"
android:layout_height="420dp"
android:onClick="imageClicked" />
</LinearLayout>
**I hope This codes Works for you, what you are looking for.....**
This is the complete code representing both ball movement and touch on ball.
public class MainActivity extends Activity implements OnTouchListener
{
BubbleGraphic bubbleGraphic;
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety, bm_w, bm_h, subBM_w, subBM_h;
int f = 0;
private float x, y;
Canvas canvas;
boolean dm_touched = false;
static int random_x;
static int random_y;
boolean touching;
// These variable for position in x_axis direction
int w1;
// These variable for position in y_axis direction
int h1;
// These variable for velocity in x_axis direction
private int w1_V;
// These variable for velocity in y_axis direction
private int h1_V;
Bitmap mainBG;
Bitmap bmpBall;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bubbleGraphic = new BubbleGraphic(this);
bubbleGraphic.setOnTouchListener(this);
// Your Background Image of Canvas
mainBG = BitmapFactory.decodeResource(getResources(), R.drawable.bg1);
w1 = -1;
h1 = -1;
w1_V = 1;
h1_V = 1;
// This is used to create bitmap object and decode the input stream into bitmap
bmpBall = BitmapFactory.decodeResource(getResources(),
R.drawable.myBall);
bm_w = bmpBall.getWidth();
bm_h = bmpBall.getHeight();
setContentView(bubbleGraphic);
}
protected void onPause() {
super.onPause();
bubbleGraphic.pause();
}
protected void onResume() {
super.onResume();
bubbleGraphic.resume();
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
touching = true;
if (dm_touched) {
w1 = (int) x - bm_offsetx;
h1 = (int) y - bm_offsety;
}
break;
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
touching = true;
// checking if Ball is touched
if ((x > w1) && (x < bm_w + w1) && (y > h1) && (y < bm_h + h1)) {
bm_offsetx = (int) x - w1;
bm_offsety = (int) y - h1;
}
dm_touched = true;
break;
case MotionEvent.ACTION_UP:
default:
dm_touched = false;
touching = false;
}
return true;
}
// **************************************************************************************//
// Creating Canvas graphic Class and running thread //
// *************************************************************************************//
class BubbleGraphic extends SurfaceView implements Runnable {
SurfaceHolder holder;
Thread thread = null;
boolean isRunning = false;
boolean holdingBubble = true;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public BubbleGraphic(Context context) {
super(context);
holder = getHolder();
}
public void pause() {
isRunning = false;
while (true) {
try {
thread.join();
} catch (Exception e) {
}
break;
}
thread = null;
}
public void resume() {
isRunning = true;
thread = new Thread(this);
thread.start();
}
public void run() {
while (isRunning) {
if (!holder.getSurface().isValid())
continue;
canvas = holder.lockCanvas();`enter code here`
canvas.drawBitmap(mainBG, 0, 0, null);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.TRANSPARENT);
if (touching) {
canvas.drawRect(x, y, x + bm_w, y + bm_h, paint);
}
canvas.drawBitmap(bmpBall, w1, h1, null);
// Method Responsible for ball movement and its position
checkBallMovementAndPosition();
holder.unlockCanvasAndPost(canvas);
}
}
private void checkBallMovementAndPosition() {
if (w1 < 0 && h1 < 0) {
w1 = this.getWidth() / 2;
h1 = this.getHeight() / 2;
} else {
w1 += w1_V;
h1 += h1_V;
if ((w1 > this.getWidth() - bm_w) || (w1 < 0)) {
w1_V = w1_V * -1;
w1 += w1_V;
}
if ((h1 > this.getHeight() - bm_h) || (h1 < 0)) {
h1_V = h1_V * -1;
h1 += h1_V;
}
}
}
}
}
Note: Replace the images with Your images.

click on moving image in the android

i have an image and it is moving all directions on the screen.Its fine.My requirement is, i want to click on that image and immediately change the direction of the image to other. Here is the code .Please help me friends.
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public class AnimatedView extends ImageView{
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
private final int FRAME_RATE = 30;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
invalidate();
}
};
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
x = this.getWidth()/2;
y = this.getHeight()/2;
} else {
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
h.postDelayed(r, FRAME_RATE);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000000">
<com.example.example.AnimatedView
android:id="#+id/anim_view"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</RelativeLayout>
Try this and modify
public class AnimatedView extends ImageView {
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
private final int FRAME_RATE = 1000;
BitmapDrawable ball;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
invalidate();
}
};
protected void onDraw(Canvas c) {
ball = (BitmapDrawable) mContext.getResources()
.getDrawable(R.drawable.ic_launcher);
if (x < 0 && y < 0) {
x = this.getWidth() / 2;
y = this.getHeight() / 2;
} else {
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity * -1;
}
if ((y > this.getHeight() - ball.getBitmap().getHeight())
|| (y < 0)) {
yVelocity = yVelocity * -1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
h.postDelayed(r, FRAME_RATE);
}
int xStart, yStart, xEnd, yEnd;
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
xStart = (int) event.getX();
yStart = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
xEnd = (int) event.getX();
yEnd = (int) event.getY();
break;
}
if(xStart >= x && yStart >= y && xEnd <= (x+ball.getBitmap().getWidth()) && yEnd <= (y+ball.getBitmap().getHeight())){
Log.i("MainActivity","AnimatedView Clicked................");
}
return false;
}
}

start image animation by clicking on same image

i have image animation(moves bottom to top and top to bottom),and it is loading automatically when ever i am install into device but i won't that,my requirement is i want click on that image then only animation has to start,
please help me
ondraw method is:
protected void onDraw(Canvas c) {
int z= c.getHeight()/2;
Log.e("bharat","z is"+z);
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
//x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if (((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0))) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(touching){
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.example.AnimatedView
android:id="#+id/anim_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="imageClicked" />
</RelativeLayout>
**animatedview.java**
//this is for image moving from bottom to top
public class AnimatedView extends ImageView{
static int count=0;
private Context mContext;
int x = 150;
int y = 450;
private float a,b;
private int xVelocity = 10;
private int yVelocity = 15;
private Handler h;
private final int FRAME_RATE = 25;
BitmapDrawable ball;
boolean touching;
boolean dm_touched = false;
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
invalidate();
}
};
#Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
//x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("bharat","ontouch called");
int touchType = event.getAction();
switch(touchType){
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
if (dm_touched) {
x = (int) a - bm_offsetx;
y = (int) b - bm_offsety;
}
break;
case MotionEvent.ACTION_DOWN:
//x and y give you your touch coordinates
a = event.getX();
b = event.getY();
touching = true;
Log.d("bharat","action_down called");
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
count++;
Log.i("bharat",""+count);
Intent intent1 = new Intent();
intent1.putExtra("score", count);
}
dm_touched = true;
case MotionEvent.ACTION_UP:
default:
dm_touched = false;
touching = false;
}
return true;
}
}
**MainActivity.java**
//here i am want to get the image the view and by clicking on that image only animation should loaded
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View animatedView = findViewById(R.id.anim_view);
ImageView imageView=(ImageView) findViewById(R.id.imageView1);
}
You are not attaching any click listners in the AnimatedView. Modify your constructor as
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
setOnTouchListener(this)
}
Also call invalidate() in the onTouchEvent()
public boolean onTouchEvent(MotionEvent event) {
int touchType = event.getAction();
switch(touchType){
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
invalidate();
break;
}
}

how to move image to mid point of bottom and center of the screen

i have class i.e., animatedview.java. this class gives an animation like, image moving from bottom to top and top to bottom.my requirement is i want to move the image from bottom to midpoint of center and bottom like the following screen!
i want to reach this position later i will get down continuously
mycode is:
public class AnimatedView extends ImageView{
static int count=0;
private Context mContext;
int x = 150;
int y = 450;
private float a,b;
private int yVelocity = 20;
private Handler h;
private final int FRAME_RATE = 25;
BitmapDrawable ball;
boolean touching;
boolean dm_touched = false;
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
if(touching = true)
invalidate();
}
};
#Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
y += yVelocity;
if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||(y <0)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(touching){
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int touchType = event.getAction();
switch(touchType){
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
break;
case MotionEvent.ACTION_DOWN:
//x and y give you your touch coordinates
a = event.getX();
b = event.getY();
touching = true;
Log.d("bharat","action_down called");
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
count++;
Log.i("bharat",""+count);
}
dm_touched = true;
case MotionEvent.ACTION_UP:
a = event.getX();
b = event.getY();
if(a>x+20&&a<330&&b<=y+320&&b>y)
invalidate();
default:
dm_touched = true;
touching = true;
}
return true;
}
}
please help me
if I understand you correctly this should fix it:
instead of y< 0, you should put y < c.getHeight()/2
#Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
y += yVelocity;
if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||(y <c.getHeight()/2)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(touching){
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}

Categories

Resources