点击android中的动态图像

时间:2014-01-23 10:32:59

标签: android animation imageview android-animation

我有一个图像,它正在屏幕上移动所有方向。很好。我的要求是,我想点击该图像并立即将图像的方向更改为其他。这是代码。请帮助我的朋友。

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>   

1 个答案:

答案 0 :(得分:1)

尝试此操作并修改

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;
    }
}