Progressbar for two or more operations - android

I have this code in doInBackground method of an AsyncTask:
for (int i = 0; i < lenght; i++) {
// Do something
count++;
publishProgress(count * 100 / lenght);
}
and all works fine. If i add another operation, how to reflect this with the progress bar?
Now i have this code:
for (int i = 0; i < lenght1; i++) {
// Do something
count++;
publishProgress(count * 100 / lenght1);
}
for (int i = 0; i < lenght2; i++) {
// Do something
count++;
publishProgress(count * 100 / lenght2);
}
How to make the bar start from 0 when operation 1 starts and finish at 100 when operation 2 ends? I tried to change count*100 to count*50 but it seems not to be the right way...

Find an arithmetic formula involving length1 and length2, to publish the progress accordingly.
For example, if you want to give same weight to each increase of count and 100% means count == length1+length2, then you can use something like this: publishProgress(count * 100 / (length1+length2));

Related

I try to make components to be invisible when I clicked the button but it make my app stop right after I clicked the button

I try to make components to be invisible but after I clicked a button. My app stop immediately
I have an array like this
I try to make components to disappear like this
Your loop is going out of bounds because your array has 6 elements in it and you're iterating from 0 to 6 (that's seven iterations)
Replace:
for (int i = 0; i < 7; i++) {
findViewById(groupSong2[i]).setVisibility(View.INVISIBLE)
}
With this:
for (int i = 0; i < 6; i++) {
findViewById(groupSong2[i]).setVisibility(View.INVISIBLE)
}
Or even better:
for (int i = 0; i < groupSong2.length; i++) {
findViewById(groupSong2[i]).setVisibility(View.INVISIBLE)
}

Vector2 is off - libgdx/java

game.batch.begin();
for (Array obstacle_array123: obstacle_array) {
body = obstacle_array123;
for (Body bodies: body) {
if (bodies.getUserData() instanceof Array && bodies.isActive()) {
sprites_array = (Array)bodies.getUserData();
for (int fix_pos = 0; fix_pos < sprites_array.size; fix_pos++) {
sprite = sprites_array.get(fix_pos);
if (verts.size != 0) verts.removeRange(0, verts.size - 1);
f = bodies.getFixtureList().get(fix_pos);
s = (PolygonShape)f.getShape();
transform = bodies.getTransform();
for (int i = 0; i < s.getVertexCount(); i++)
{
s.getVertex(i, tmp);
transform.mul(tmp);
verts.add(new Vector2(tmp));
}
rotation_point.set((verts.get(0).x + verts.get(1).x + verts.get(2).x + verts.get(3).x) / 4, (verts.get(0).y + verts.get(1).y + verts.get(2).y + verts.get(3).y) / 4);
sprite.setPosition(rotation_point.x - sprite.getWidth() / 2, rotation_point.y - sprite.getHeight() / 2);
sprite.setRotation(bodies.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(game.batch);
}
}
}
}
game.batch.end();
I have a game where my bodies are made from multiple square fixtures, so this is the code to render each square sprite on each square fixture.
2 problems - 1.st --> it only renders the first sprite in the array
2.nd --> if you look at the following loop (SOLVED)
for (int i = 0; i < s.getVertexCount(); i++)
{
s.getVertex(i, tmp);
transform.mul(tmp);
verts.add(new Vector2(tmp));
}
well it is apperantly different compared to
for (int i = 0; i < s.getVertexCount(); i++)
{
s.getVertex(i, tmp);
transform.mul(tmp);
verts.add(tmp);
}
The spawned coordinates in 2nd example are wrong for half width and half height of the square.
When I try to get the coordinated from both examples the numbers are the same, but when setting the sprite position, 2nd example goes off.
You should probably ask both questions separately, but to answer your second question, then they ARE different.
In the first one you add a new Vector2 to verts each time through the loop. So verts will end up holding a load of different Vector2's.
In the second one you add the same Vector2 to verts over and over again, so it will just have one Vector2 with the same value over and over again (remember Java is pass by reference).
Caveat - My answer assumes that verts is some sort of standard collection or a libgdx Array.

how to get pixel color using byte array in Android

In my Android project,
Here is my code.
for (int x = 0; x < targetBitArray.length; x += weight) {
for (int y = 0; y < targetBitArray[x].length; y += weight) {
targetBitArray[x][y] = bmp.getPixel(x, y) == mSearchColor;
}
}
but this code wastes a lot of time.
So I need to find way faster than bitmap.getPixel().
I'm trying to get pixel color using byte array converted from bitmap, but I can't.
How to replace Bitmap.getPixel()?
Each Bitmap.getPixel method invocation requires a lot of resources, so you need to avoid the amount of requests in order to improve the performace of your code.
My suggestion is:
Read the image data row-by-row with Bitmap.getPixels method into a local array
Iterate along your local array
e.g.
int [] rowData= new int [bitmapWidth];
for (int row = 0; row < bitmapHeight; row ++) {
// Load row of pixels
bitmap.getPixels(rowData, 0, bitmapWidth, 0, row, bitmapWidth, 1);
for (int column = 0; column < bitmapWidth; column ++) {
targetBitArray[column][row] = rowData(column) == mSearchColor;
}
}
This will be a great improvement for the performace of your code

Trouble running my code: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

can someone help me out with this problem. this is the error message I get:
Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.mygdx.Papermadness.Papermadness.render(Papermadness.java:102)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
This is the code of my for loop through the huisArray (huis= house).
private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
spriteBatch.begin();
sprite.draw(spriteBatch);
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisBatch.draw(huisArray.get(i), huisArray.get(i).getX(), huisArray.get(i).getY());
}
spriteBatch.end();
huisBatch.end();
The idea of this code is that houses at the two edges of the screen fall continuesly.
Any help would be appreciated :)
The problem is that huisArray is empty and you are getting values from this list at line
if (huisArray.get(0).getY() < 1200) {
So first add items in huisArray before checking item in huisArray as
private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
// Add huisArray item here as huisArray.add(addspritehere);
Or change
if (huisArray.get(0).getY() < 1200) {
to
if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
Your error is likely right here:
if (huisArray.get(0).getY() < 1200) {
If your huisArray has a size of zero, then getting an object out of it will throw an exception. You should check if it has anything in it first, as follows
if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
Here:
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
You're accessing to the first element in the huisArray, but never checking if it's empty.
Add a size check before:
if (huisArray.size > 0 &&huisArray.get(0).getY() < 1200) {
addNewHuis();
}
There's also another problem in your for loops:
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
If you remove things inside the for loop just like that, you'll be ignoring some items:
Let's say you're array have 3 elements [A, B, C], and the first one have the conditions to be removed.
So in the first loop, you'll have:
i = 0
array = [A, B, C]
A is removed, and the second loop starts
i = 1
array = [B, C]
See that now you'll check for C (which is in position 1), and you'll never check B
To avoid this (using that kind of for loop) just add:
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
i--;
}
}
Also as an advice, if you know the position of the item you wan't to remove, remove it by index instead of removing by object (it's faster)
huisArray.remove(i);

counter in the loop, after finish start again

I need a counter that when it reaches the maximum value, starts to count from the beginning. I tried some if statements, but i am new in android developing and my attempts was wrong. Please help me a little. Thank you
Example:
int maxValue = 10;
for (int i = 0; i <= maxValue; i++)
{
if ( i == maxValue)
{
i = 0;
}
}

Categories

Resources