Get the drawable's name after setting it on ImageView - android

I'am Actually setting an image to the ImageView Dynamically from the Drawable folder. I got the working code to set the image dynamically but I can't get the name of the image/drawable which gets set on the image view at the moment.
Either I should get the drawable name or any code to get the instance of each drawable with a name which I can set on the ImageView.
Any help is Much Appreciated!!
I searched and tried all answers on StackOverflow.
Set the image dynamically to the imageview and also get that image's name.

As a good practice, you can store the drawable in a variable and update the imageview.
#DrawableRes int myDrawable;
iv.setImageDrawable(getResources().getDrawable(myDrawable));
Setting tag and getting from tag is no ideal imho because it's more maintainable and debuggable.

Ok. after 2 days of research and trials, I finally managed to do what I planned to do. Here's my code, if anybody gets help from it.
ArrayList<Integer> array_fruits = new ArrayList<Integer>();
int fruit_apricot = R.drawable.fruits_apricot;
int fruit_apple = R.drawable.fruits_apple;
int fruit_banana = R.drawable.fruits_banana;
.... and more
array_fruits.add(fruit_apple);
array_fruits.add(fruit_apricot);
array_fruits.add(fruit_banana);
... and more
//WORKING CODE TO GENERATE NUMBER BETWEEN 30 AND 1 (0 to 29 in arraylist)
Random rand = new Random();
int num = rand.nextInt(30) + 1;
array_fruits.add(num);
itemImg.setImageDrawable(getResources().getDrawable(array_fruits.get(num)));
Log.e("num: ", num+"");
Toast.makeText(getApplicationContext(), num+"", Toast.LENGTH_LONG).show();
I had tried almost all solutions given on stackoverflow and other sites.

Related

How to Get the CORRECT Pixel Colour

