Skip to content

Commit

Permalink
Added weighting to random scene generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuijten committed Oct 5, 2015
1 parent 15e2aaa commit df3e3d2
Show file tree
Hide file tree
Showing 46 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static java.lang.Math.abs;
import static java.util.Collections.singletonList;

/**
* Created by thijs on 08-06-15.
*/
public class ScrollingImageView extends View {
private Bitmap[] bitmaps;
private List<Bitmap> bitmaps;
private float speed;
private int[] scene;
private int arrayIndex = 0;
Expand All @@ -35,29 +38,50 @@ public ScrollingImageView(Context context, AttributeSet attrs) {
try {
speed = ta.getDimension(R.styleable.ParallaxView_speed, 10);
int sceneLength = ta.getInt(R.styleable.ParallaxView_sceneLength, 1000);
final int randomnessResourceId = ta.getResourceId(R.styleable.ParallaxView_randomness, 0);
int[] randomness = new int[0];
if (randomnessResourceId > 0) {
randomness = getResources().getIntArray(randomnessResourceId);
}

int type = ta.peekValue(R.styleable.ParallaxView_src).type;
if (type == TypedValue.TYPE_REFERENCE) {
int resourceId = ta.getResourceId(R.styleable.ParallaxView_src, 0);
TypedArray typedArray = getResources().obtainTypedArray(resourceId);
try {
bitmaps = new Bitmap[typedArray.length()];
int bitmapsSize = 0;
for (int r : randomness) {
bitmapsSize += r;
}

bitmaps = new ArrayList<>(Math.max(typedArray.length(), bitmapsSize));

for (int i = 0; i < typedArray.length(); i++) {
bitmaps[i] = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(i, 0));
maxBitmapHeight = Math.max(bitmaps[i].getHeight(), maxBitmapHeight);
int multiplier = 1;
if (randomness.length > 0 && i < randomness.length) {
multiplier = Math.max(1, randomness[i]);
}

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(i, 0));
for (int m = 0; m < multiplier; m++) {
bitmaps.add(bitmap);
}

maxBitmapHeight = Math.max(bitmap.getHeight(), maxBitmapHeight);
}

Random random = new Random();
this.scene = new int[sceneLength];
for (int i = 0; i < this.scene.length; i++) {
this.scene[i] = random.nextInt(bitmaps.length);
this.scene[i] = random.nextInt(bitmaps.size());
}
} finally {
typedArray.recycle();
}
} else if (type == TypedValue.TYPE_STRING) {
bitmaps = new Bitmap[]{BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0))};
bitmaps = singletonList(BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0)));
scene = new int[]{0};
maxBitmapHeight = bitmaps[0].getHeight();
maxBitmapHeight = bitmaps.get(0).getHeight();
}
} finally {
ta.recycle();
Expand Down Expand Up @@ -100,7 +124,7 @@ public void onDraw(Canvas canvas) {
}

private Bitmap getBitmap(int sceneIndex) {
return bitmaps[scene[sceneIndex]];
return bitmaps.get(scene[sceneIndex]);
}

private float getBitmapLeft(float layerWidth, float left) {
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<attr name="speed" format="dimension" />
<attr name="src" format="reference" />
<attr name="sceneLength" format="integer" />
<attr name="randomness" format="reference" />
</declare-styleable>
</resources>
Binary file added sampleapp/src/main/res/drawable-hdpi/fg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-hdpi/fg9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-mdpi/fg9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg8.png
Binary file added sampleapp/src/main/res/drawable-xhdpi/fg9.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg1.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg2.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg3.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg4.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg5.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg6.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg7.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg8.png
Binary file added sampleapp/src/main/res/drawable-xxhdpi/fg9.png
Empty file modified sampleapp/src/main/res/drawable-xxhdpi/layer_1.png
100755 → 100644
Empty file modified sampleapp/src/main/res/drawable-xxhdpi/layer_2.png
100755 → 100644
Empty file modified sampleapp/src/main/res/drawable-xxhdpi/layer_3.png
100755 → 100644
Empty file modified sampleapp/src/main/res/drawable-xxhdpi/layer_4.png
100755 → 100644
Empty file modified sampleapp/src/main/res/drawable-xxhdpi/layer_5.png
100755 → 100644
Empty file modified sampleapp/src/main/res/drawable-xxhdpi/layer_7.png
100755 → 100644
27 changes: 26 additions & 1 deletion sampleapp/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
style="@style/Base.TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Random scene generator" />

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">

<com.q42.android.scrollingimageview.ScrollingImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
scrolling_image_view:speed="3dp"
scrolling_image_view:src="@array/random_imgs"
scrolling_image_view:randomness="@array/randomness" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:src="@drawable/van" />
</FrameLayout>

<TextView
style="@style/Base.TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
Expand All @@ -26,7 +52,6 @@
android:layout_marginTop="10dp">

<com.q42.android.scrollingimageview.ScrollingImageView
android:id="@+id/thijs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
Expand Down
22 changes: 20 additions & 2 deletions sampleapp/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="random_imgs">
<item>@drawable/van</item>
<item>@drawable/layer_7</item>
<item>@drawable/fg1</item>
<item>@drawable/fg2</item>
<item>@drawable/fg3</item>
<item>@drawable/fg4</item>
<item>@drawable/fg5</item>
<item>@drawable/fg6</item>
<item>@drawable/fg7</item>
<item>@drawable/fg8</item>
<item>@drawable/fg9</item>
</array>
<integer-array name="randomness">
<item>1</item>
<item>5</item>
<item>1</item>
<item>1</item>
<item>1</item>
<item>1</item>
<item>1</item>
<item>1</item>
<item>3</item>
</integer-array>
</resources>

0 comments on commit df3e3d2

Please sign in to comment.