A library that scatters some ImageViews around haphazardly, or orderly
implementation 'com.simmorsal.library:scatter_image_view:1.0.0'
Your XML file should look like this:
The rootView
of your XML layout file should be a RelativeLayout
(or FrameLayout
), add anything you want to the layout, and at the bottom of the rootView
, add ScatterImageView
, and set it's width and height to match_parent
.
NOTE: if you give an elevation to any of the layouts, make sure to give a higher one to ScatterImageView
.
Aside from getting a reference to the ScatterImageView
in the XML layout, this part has two steps. One, adding images at the start of layout creation. and two, starting the animation.
-
First keep in mind that all the images you add are stored in one
list
(more on this below). For adding images use any of the following 4 methods:addImage(Context context, int imgRes, Bitmap bitmap, int size, int color, int count)
Use this method when you want to add one image. If you want to use an
imageResource
, passnull
forbitmap
, and if you want to usebitmap
, pass0
forimgRes
.addImages(Context context, int size, int color, int count, int... imgReses)
addImages(Context context, int size, int color, int count, Bitmap... bitmaps)
The two methods above are used when you have a set of
imageResources
, orbitmaps
.addImages(Context context, int size, int count, Entry... entries)
This is for when you have multiple images, and you want each to have it's own
colorFilter
. Here is an example for the last method:scatterImageView.addImages(this, 40, 20, new Entry(Color.parseColor("#FFEB3B"), R.drawable.beer), new Entry(Color.parseColor("#D81B60"), R.drawable.glass_wine));
You can pass either
imageResource
orbitmap
as second argument forEntry
.
This example creates 20 instances of images inentries
array, totaling in 40 images added to thelist
. -
To start the animation, you can first choose the scattering to be orderly (like dollar signs and hearts in the GIF above), or haphazardly (like music notes and drinks in the GIF):
scatterImageView.setHaphazardize(true);
Then call
start()
on the object:start(int posX, int posY, int duration, int travelDistance) and start(int posX, int posY, int duration, int travelDistance, int from, int to)
posX
andposY
are the position you want the animation to start.duration
is how long should it take images to movetravelDistance
from the center.As mentioned above, all the images you add are stored in one
list
. So by usingfrom
andto
in thestart()
, you can specify from which image to which one you want animated.So continuing example above:
scatterImageView.setHaphazardize(true) .start(x, y, 2000, 100, 0, 39);
So if you added 20 more images to the list and wanted to show them, pass
40
forfrom
, and59
forto
.If you want to pass the touch position to the
start()
, 2 easy ways are like this:-
One, if you have a widget the size of the screen and want the touches to be registered on that (like the GIF example above), make the widget
clickable
, and then listen for the touch on it:fullScreenWidget.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { // this if acts like a click, so if you need onClick on the widget, write your code in this IF if (motionEvent.getAction() == MotionEvent.ACTION_UP) { int posX = (int) e.getX(); int posY = (int) e.getY(); ... } return false; } });
-
Two, if you don't have a full screen widget, instead you have a widget inside a, say,
NestedScrollView
, you can write the exact same code as above for it, with the difference of getting the location of the widget on the screen, and adding values to theposX
andposY
:int[] widgetPosition = new int[2]; theWidget.getLocationOnScreen(widgetPosition); int posX = (int) e.getX() + widgetPosition[0]; int posY = (int) e.getY() + widgetPosition[1];
-
- You can add images to the list at any point of app's runtime, but as it makes the app stutter, I suggest doing it in
onCreate
. - If you dont want
colorFilter
on you images, pass0
forcolor
. - All the sizes are in
dp
.
None. have fun with the code.