Android Open Source - study-android-fallballgame Holder Call Back






From Project

Back to project page study-android-fallballgame.

License

The source code is released under:

Apache License

If you think the Android project study-android-fallballgame listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jp.hym.falingballgamge;
/*w  w  w .ja va2 s .  c o  m*/
import java.util.ArrayList;
import java.util.List;

import javax.crypto.spec.OAEPParameterSpec;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;

import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.SurfaceHolder;

public class HolderCallBack implements SurfaceHolder.Callback, Runnable{

  private SurfaceHolder holder = null;
  private Thread thread = null;
  private boolean isAttached = true;

    //resources
    private Bitmap image_hall;
    private Bitmap image_ball;
    private Bitmap image_goal;
    private Bitmap image_floor;

  private float width, height;  //???????
  private float dx=0, dy=0;  //?????
  
  //??????  
  private float coordAcceleration = 0.01f;  //?????
  private float coordMaxSpeed = 20.0f;  //???????????????
  private float coordResist = 0.1f;  //????
  private float coordRebound = 0.8f;  //??????????????????
  private float coordCd = 0.5f;  //???????
  
  //??????????????
  private int ballSize = 50;  //??????
  private int ball_x, ball_y;  //?????
  private int start_x = ballSize, start_y = ballSize;  //?????
  private float acceleration = 1.0f;  //???
  private float maxSpeed = 1.0f;  //?????????????
  private List<PointF> holls = new ArrayList<PointF>();//??????
  private int hollsSize = 100;  //???????????
  private int goal_x = 500, goal_y = 500;  //?????
  private int goalSize = 100;  //?????????????
  private float resist = 1.0f; //??
  private float rebound = 1.0f;  //????????????????
  
  private FragmentActivity mActivity;
//??????
  Context context;

  InclinationSensor is;

  public HolderCallBack(FragmentActivity activity) {
    mActivity = activity;
    is = new InclinationSensor(activity, activity.getWindowManager().getDefaultDisplay().getOrientation());

    Resources res = activity.getResources();
        image_ball = ((BitmapDrawable) res.getDrawable(R.drawable.ic_ball_02)).getBitmap();
        image_hall = ((BitmapDrawable) res.getDrawable(R.drawable.ic_hall_02)).getBitmap();
        image_goal = ((BitmapDrawable) res.getDrawable(R.drawable.ic_goal_02)).getBitmap();
        image_floor = ((BitmapDrawable) res.getDrawable(R.drawable.bg_floor_05_tile_parts)).getBitmap();
        
  }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      //Log.i("System.out" , "HoldCallBack???surfaceChanged");
      
      this.width = width;
      this.height = height;
      
      Log.i("System.out", "???????(?:" + this.height + "?:" + this.width + ")");
      
      //?????????????
      for(int i=0;i<5;i++){
        //holls.add(new PointF((float)Math.random()*width, (float)Math.random()*height));
      }

//??????????

      start_x = ballSize;  //????x??
      start_y = ballSize;  //????y??
      ballSize = 50;  //??????
      acceleration = 1.0f;  //???
      maxSpeed = 1.0f;  //???????????????????

      goal_x = 500;  //???x??
      goal_y = 500;  //???y??
      goalSize = 100;  //?????????????

      holls.add(new PointF(500, 200));  //holls.add(new PointF(?x??,?y??))  ?????OK
      hollsSize = 100;  //???????????

      resist = 1.0f;  //??
      rebound = 1.0f;  //????????????????
      
//??????????

      //????????????
      maxSpeed = Math.abs(maxSpeed);  //??
      
      //???????
      start_x = Inside(start_x, ballSize, (int)this.width);  //????x??
      start_y = Inside(start_y, ballSize, (int)this.height);  //????y??
      ball_x = start_x;  //??????????????????
      ball_y = start_y;  //??????????????????

      goal_x = Inside(goal_x, goalSize, (int)this.width);  //???x??
      goal_y = Inside(goal_y, goalSize, (int)this.height);  //???y??

