-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackGround.java
More file actions
72 lines (54 loc) · 1.9 KB
/
BackGround.java
File metadata and controls
72 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.example.android.nextgame;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
public class Background {
Bitmap bitmap;
Bitmap bitmapReversed;
int width;
int height;
boolean reversedFirst;
float speed;
int xClip;
int startY;
int endY;
Background(Context context, int screenWidth, int screenHeight, String bitmapName, int sY, int eY, float s){
int resID = context.getResources().getIdentifier(bitmapName,
"drawable", context.getPackageName());
// Load the bitmap using the id
bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
// Which version of background (reversed or regular)
// is currently drawn first (on left)
reversedFirst = false;
//Initialise animation variables.
// Where to clip the bitmaps
// Starting at the first pixel
xClip = 0;
//Position the background vertically
startY = sY * (screenHeight / 100);
endY = eY * (screenHeight / 100);
speed = s;
// Create the bitmap
bitmap = Bitmap.createScaledBitmap(bitmap, screenWidth,
(endY - startY)
, true);
// Save the width and height for later use
width = bitmap.getWidth();
height = bitmap.getHeight();
//Create a mirror image of the background (horizontal flip)
Matrix matrix = new Matrix();
matrix.setScale(-1, 1);
bitmapReversed = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
public void update(long fps){
xClip -= speed/fps;
if (xClip >= width) {
xClip = 0;
reversedFirst = !reversedFirst;
} else if (xClip <= 0) {
xClip = width;
reversedFirst = !reversedFirst;
}
}
}