I would also like to read out the colour of the pixels from an 'Image'. I've read a lot of topics here about this but the success is still missing.
So after a lot of unwanted values, I tried to simplify my code and the Image as well. I tried with the following:
//..
Android.Graphics.BitmapFactory.Options op = new BitmapFactory.Options();
op.InPreferredConfig = Bitmap.Config.Argb8888;
Bitmap b = Android.Graphics.BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.Image, op);
//imageView.SetImageBitmap(b);
int pixel1 = b.GetPixel(3, 4);
int pixel2 = b.GetPixel(12, 11);
int pixel3 = b.GetPixel(19, 20);
int pixel4 = b.GetPixel(27, 28);
int pixel5 = b.GetPixel(27, 19);
int pixel6 = b.GetPixel(20, 11);
int redV1 = Android.Graphics.Color.GetRedComponent(pixel1);
int greenV1 = Android.Graphics.Color.GetGreenComponent(pixel1);
int blueV1 = Android.Graphics.Color.GetBlueComponent(pixel1);
int redV2 = Android.Graphics.Color.GetRedComponent(pixel2);
int greenV2 = Android.Graphics.Color.GetGreenComponent(pixel2);
int blueV2 = Android.Graphics.Color.GetBlueComponent(pixel2);
//..
Surprisingly I got the results as follows:
pixel1 color is OK.
pixel2 color is NOT OK its pixel1 color.
pixel3 color is NOT OK its pixel2 color.
pixel4 color is NOT OK its pixel2 color.
pixel5 color is NOT OK its pixel2 color.
pixel6 color is NOT OK its existing color in my Image but on different coordinates. (it was not mentioned in my code)
Presentation of the .png Image in an imageView seems to be smooth.
Could anyone help with my fault?
THX!
pixel1..6 values seems to be correct (different on different colours and equal on the same) and they would be enough for me but all the values became -1 after I start to work with my real .png file (1500x1500 image as a resource). Is that too large to work with? What is the maximum in this case? Thx!
... Ok, perhaps I got the fault. To be precise, not the fault but a usable solution! Now I'm trying with a smaller .png Image sized 500x500. It is small enough to work with.
BUT: It seems the BitmapFactory creates a larger image (1000 x 1000). So I have to divide each coordinate with 2 if I want to check the correct colour on my original image. AND there is another problem: Some colours may be different read by the Getpixel(). It can be small differences in R,G,B values. In example: RGB(148,0,198) instead of (153,0,204). It means a 'bit' extra work for me to identify all the new 'Android colours' on my bitmap :( I hope, I could help for the others with this topic. It wasn't easy to find one bug/problem after another.

Android: Stopping random image being used more than once at the same time

Hi im currently setting button images to have random backgrounds. I have 9 buttons and 9 images each button needs one of the images assigned to it. Currently im using the same random int so all the buttons look the same. How can I make it so images don't repeat so if image one is already on a button none of the other buttons can have it. The random feature is important as I don't want to have the images fixed to just the one button.
He's my random setup:
private void nextBUTImage() {
Random rand = new Random();
int rndInt = rand.nextInt(9)+ 1 ;
String imgName = "b" + rndInt;
int ids = getResources().getIdentifier(imgName, "drawable", getPackageName());
but1.setBackgroundResource(ids);
but9.setBackgroundResource(ids);
but4.setBackgroundResource(ids);
but3.setBackgroundResource(ids);
but5.setBackgroundResource(ids);
but6.setBackgroundResource(ids);
but2.setBackgroundResource(ids);
but8.setBackgroundResource(ids);
but7.setBackgroundResource(ids);
}
What process can I use I did try an if statement but wasn't feasible I have been wondering about using states but not sure how to set that up. Help with this would be great.
You could create an array of the image identifiers.
Then randomize that array.
Then assign them out in order (but1 will be assigned array[0], but2 is array[1]..).

Sometimes, I cannot fetch string from resource in Android?

I found this problem on ASUS fonepad(android version 4.1.2) not on zendfone 3.
(Two devices I only have)
It function normally when I just open my app.
Once I let it idle too long, some of the text would be blank.
--
e.q.:
1.
http://ppt.cc/jcsi
This page should full of text in each block.
2.http://ppt.cc/PY67
There should be some text shown in the blank area.
--
I saved all of the text in the xml file.
Therefore, I think this should be the problem of resource?
Does someone know how to solve this problem?
----code----
/* I use this function for creating textview */
protected TextView customizeTextView(String text,float textSize, int bgc, int gravity,int textcolor)
{
TextView tv= new TextView(this);
tv.setText(text);
tv.setTextSize(textSize);
tv.setBackgroundColor(bgc);
tv.setGravity(gravity);
tv.setTextColor(textcolor);
return tv;
}
/*The parameter of string should be getString(R.string.stopDectecting); or logObject.recordId+"" */
You should replace tv.setText(text); with tv.setText(R.string.MY_STRING);
I know the answer finally.
At first, my app would calculate the ratio of current screen verse 800*480.
But with low possibility, it would get the 0 value.
This is only happened one ASUS fonepad.
Therefore, I change the time of calculation the ratio and it solved.

How to add text to image and save as new image

I'm trying to create an Android app that adds a random quote to images.
The general process is this:
Start from a custom given image that shows when starting the app.
From this image all the user can do is tap on it and generate a new random "quote" that get overlaid on the image.
The user can save the newly created image with the quote he chose and set it as wallpaper.
I have got to the point where I can display the image in an ImageView.
My list of quotes is stored in my strings.xml file.
I do something like this in an app. Use Canvas.
I edited down a piece of my code, which actually adds a couple of other images on the background and stuff too.
Meat of code:
private static Bitmap getPoster(...) {
Bitmap background = BitmapFactory.decodeResource(res, background_id)
.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(background);
Typeface font = Typeface.createFromAsset(res.getAssets(), FONT_PATH);
font = Typeface.create(font, Typeface.BOLD);
Paint paint = new Paint();
paint.setTypeface(font);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
float fontSize = getFontSize(background.getWidth(), THE_QUOTE, paint); //You'll have to define a way to find a size that fits, or just use a constant size.
paint.setTextSize(fontSize);
canvas.drawText(THE_QUOTE, (background.getWidth() - paint.measureText(THE_QUOTE)) / 2,
background.getHeight() - FILLER_HEIGHT, paint); //You might want to do something different. In my case every image has a filler in the bottom which is 50px.
return background;
}
Put your own version of that in a class and feed it the image id and anything else. It returns a bitmap for you to do whatever you want with (display it in an imageview, let the user save it and set as wallpape).
I know i did this for the PC with imagemagick a few years ago(save image with text on)
Seems like imagemagick have been ported to android, so I would start digging into thier documentation.
https://github.com/lilac/Android-ImageMagick
Ok! Francesco my friend, I've an idea although not a working code ('cuz I'm not really good at it). So, here it is:
Implement an onClickListener() on your ImageView like below:
ImageView iv = (ImageView)findViewById(R.id.imageview1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
/** When I say do your stuff here, I mean read the user input and set your wallpaper here. I'm sorry that I don't really know how to save/set the wallpaper */
}
});
When it comes to reading user input/generating random quotes, you can do this:
You said you already have the quotes saved in the strings.xml file. Using the ids of those strings, I think you can implement a switch case scenario where it uses java imports - java.util.Scanner and java.util.Random. Ultimately, using these in your ImageView onClickListener could/should result in the desired output.
I know my answer is too vague, but I've a faint hope that it has given you a minute lead as to what you can implement. I seriously hope there are better answers than this. If not, then I hope this helps you some, and I also hope that I'm not leading you in the wrong direction since this is just a mere speculation. Sorry, but this is all I've got.

Android: Canvas made out of multiple images won't save

Right my title isn't the best in the world. I've got a big code that's supposed to make on big bitmap out of multiple bitmaps. I've isolated the problem to this part of the code
bity = Bitmap.createBitmap(specialWidth,specialHeight,Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(bity);
float left=0.0f;
for (int i = 0; i < imagesArrayz.length; i++){
float top=0.0f;
canvas.drawBitmap(imagesArrayz[i], left, top, null);
left+=imagesArrayz[i].getWidth();
}
To explain: "bity" is a globally defined Bitmap object and it's unassigned untill this point; imagesArrayz is an array of 5 Bitmaps that has already ben assigned and has ben assigned correctly (i tested it to see if each image is in the array)
After this i just have a function that saves the global variable bity to a file. THE PROBLEM is that instaid of saving my nicely drawn canvas it saves an empty jpg file of 0kb. Please help!
I answered my own question... Replace ALPHA_8 with ARGB_8888 and it all magically works.
Someone shoot me please...

Categories

Resources