Displaying random images - android

im novice to android.For displaying random images i used arraylist.
ArrayList<Integer> List = new ArrayList<Integer>();
for(int i=0;i<10;i++)
{
List.add(i);
System.out.println("Random Images"+List.add(i));
}
Collections.shuffle(List);
for(int i=0;i<10;i++){
imageArr[i] = (ImageView)findViewById(id[List.get(i)]);
System.out.println("Display Images"+List.get(i));
}
}
it is running correctly in logcat but what should do to display images on emulator screen. Pls Suggest

You will need an ImageView to display Images on the Screen.
You can display drawable, bitmaps, ...
The easiest way to do this create your main.xml like this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/myImageView" />
Then call in your code (maybe in the onCreate):
ImageView imgView = (ImageView) findViewById(R.id.myImageView);
Then make a Drawable Array or an ArryList with bitmaps, whatever.
Get a Random value (like Math.random()) and fetch a random image from array or arraylist like
Drawable drawable = drawableArray[YOURRANDOMNUMBER];
and set the Drawable to the imageview.
imgView .setImageDrawable(drawable);
Hope this helps :)

Give this a read and see if it helps you: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/

Related

Set ImageView Drawable programmatically

I am trying to set the image of a ImageView programmatically from a array of drawables but keep getting a NUllPointerException....does this method look correct for setting Image resource..
//get a random drawable
int[] imageSelection= {R.drawable.buddycheck, R.drawable.logdive3, R.drawable.sea, R.drawable.weather, R.drawable.logo1};
Random whichImage = new Random();
int theImage = whichImage.nextInt(imageSelection.length);
displayImage.setBackgroundResource(theImage);
You are setting the image resource to a random number.
You need to do it like this:
int theImage = imageSelection[whichImage.nextInt(imageSelection.length)];
displayImage.setBackgroundResource(theImage);
Try this : imageView.setImageResource(R.drawable.image);

OutOfMemoryError when doing setImageResource()

ImageView.setImageResource(int id); is showing OutOfMemoryError.
i have a TableLayout in that i am adding TableRow pragmatically, in each row i'm setting a image, that is already in my projects resource folder, i am referencing those image withe the resource_id.
private void addItems() {
table.removeAllViews();
Random rand = new Random();
for (int i = 0; i < 20; i++) {
TableRow row = new TableRow(getActivity());
table.addView(row);
RelativeLayout profile = (RelativeLayout) getLayoutInflater(null).inflate(R.layout.buddyname, null);
TextView name = (TextView) profile.findViewById(R.id.name);
name.setText(randomNames[i]);
ImageView photo = (ImageView) profile.findViewById(R.id.photo);
photo.setImageResource(randomPhoto[new Random().nextInt(randomPhoto.length - 1)]);
row.addView(profile);
}
}
each time i am doing table.removeAllViews(); even then its not working, can i guess is it because the image resource is loading again and again on heap and not clearing the heap, can we reference the previously loaded image, is there any solution.
Buddyname? It seems like it is a contact list. If it is, use the ListView instead. It dynamically loads elements which are visible. 20 Table rows are indeed much.

Image overlaps with other images when scaled

