77import android .graphics .Canvas ;
88import android .graphics .Rect ;
99import android .util .AttributeSet ;
10+ import android .util .TypedValue ;
1011import android .view .View ;
1112
13+ import java .util .Random ;
14+
1215import static java .lang .Math .abs ;
13- import static java .lang .Math .floor ;
1416
1517/**
1618 * Created by thijs on 08-06-15.
1719 */
1820public 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 ;
0 commit comments