How to set ImageView Looping while i only have limited images ? (Android) - android

I have array drawable, the array drawable only have 4 images, and i have could be more than 4 ImageView, How to set image on the ImageView by looping, and when the looping reach more than 4, i want it's return to the first image, so it will be like this:
ImageView 1 with first drawable
ImageView 2 with second drawable
ImageView 3 with third drawable
ImageView 4 with fourth drawable
ImageView 5 with first drawable
ImageView 6 with second drawable
and so on..
How to do that ?
Any help will be appreciate, thank you!

u can use % operator. It returns remainder of a division.
For example, if you have 6 views and 4 images, you can do this way:
ImageView[] viewsArray = new ImageView[6];
//init your array with findViewById()
for(int i = 0; i < viewsArray.length; ++i) {
viewsArray[i].setImageDrawable(imagesArray[i % 4]); // if your index is 5 % operator will return 1.
}

Related

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

Change Color of cell of grid

I want to change the color of the grid cell using the number of grid position . e.g. I have 5X6 grid means 30 gridItems so i want to change the color of 21st position. Please tell me how can i do this Without clicking on the Grid View.
You will need to define a custom adapter for this.
In the getView() method of adapter you'll have to check the position parameter if is equal with 21. If it's equal with 21, then change the background for currently cell.
If you did not had the experience to define a custom adapter yet, then it will make more sense to pass through an example first.
Here's an example of a GridView that uses a custom adapter to display images.
In order to set color in grid cell while inflating grid cell's layout, in your baseadapter class create a cell's array then set the color as you wish.
Like
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grd = li.inflate(R.layout.grid_item, null);
FrameLayout dgcl = (FrameLayout) grd.findViewById(R.id.grditm);
parent_l[position] = dgcl;
then
parent_l[21].setBackgroundColor(Color.RED);
here griditm is the id of the layout grid_item
First you must decide the order of the grid, where are columns, and where are lines. For example:
1 2 3 4 5
6 7 8 9 10
etc..
then just do a multiplication
i = Y*numberOfColums + X;
grid[i].myColor = Color(R,G,B);
I'm assuming 0 based index, that simply means:
if there are 6 columns:
0 <= X <= 5
if there are 5 rows
0 <= Y <=4
0 based index allows you to iterate the whole grid in a very simple manner
for(int x = 0; x < numberOfColumns; x++)
{
for(int y = 0; y < numberOfRows; y++)
{
i = Y*numberOfColums + X;
}
}

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.

Displaying random images

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/

Categories

Resources