I am programatically adding ImageView elements into a horizontal Linear Layout. Then I set the scaleX and scaleY properties to "2" on one of the ImageView resources. The image gets scaled properly, but it doesn't move the other ImageView elements, instead of that it overlaps them. I don't like the image to overlap with other images. How can I fix that? Here's my code:
int resources[] = {R.drawable.desert, R.drawable.koala, R.drawable.jellyfish,
R.drawable.lighthouse, R.drawable.desert};
for(int i=0; i<5; i++) {
ImageView logo = new ImageView(this);
logo.setLayoutParams(new LinearLayout.LayoutParams(100, 75));
logo.setImageResource(resources[i]);
logosContainer.addView(logo);
}
ImageView middleImage = (ImageView) logosContainer.getChildAt(2);
middleImage.setScaleX(middleImage.getScaleX() * 2);
middleImage.setScaleY(middleImage.getScaleY() * 2);
The result from the code looks like this:
http://imageshack.us/a/img15/2811/scaleal.jpg
You can clearly see that the scaled image overlaps with the other images.
Your ImageViews are already measured and placed in the container when you leave the loop. You would have to refresh your layout to make this work, which is tricky and often leads into "removing all views and adding them again". So why don't you just do the "scaling" in the loop?
for(int i=0; i<5; i++) {
ImageView logo = new ImageView(this);
if(i==middleImageIndex){
logo.setLayoutParams(new LinearLayout.LayoutParams(100*2, 75*2,1));
} else {
logo.setLayoutParams(new LinearLayout.LayoutParams(100, 75,1));
}
logo.setImageResource(resources[i]);
logosContainer.addView(logo);
}
The last parameter (100,75, 1) is the weight of the view, which makes sure that each ImageView is equally important and doesn't get overlapped.
Another note: setScale requires API level 11 or higher, which could cut a lot of users out there

Convert Bitmap to drawable and store it in array

i am using image slider in my layout, for this i used
private Integer[] mImageIds = { R.drawable.j, R.drawable.f,R.drawable.i, R.drawable.g, R.drawable.k };
to take the images.But now i want to take the images from sdcard and store these images into that array.How can i do this?
below is my code. i stored thumb in database.I converted bitmap to drawable and then how to store this into Array mImages?
List<String> thumb = new ArrayList<String>();
public Integer[] mImageIds;
Bitmap bitmap;
thumb = db.getRecomdThumb();//get image address from db
for(int i = 0; i < thumb.size();i++){
bitmap = BitmapFactory.decodeFile(thumb.get(i));
Drawable d = new BitmapDrawable(getResources(),bitmap);
}
Only the images you store inside the "res" folder can be added to a Integer array. You can create a int res from dynamically created Bitmap. You can only convert it to Drawable as you have mentioned in your code.
If you ask me, will it be possible to get Int representation if I add the dynamically created drawable to res folder, it is not possible.
"Res" folder can't be modified. It is read only.
So what you have to do is, just create a Drawable array instead of Integer array and do your operation.
Something like this would work,
List<String> thumb = new ArrayList<String>();
public Integer[] mImageIds;
Bitmap bitmap;
thumb = db.getRecomdThumb();//get image address from db
private Drawable[] drawables = new Drawable[thumb.size()];
for(int i = 0; i < thumb.size();i++){
bitmap = BitmapFactory.decodeFile(thumb.get(i));
drawables[i] = new BitmapDrawable(getResources(),bitmap);
}
You could just create a
private Drawable[] drawables = new Drawable[mImageIds.length];
and copy the images at the respective size.
Having said that: drawables may be large and storing them (or the bitmaps) in memory may make you quickly run into out-of-memory situations as the handsets usually have small heap sizes.

How to can i control on the Bitmap size that will be draw on ImageButton?

On my code i need to create some table that each column contain Bitmap and text - and i create this table dynamically.
So i doing this by using this code:
for (int i = 0; i < collection.size(); i++)
{
ObjectITem item = collection.get(i);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView textView = new TextView(this);
textView.setText(item.getText());
linearLayout.addView(textView);
linearLayout.addView( (new ImageButton(this)).setImageBitmap(item.getBitmap());
layout.addView(linearLayout, LayoutParams.WRAP_CONTENT);
}
this code is working good - but because the originaly images are not in same size - i see that the images that appear on the ImageButton are look not good.
How can i make all the images on the ImageButton to look the same ?
Is there some better way to make the layout that i did here ?
1) You can use Bitmap.createScaledBitmap() to scale all the images to a desired size. Like this...
Bitmap buttonBmp = Bitmap.createScaledBitmap(item.getBitmap(), buttonW, buttonH, true);
linearLayout.addView((new ImageButton(this)).setImageBitmap(buttonBmp);
2) You might want to consider using a ListActivity and a ListView. This is more complicated, but may be more efficient if your collection is large and needs to scroll over many pages.

Categories

Resources