如何将AdMob集成到班级中?

时间:2018-12-23 01:24:59

标签: java android admob

我正在制作一个简单的游戏,就像《飞扬的小鸟》,我想将AdMob集成到我用来绘制位图的类中。我设法将AdMob集成到其他活动中,但是相同的方法在这里行不通。我找不到其他资源可以帮助我。一些帮助,将不胜感激。谢谢。

public class Fish extends View{
    private SharedPreferences sharedPreferences;

    Dialog myDialog;
    private AdView mAdView;
    private Bitmap fish[] = new Bitmap[2];

    private int fishX = 10;
    private int fishY;
    private int fishSpeed;

    private int canvasWidth, canvasHeight;
    private int yellowX, yellowY, yellowSpeed = 16;

    private Paint yellowPaint = new Paint();
    private int greenX, greenY, greenSpeed = 16;
    private Paint greenPaint = new Paint();

    //was 25
    private int redX, redY, redSpeed = 25;
    private Paint redPaint = new Paint();


    private int score, lifeCountOfLife;

    private boolean touch = false;

    private Bitmap backgroundImage;
    private Paint scorePaint = new Paint();

    private Bitmap life[] = new Bitmap[2];

    private SoundPlayer sound;


    Rect backgroundRect = new Rect(0, 0, canvasWidth, canvasHeight);


    //updates the  bg


    public Fish(Context context) {
        super(context);








        fish[0] = BitmapFactory.decodeResource(getResources(), R.drawable.fishyvector);
        fish[1] = BitmapFactory.decodeResource(getResources(), R.drawable.fishyvector);
        myDialog = new Dialog(getContext());


        backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.mn
        );
        sound = new SoundPlayer(getContext());


        //creates the ball/food color
        yellowPaint.setColor(Color.YELLOW);
        yellowPaint.setAntiAlias(false);

        greenPaint.setColor(Color.BLUE);
        greenPaint.setAntiAlias(false);

        redPaint.setColor(Color.RED);
        redPaint.setAntiAlias(false);

        // updates score
        scorePaint.setColor(Color.WHITE);
        scorePaint.setTextSize(70);
        scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
        scorePaint.setAntiAlias(true);

        //displays the heart and life
        life[0] = BitmapFactory.decodeResource(getResources(), R.drawable.hearts);
        life[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heart_grey);

        fishY = 550;
        score = 0;
        lifeCountOfLife = 3;


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //getWidth/getHeight canvas.
        canvasWidth = getWidth();
        canvasHeight = getHeight();

        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        int w = metrics.widthPixels;
        int h = metrics.heightPixels;

        //displays it to the main activity
        canvas.drawBitmap(backgroundImage, null, new RectF(0, 0, canvasWidth, canvasHeight), null);


        int minFish = fish[0].getHeight();
        int maxFishY = canvasHeight - fish[0].getHeight() * 3;
        fishY = fishY + fishSpeed;

        if (fishY < minFish) {
            fishY = minFish;
        }

        if (fishY > maxFishY) {
            fishY = maxFishY;
        }

        fishSpeed = fishSpeed + 2;
        if (touch) {
            canvas.drawBitmap(fish[1], fishX, fishY, null);
            touch = false;

        } else {
            canvas.drawBitmap(fish[0], fishX, fishY, null);
        }

        yellowX = yellowX - yellowSpeed;

        //updates the score if the ball is collected
        if (hitBallChecker(yellowX, yellowY)) {


            score = score + 1;
            yellowX = -100;
        }

        if (yellowX < 0) {
            yellowX = canvasWidth + 21;
            yellowY = (int) Math.floor(Math.random() * (maxFishY - minFish)) + minFish;

        }
        //increases size of ball
        canvas.drawCircle(yellowX, yellowY, 25, yellowPaint);


        //Green ball

        greenX = greenX - greenSpeed;
        if (hitBallChecker(greenX, greenY)) {
            score = score + 1;
            greenX = -100;

        }

        if (greenX < 0) {
            greenX = canvasWidth + 21;
            greenY = (int) Math.floor(Math.random() * (maxFishY - minFish)) + minFish;

        }
        updateDifficulty();


        //increases size of ball
        canvas.drawCircle(greenX, greenY, 25, greenPaint);


        //red ball

        //if the ball gets hit
        redX = redX - redSpeed;
        if (hitBallChecker(redX, redY)) {
            vibratePhone();


            redX = -100;
            lifeCountOfLife--;
            if (lifeCountOfLife == 0) {
                SharedPreferences preferences = getContext().getSharedPreferences("PREFS", 0);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("lastScore", score);
                editor.apply();
                Intent gameOverIntent = new Intent(getContext(), GameOverActivity.class);
                gameOverIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                gameOverIntent.putExtra("score", score);
                getContext().startActivity(gameOverIntent);

            }
        }

        //increases size of ball

        if (redX < 0) {
            redX = canvasWidth + 21;
            redY = (int) Math.floor(Math.random() * (maxFishY - minFish)) + minFish;


        }
        canvas.drawCircle(redX, redY, 30, redPaint);
        canvas.drawText("Score: " + score, 20, 60, scorePaint);

        for (int i = 0; i < 3; i++) {
            int x = (int) (580 + life[0].getWidth() * 1.5 * i);
            int y = 30;
            if (i < lifeCountOfLife) {
                canvas.drawBitmap(life[0], x, y, null);


            } else {
                canvas.drawBitmap(life[1], x, y, scorePaint);

            }
        }


    }

    public boolean hitBallChecker(int x, int y) {
        if (fishX < x && x < (fishX + fish[0].getWidth()) && fishY < y && y < (fishY + fish[0].getHeight())) {
            return true;
        }
        return false;

    }

    //updates fish speed
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            touch = true;
            fishSpeed = -24;
            sound.playFlapping();

        }
        return true;
    }

    public void updateDifficulty() {


        if (score >= 100 && score < 200) {
            redSpeed = 30;
            greenSpeed = 20;
            yellowSpeed = 15;


        }
        if (score >= 200 && score < 300) {
            redSpeed = 40;
            greenSpeed = 30;
            yellowSpeed = 25;
        }
        if (score >= 300 && score < 400) {
            redSpeed = 50;
            greenSpeed = 40;
            yellowSpeed = 35;
        }
        if (score >= 400 && score < 500) {
            redSpeed = 65;
            greenSpeed = 45;
            yellowSpeed = 40;
        }
        if (score >= 500 && score < 600) {
            redSpeed = 80;
            greenSpeed = 55;
            yellowSpeed = 50;

        }
        if (score >= 700) {
            redSpeed = 90;
            greenSpeed = 60;
            yellowSpeed = 55;
        }
    }

    public void ShowPopup() {
        TextView txtclose;
        Button btnFollow;
        myDialog.setContentView(R.layout.custompopup);
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.show();
    }

    public void vibratePhone() {
        Vibrator v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
        // Vibrate for 1 seconds
        v.vibrate(500);
    }


}

0 个答案:

没有答案