1010import android .util .TypedValue ;
1111import android .view .View ;
1212
13+ import java .util .ArrayList ;
14+ import java .util .List ;
1315import java .util .Random ;
1416
1517import static java .lang .Math .abs ;
18+ import static java .util .Collections .singletonList ;
1619
1720/**
1821 * Created by thijs on 08-06-15.
1922 */
2023public 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 ) {
0 commit comments