-1

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>
kumar
  • 148
  • 6
  • 25

1 Answers1

0

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;
}
Shaji Thorn Blue
  • 589
  • 2
  • 9
  • 19
  • thanks for ur response shaji, actually i am new for android,if possible will u please modify my code as per my requirement,i know this is bad request other wise i will try. – kumar Jan 27 '14 at 05:32
  • I dont have so much time.. still you can send me the code.. i will try to modify if i get time.. – Shaji Thorn Blue Jan 27 '14 at 05:35
  • thanku friend,already i am pasted total code above except animation.xml – kumar Jan 27 '14 at 05:41
  • animation.xml code is event code,i have to apply on when image is clicked and later i have to do some some modifications – kumar Jan 27 '14 at 05:46
  • @user3202568 i dont have time for next 10 hours...until then better you will try to implement by yourself make mistake, post your query here.. learn..:) – Shaji Thorn Blue Jan 27 '14 at 05:56
  • ok friend i will try,once after u r work finish,please look at my code because i am struggle from on week.once again thanku – kumar Jan 27 '14 at 06:01
  • i am try to use ur code in my code if(x==ball.getBitmap().getWidth() || y==ball.getBitmap().getHeight()) Log.d("bharat","image clicked"); i am using this codition in my ontouchevent method,but it is not called ,please tell me reasion with respect to my code – kumar Jan 27 '14 at 07:09