Android Bitmap Decode and Arrays - android

The following code is what I have pieced together from articles on here but im really stumped what next to try
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
for (int i = 1; i < 4; i++){
bitmapArray.add(BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + Global.svar7));
canvas.drawBitmap(bitmapArray[i], 0, 0, null);
}
Basically I am trying to draw 3 bitmaps to the canvas one below the other within a loop
This is the nearest I have got with just one error on the Canvas.drawBitmap line saying that bitmapArray[i] has the error
The type of the expression must be an array type but it resolved to ArrayList
I have search this error on here and can only find examples that include strings and setting controls
Any ideas? can you please point me in the right direction?
Your help is Greatly appreciated
Mark

To access items in an ArrayList, use ArrayList.get():
canvas.drawBitmap(bitmapArray.get(i-1), 0, 0, null);
You'll need to subtract 1 from the index because you are starting your loop from 1 not 0.

You try to use ArrayList as Array. replace bitmapArray[i] with bitmapArray.get(i).
Also you should consider that array and List indexes start from 0, not from 1, so you need also to replace for (int i = 1; i < 4; i++){ with for (int i = 0; i < 3; i++){

Related

how to create array of numbers in range 1 to 100 in view class

everyone, Please help me!
I want to show an array of numbers in a shape.
What I did
1) I draw a shape with canvas in View class.
2) I create a single random number.
3) I've searched a lot & find out that should use Drawtext to show a text or number.
4) I've read the documentation of Random, Drawtext, etc.
5) I've tried for loop but it didn't work outside of canvas class & inside, also will repeat that single number infinite times.
my Problem
I don't know how to put that number in an array & how to show array with drawtext in view class.
at the moment, I can show only one random number, but a want to show an array of random numbers.
I'm very new in Android & my English is not so good.
I'll be very grateful if anybody can Help me through this.
Thank You.
Here's the part of my code that I used for creating a single random number (this is outside of class, constructor & onDraw method) :
Random rand = new Random();
int number = rand.nextInt(100) + 1;
String mystring = String.valueOf(number);
& in onDraw method for showing this number i used below code :
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mystring,130,480,black_paintbrushstroke);
}
Please try to use ArrayList to hold your random data, like:
ArrayList<Integer> arrayList = new ArrayList<>(100);
Random r = new Random(System.currentTimeMillis());
for(int i = 0; i < 100; i++) {
arrayList.add(r.nextInt());
}
Then:
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int yOffset = 10;
int yStartPosition = 480;
for (int i = 0; i < arrayList.size(); i++) {
Integer integer = arrayList.get(i);
canvas.drawText(String.valueOf(integer), 130, yStartPosition+(i*yOffset), black_paintbrushstroke);
}
}
Looks like you are drawing all your items into the same postion (x,y), you need to add some vertical offset to draw new value bellow previous one.
If you want to draw all you numbers as comma-separted string, you need to convert your array into String using StringBuilder:
StringBuilder sb = new StringBuilder();
for(int i = 0; i < arrayList.size(); i++) {
sb.append(arrayList.get(i)).append(",");
}
String myRandomNumbersArray = sb.toString();
sb.setLength(0);

Getting numbers from 1 to 4 in a random order

I want to get 4 random value(different from each other) 1 to 4 in Android.
I wrote a code like this. But the values aren't different from each other.
Random random = new Random();
int number, idSearch[]=new int[4];
number = random.nextInt(4);
idSearch[0] = number;
for (int i = 1; i < 4; i++)
{
number = (int) random.nextInt(4);
for (int j = 0; j <= i; j++)
{
if (idSearch[j] == number) {
number = random.nextInt(4);
}
}
idSearch[i] = number;
}
Where is error? Can you help me?
I reckon the easiest way of generating a List<Integer> with values from 1 to 4 (inclusive) in a random order would be to first create a List<Integer> with the initial values and shuffle it:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Collections.shuffle(list);
System.out.println(list);
>> [4, 1, 3, 2]
Keep in mind that this list cannot be added to or removed from.
If you would like it to be mutable, simply pass it to the constructor of an ArrayList:
List<Integer> mutableList = new ArrayList<>(list);
Remember to import java.util.*
If you are using JDK1.8 then you can generate numbers from 1 to 4 in random order, you can use new Random().ints() as shown below:
int[] randoms = new Random().ints(1,5).distinct().limit(4).distinct().toArray();
If you are using JDK1.7 or earlier, you need to use Collections.shuffle() rather than you re-inventing the shuffling the logic.
You have to create new object Random and pls use second for behind fist for.

Having problems using setPixel() method

Can someone explain me the way setPixel() method works in Android? I am trying to replace some pixels on a bitmap. I extract them by using getPixel() method and their individual colors, eg.green = Color.green(a[i][j]);, but I cannot set them new values back, so as to show the processed image onscreen.
Edit: This is where some of the processing occurs. I try to algorithmically convert to grayscale
for (int i = 0; i < grayWidth; i++) {
for (int j = 0; j < grayHeight; j++) {
a[i][j] = myImage.getPixel(i, j);
red = Color.red(a[i][j]);
green = Color.green(a[i][j]);
blue = Color.blue(a[i][j]);
gray = (red + green + blue) / 3;
a[i][j] = gray;
}
}
and then replace pixels:
for (int m = 0; m < grayHeight; m++) {
for (int n = 0; n < grayWidth; n++) {
grayScale.setPixel(m, n, a[m][n]);
}
}
and finally show it on-screen
imageView.setImageBitmap(grayScale);
Sorry for not explaining it thoroughly in the first place/
Why posts questions without any code? Put yourself in our position, how can we help you, if we do not know what you are trying to do? You're only presenting your problem in English language, which does not equal to programming language in most cases. We want to see the latter, supported with english description of what you are doing and what's going on.
Based on provided information and Android Dev Page for Bitmap, I can assume that your BitMap image might not be mutable. This would throw then IllegalStateException, but without seeing your LogCat / Code, I cannot be sure, whether this is the case.
If the BitMap indeed is immutable, then you can try and look at converting immutable bitmap to mutable and try again.

Graphview not showing all x axis values

See picture below, trying to figure out why they it is skipping the '1', '3', and so forth.
Where i set the series and graph:
DataPoint[] dataPoints = new DataPoint[rankList.size()]; // declare an array of DataPoint objects with the same size as your list
for (int i = 0; i < rankList.size(); i++) {
dataPoints[i] = new DataPoint(i, Double.parseDouble(rankList.get(i))); // not sure but I think the second argument should be of type double
}
BarGraphSeries<DataPoint> series2 = new BarGraphSeries<DataPoint>(dataPoints); // This one should be obvious right? :)
series2.setAnimated(true);
series2.setTitle("Random Curve 1");
series2.setColor(Color.GREEN);
series2.setSpacing(30);
series2.setDataWidth(1);
graph2.getViewport().setMinX(-1);
graph2.getViewport().setMaxX(12);
graph2.addSeries(series2);
The right info is being plotted, but i've tried a bunch of stuff from the docs and get get it to work.
Sorry I misunderstood your question...
This will help:
yourGraph.getGridLabelRenderer().setNumHorizontalLabels(numberOfBars);
It actually shows all bars. You've set series2.setSpacing(30);.
Instead do series2.setSpacing(0);.

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

Categories

Resources