Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit df3e3d2

Browse files
committed
Added weighting to random scene generator
1 parent 15e2aaa commit df3e3d2

46 files changed

Lines changed: 79 additions & 11 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

library/src/main/java/com/q42/android/scrollingimageview/ScrollingImageView.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010
import android.util.TypedValue;
1111
import android.view.View;
1212

13+
import java.util.ArrayList;
14+
import java.util.List;
1315
import java.util.Random;
1416

1517
import static java.lang.Math.abs;
18+
import static java.util.Collections.singletonList;
1619

1720
/**
1821
* Created by thijs on 08-06-15.
1922
*/
2023
public class ScrollingImageView extends View {
21-
private Bitmap[] bitmaps;
24+
private List<Bitmap> bitmaps;
2225
private float speed;
2326
private int[] scene;
2427
private int arrayIndex = 0;
@@ -35,29 +38,50 @@ public ScrollingImageView(Context context, AttributeSet attrs) {
3538
try {
3639
speed = ta.getDimension(R.styleable.ParallaxView_speed, 10);
3740
int sceneLength = ta.getInt(R.styleable.ParallaxView_sceneLength, 1000);
41+
final int randomnessResourceId = ta.getResourceId(R.styleable.ParallaxView_randomness, 0);
42+
int[] randomness = new int[0];
43+
if (randomnessResourceId > 0) {
44+
randomness = getResources().getIntArray(randomnessResourceId);
45+
}
46+
3847
int type = ta.peekValue(R.styleable.ParallaxView_src).type;
3948
if (type == TypedValue.TYPE_REFERENCE) {
4049
int resourceId = ta.getResourceId(R.styleable.ParallaxView_src, 0);
4150
TypedArray typedArray = getResources().obtainTypedArray(resourceId);
4251
try {
43-
bitmaps = new Bitmap[typedArray.length()];
52+
int bitmapsSize = 0;
53+
for (int r : randomness) {
54+
bitmapsSize += r;
55+
}
56+
57+
bitmaps = new ArrayList<>(Math.max(typedArray.length(), bitmapsSize));
58+
4459
for (int i = 0; i < typedArray.length(); i++) {
45-
bitmaps[i] = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(i, 0));
46-
maxBitmapHeight = Math.max(bitmaps[i].getHeight(), maxBitmapHeight);
60+
int multiplier = 1;
61+
if (randomness.length > 0 && i < randomness.length) {
62+
multiplier = Math.max(1, randomness[i]);
63+
}
64+
65+
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(i, 0));
66+
for (int m = 0; m < multiplier; m++) {
67+
bitmaps.add(bitmap);
68+
}
69+
70+
maxBitmapHeight = Math.max(bitmap.getHeight(), maxBitmapHeight);
4771
}
4872

4973
Random random = new Random();
5074
this.scene = new int[sceneLength];
5175
for (int i = 0; i < this.scene.length; i++) {
52-
this.scene[i] = random.nextInt(bitmaps.length);
76+
this.scene[i] = random.nextInt(bitmaps.size());
5377
}
5478
} finally {
5579
typedArray.recycle();
5680
}
5781
} else if (type == TypedValue.TYPE_STRING) {
58-
bitmaps = new Bitmap[]{BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0))};
82+
bitmaps = singletonList(BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0)));
5983
scene = new int[]{0};
60-
maxBitmapHeight = bitmaps[0].getHeight();
84+
maxBitmapHeight = bitmaps.get(0).getHeight();
6185
}
6286
} finally {
6387
ta.recycle();
@@ -100,7 +124,7 @@ public void onDraw(Canvas canvas) {
100124
}
101125

102126
private Bitmap getBitmap(int sceneIndex) {
103-
return bitmaps[scene[sceneIndex]];
127+
return bitmaps.get(scene[sceneIndex]);
104128
}
105129

106130
private float getBitmapLeft(float layerWidth, float left) {

library/src/main/res/values/attr.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<attr name="speed" format="dimension" />
55
<attr name="src" format="reference" />
66
<attr name="sceneLength" format="integer" />
7+
<attr name="randomness" format="reference" />
78
</declare-styleable>
89
</resources>
1.58 KB
220 Bytes
471 Bytes
1.32 KB
1.3 KB
909 Bytes
252 Bytes
281 Bytes

0 commit comments

Comments
 (0)