      for(int i=0;i<holls.size();i++){
        holls.get(i).x = Inside((int)holls.get(i).x, hollsSize, (int)this.width);  //?x??
        holls.get(i).y = Inside((int)holls.get(i).y, hollsSize, (int)this.height);  //?y??
      }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
      //Log.i("System.out" , "HoldCallBack???surfaceCreated");
        this.holder = holder;
        thread = new Thread(this);
        thread.start(); //???????
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
      //Log.i("System.out" , "HoldCallBack???surfaceDestroyed");
           isAttached = false;
           thread = null; //???????
    }

    @Override
    public void run() {

      while( isAttached ){

        is.getInclination();  //????????????
        //String strlog = "roll = " + is.GetRoll() + "\tpicth = " + is.GetPicth();
          //Log.i("System.out", strlog);
          
          dx += is.GetRoll() * coordAcceleration * acceleration;
          dy += -is.GetPicth() *coordAcceleration * acceleration;
          
          //????????
          if(dx > coordMaxSpeed * maxSpeed){dx = coordMaxSpeed * maxSpeed;}
          if(dx < -(coordMaxSpeed * maxSpeed)){dx = -(coordMaxSpeed * maxSpeed);}
          if(dy > coordMaxSpeed * maxSpeed){dy = coordMaxSpeed * maxSpeed;}
          if(dy < -(coordMaxSpeed * maxSpeed)){dy = -(coordMaxSpeed * maxSpeed);}

          //??
          if(dx > coordResist * resist){
            dx -= coordResist * resist;
          }else
          if(dx < -(coordResist * resist)){
            dx += coordResist * resist;
          }else{
            dx = 0;
          }
          if(dy > coordResist * resist){
            dy -= coordResist * resist;
          }else
          if(dy < -(coordResist * resist)){
            dy += coordResist * resist;
          }else{
            dy = 0;
          }
          
        //????
        if(ball_x < ballSize || this.width - ballSize < ball_x){
          dx = (int) - dx * coordRebound * rebound;
        }
        if(ball_y < ballSize || this.height - ballSize < ball_y){
          dy = (int) - dy * coordRebound * rebound;
        }

        //????????????????????????????
        if(ball_x < ballSize){ ball_x = ballSize; }
        if(this.width - ballSize < ball_x){ ball_x = (int)this.width - ballSize; }
        if(ball_y < ballSize){ ball_y = ballSize; }
        if(this.height - ballSize < ball_y){ ball_y = (int)this.height - ballSize; }

        //Log.i("System.out", "??(" +ball_x + "," + ball_y + ")");
        
        ball_x = ball_x + (int)dx;
        ball_y = ball_y + (int)dy;

        draw();  //????
        
        //?????
        if(Math.pow((goalSize - (ballSize * coordCd)), 2) >= Math.pow((goal_x - ball_x), 2) + Math.pow((goal_y - ball_y), 2)){
        
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
          @Override
          public void run() {
            Intent intent = new Intent(mActivity, GoalActivity.class);
            mActivity.startActivity(intent);

                        mActivity.finish();
          }
        }, 100);
        //???????
        isAttached = false;
        
        //??????????
        is.releaseSensor();
      }
      
        //???????
      for(int i=0;i<holls.size();i++)
        if(Math.pow((hollsSize - (ballSize * coordCd)), 2) >= Math.pow((holls.get(i).x - ball_x), 2) + Math.pow((holls.get(i).y - ball_y), 2)){
          ball_x = start_x;
          ball_y = start_y;
          dx = 0;
          dy = 0;
        }
      }
    }

    //????
    private void draw(){

      //???????????
    Canvas canvas = holder.lockCanvas();
        if(canvas == null) return ;
    //canvas.drawColor(255, PorterDuff.Mode.CLEAR);
    //canvas.drawColor(Color.WHITE);
    Paint paint = new Paint();
        drawBackgroundRepeat(canvas, image_floor, paint);
        //drawBackground(canvas, image_floor, paint);

      //?????
      for(int i = 0; i < holls.size(); i++){
      paint.setColor(Color.BLACK);  //?
      //canvas.drawCircle(holls.get(i).x, holls.get(i).y, hollsSize, paint);
            drawBall(canvas, image_hall, paint, holls.get(i).x, holls.get(i).y, hollsSize);
      }

      //???????
    paint.setColor(Color.RED);  //?
        drawBall(canvas, image_goal, paint, goal_x, goal_y, goalSize);
    //canvas.drawCircle(goal_x, goal_y, goalSize, paint);

      //???????
    //paint.setColor(Color.BLUE);  //?
    //canvas.drawCircle(ball_x, ball_y, ballSize, paint);
    
    paint.setColor(Color.BLUE);  //?
        drawBall(canvas, image_ball, paint, ball_x, ball_y, ballSize);
    //canvas.drawCircle(ball_x, ball_y, ballSize, paint);

    holder.unlockCanvasAndPost(canvas);  //???????????
    }

    private void drawBall(Canvas cv, Bitmap bmp, Paint p, float cx, float cy, int ball_size){
        float scale = ball_size / ((float)bmp.getWidth()/2);

        Rect s = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
        Rect d = new Rect(
                (int)(cx-bmp.getWidth()*scale/2), (int)(cy-bmp.getHeight()*scale/2),
                (int)(cx+bmp.getWidth()*scale/2), (int)(cy+bmp.getHeight()*scale/2)
        );

        cv.drawBitmap(bmp, s, d, p);
    }

    private void drawBackgroundRepeat(Canvas cv, Bitmap bmp, Paint p){

        for(int y=0; y<height; y+=bmp.getHeight()) {
            for (int x = 0; x < width; x += bmp.getWidth()) {
                cv.drawBitmap(bmp, x, y, p);
            }
        }

    }
    private void drawBackground(Canvas cv, Bitmap bmp, Paint p){

        Rect s = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
        Rect d = new Rect(
                0,0,
                (int)width, (int)height
        );

        cv.drawBitmap(bmp, s, d, p);
    }



    //?????????
    private int Inside(int point, int size, int max){

      if(point < size){return size;}
      if(max - size < point){return max - size;}
      return point;
    }
}




Java Source Code List

jp.hym.falingballgamge.GameSurface.java
jp.hym.falingballgamge.GoalActivity.java
jp.hym.falingballgamge.HolderCallBack.java
jp.hym.falingballgamge.InclinationSensor.java
jp.hym.falingballgamge.MainActivity.java
jp.hym.falingballgamge.SensorValue.java