触摸对象时如何在画布内移动对象?

时间:2014-01-05 11:06:15

标签: android

我是Android新手,我在onDraw(Canvas c)内移动对象时遇到问题。 以下是我的代码。我希望有人能告诉我如何解决它。

public class GameviewLuzon1 extends ImageView {

int y = 0;
int x = 0;

 int obj1 = R.drawable.obj_androidball;
 int obj2 = R.drawable.obj_drum;
 /****************************************/

 Drawable dr1 = getResources().getDrawable(obj1);
 Bitmap bitmap1 = ((BitmapDrawable) dr1).getBitmap();
 Drawable d1 = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap1, 20, 20, true));
 float d1_startX = (float) 0.00; 
 float d1_startY = (float) 0.00; 
 float d1_movementX = 2;
 float d1_movementY = 2;

 Drawable dr2 = getResources().getDrawable(obj2);
 Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
 Drawable d2 = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap2, 35, 35, true));
 float d2_startX = (float) 0.00; 
 float d2_startY = (float) 0.00; 
 /****************************************/

public GameviewLuzon1(Context context, AttributeSet attrs)  {
        super(context, attrs);
}
 protected void onDraw(Canvas c) {

     d1_startX = (float) (this.getWidth()/d1_movementX);
     d1_startY = (float) (this.getHeight()/d1_movementY);
     d2_startX = (float) (this.getWidth()/5.75);
     d2_startY = (float) (this.getHeight()/7.99);
     /****************************************/

     c.drawBitmap(((BitmapDrawable) d1).getBitmap(), d1_startX, d1_startY, null);
     c.drawBitmap(((BitmapDrawable) d2).getBitmap(), d2_startX, d2_startY, null);
     /****************************************/



 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {

     float touchX = event.getX();
     float touchY = event.getY();

     if( touchX >= d1_startX && touchX <= (d1_startX + d1.getIntrinsicWidth())
      && touchY >= d1_startY && touchY <= (d1_startY +d1.getIntrinsicHeight())) {
         Log.w("d1 success","you touched object");

         d1_movementX = 0;
         d1_movementY = 0;
     }
     else if( touchX >= d2_startX && touchX <= (d2_startX + d2.getIntrinsicWidth())
             && touchY >= d2_startY && touchY <= (d2_startY +d2.getIntrinsicHeight())) {
             Log.w("d2 success","you touched object");
            invalidate();
    }
     else {
         Log.w("epic","wala jud ka kaigu");
     }

     /*Commented out for debugging purpose only*/
     /*Log.w("touch",Float.toString(touchX));
     Log.w("start of Image",Float.toString(d1_startX));
     Log.w("edge of  image",Float.toString(d1_startX + d1.getIntrinsicWidth()));*/

     return true;
 }

}

1 个答案:

答案 0 :(得分:2)

public class Game1LuzonAnimation extends ImageView {

    int y = 0;
    int x = 0;
    int zoomControler = 0;

    /*****************TEST*******************/
    private Context mContext;
    private Handler h;
    private final int FRAME_RATE = 30;

    private int xVelocity = 10;
    boolean animateObj = false;
    boolean setVisibleObj = true;
    /*****************TEST*******************/
    /*Create variable obj1 and so on, so that
      * we can create a dynamic objects..,
      * if we need it to be dynamic.
      * */
     int obj1 = R.drawable.obj_androidball;
     int obj2 = R.drawable.obj_drum;
     /****************************************/

     /* Assign some obj values into their respective places and 
      * set their sizes to fit in our screen sizes
      * */
     Drawable dr1 = getResources().getDrawable(obj1);
     Bitmap bitmap1 = ((BitmapDrawable) dr1).getBitmap();
     Drawable d1 = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap1, 20, 20, true));
     float d1max_width = d1.getIntrinsicWidth() * 2;
     float d1max_height = d1.getIntrinsicHeight() * 2;
     float d1_startX = (float) 0.00; //initialize the position value of first object
     float d1_startY = (float) 0.00; //initialize the position value of first object

     /****************************************/

    public Game1LuzonAnimation(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) {

         /* put the values for position of each object from database;
          * */
         if(animateObj == false){
             d1_startX = (float) (c.getWidth()/2);
             d1_startY = (float) (c.getHeight()/2);
         }
         else {
             if (d1_startX < 0) {
                 animateObj = false;
                 setVisibleObj = false;

             } 
             else {
                 d1_startX -= xVelocity;
                 if(d1_startX > c.getWidth()){
                     xVelocity = xVelocity*-1;
                 }
             }
         }

         if(setVisibleObj == true) {
             c.drawBitmap(((BitmapDrawable) d1).getBitmap(), d1_startX, d1_startY, null);
             h.postDelayed(r, FRAME_RATE);
         }


         /*Log.w("d1max_width", Float.toString(d1max_width));
         Log.w("d1max_height", Float.toString(d1max_height));
         Log.w("d1_startX", Float.toString(d1_startX));
         Log.w("d1_startY", Float.toString(d1_startY));*/
         /****************************************/      
     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {

         float touchX = event.getX();
         float touchY = event.getY();

         if( touchX >= d1_startX && touchX <= (d1_startX + d1.getIntrinsicWidth())
          && touchY >= d1_startY && touchY <= (d1_startY +d1.getIntrinsicHeight())) {
             Log.w("d1 success","you touched object");

            if(d1.getIntrinsicWidth() < d1max_width && d1.getIntrinsicHeight() < d1max_height) {
                d1 = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap1, d1.getIntrinsicWidth()*2, d1.getIntrinsicHeight()*2, true));
                animateObj = true;
            }

         }
         else {
             Log.w("epic","wala jud ka kaigu");
         }

         return true;
     }

}