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

Commit 15e2aaa

Browse files
committed
Added random scene generator
1 parent 2171dd9 commit 15e2aaa

5 files changed

Lines changed: 57 additions & 13 deletions

File tree

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
import android.graphics.Canvas;
88
import android.graphics.Rect;
99
import android.util.AttributeSet;
10+
import android.util.TypedValue;
1011
import android.view.View;
1112

13+
import java.util.Random;
14+
1215
import static java.lang.Math.abs;
13-
import static java.lang.Math.floor;
1416

1517
/**
1618
* Created by thijs on 08-06-15.
1719
*/
1820
public class ScrollingImageView extends View {
19-
private final int speed;
20-
private final Bitmap bitmap;
21+
private Bitmap[] bitmaps;
22+
private float speed;
23+
private int[] scene;
24+
private int arrayIndex = 0;
25+
private int maxBitmapHeight = 0;
2126

2227
private Rect clipBounds = new Rect();
2328
private float offset = 0;
@@ -28,8 +33,32 @@ public ScrollingImageView(Context context, AttributeSet attrs) {
2833
super(context, attrs);
2934
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 0, 0);
3035
try {
31-
speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);
32-
bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));
36+
speed = ta.getDimension(R.styleable.ParallaxView_speed, 10);
37+
int sceneLength = ta.getInt(R.styleable.ParallaxView_sceneLength, 1000);
38+
int type = ta.peekValue(R.styleable.ParallaxView_src).type;
39+
if (type == TypedValue.TYPE_REFERENCE) {
40+
int resourceId = ta.getResourceId(R.styleable.ParallaxView_src, 0);
41+
TypedArray typedArray = getResources().obtainTypedArray(resourceId);
42+
try {
43+
bitmaps = new Bitmap[typedArray.length()];
44+
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);
47+
}
48+
49+
Random random = new Random();
50+
this.scene = new int[sceneLength];
51+
for (int i = 0; i < this.scene.length; i++) {
52+
this.scene[i] = random.nextInt(bitmaps.length);
53+
}
54+
} finally {
55+
typedArray.recycle();
56+
}
57+
} else if (type == TypedValue.TYPE_STRING) {
58+
bitmaps = new Bitmap[]{BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0))};
59+
scene = new int[]{0};
60+
maxBitmapHeight = bitmaps[0].getHeight();
61+
}
3362
} finally {
3463
ta.recycle();
3564
}
@@ -39,7 +68,7 @@ public ScrollingImageView(Context context, AttributeSet attrs) {
3968
@Override
4069
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
4170
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
42-
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bitmap.getHeight());
71+
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), maxBitmapHeight);
4372
}
4473

4574
@Override
@@ -51,15 +80,17 @@ public void onDraw(Canvas canvas) {
5180

5281
canvas.getClipBounds(clipBounds);
5382

54-
float layerWidth = bitmap.getWidth();
55-
if (offset < -layerWidth) {
56-
offset += (floor(abs(offset) / layerWidth) * layerWidth);
83+
while (offset <= -getBitmap(arrayIndex).getWidth()) {
84+
offset += getBitmap(arrayIndex).getWidth();
85+
arrayIndex = (arrayIndex + 1) % scene.length;
5786
}
5887

5988
float left = offset;
60-
while (left < clipBounds.width()) {
61-
canvas.drawBitmap(bitmap, getBitmapLeft(layerWidth, left), 0, null);
62-
left += layerWidth;
89+
for (int i = 0; left < clipBounds.width(); i++) {
90+
Bitmap bitmap = getBitmap((arrayIndex + i) % scene.length);
91+
int width = bitmap.getWidth();
92+
canvas.drawBitmap(bitmap, getBitmapLeft(width, left), 0, null);
93+
left += width;
6394
}
6495

6596
if (isStarted) {
@@ -68,6 +99,10 @@ public void onDraw(Canvas canvas) {
6899
}
69100
}
70101

102+
private Bitmap getBitmap(int sceneIndex) {
103+
return bitmaps[scene[sceneIndex]];
104+
}
105+
71106
private float getBitmapLeft(float layerWidth, float left) {
72107
if (speed < 0) {
73108
return clipBounds.width() - layerWidth - left;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<declare-styleable name="ParallaxView">
44
<attr name="speed" format="dimension" />
55
<attr name="src" format="reference" />
6+
<attr name="sceneLength" format="integer" />
67
</declare-styleable>
78
</resources>

sampleapp/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
android:theme="@style/AppTheme" >
1010
<activity
1111
android:name=".MainActivity"
12-
android:label="@string/app_name" >
12+
android:label="@string/app_name">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515

sampleapp/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
android:layout_marginTop="10dp">
2727

2828
<com.q42.android.scrollingimageview.ScrollingImageView
29+
android:id="@+id/thijs"
2930
android:layout_width="match_parent"
3031
android:layout_height="wrap_content"
3132
android:layout_gravity="bottom"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<array name="random_imgs">
4+
<item>@drawable/van</item>
5+
<item>@drawable/layer_7</item>
6+
</array>
7+
</resources>

0 commit comments

Comments
 (0)