0

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>   
Sandeep R
  • 2,284
  • 3
  • 25
  • 51
kumar
  • 148
  • 6
  • 25
  • please check this out [link](http://stackoverflow.com/questions/21280363/how-to-apply-event-on-moving-imageanimating-from-top-to-bottom/21283716#21283716) – Imtiyaz Khalani Jan 23 '14 at 10:45
  • Write touch listener for this view.And check whether the touched position is the image – Asha Soman Jan 23 '14 at 10:48
  • will u please explain with example – kumar Jan 23 '14 at 10:51
  • Refer http://stackoverflow.com/questions/3476779/how-to-get-the-touch-position-in-android for getting touch event. – Asha Soman Jan 23 '14 at 10:56
  • thanku for ur response i am new for android but i have to do this task will u please once check my code and modify if ur free only otherwise i will try – kumar Jan 23 '14 at 11:07

1 Answers1

1

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;
    }
}
Asha Soman
  • 1,846
  • 1
  • 18
  • 28