Android How to iterate an R.drawable object - android

I'm trying to display a frame by frame animation and want to iterate through the drawables so I don't have to type all their names in case the number of frames increases.
However I can't seem to find how to iterate through the drawables. I have looked up a fair bit of java for loop tutorials but they all just printed stuff which (as far as I'm sure) don't have to use here.
Here's the relevant code (the image's names are dude1, dude2, ...):
private void startAnimation(){
animation = new AnimationDrawable();
for (int i = 1; i < 4; i++) {
animation.addFrame(getResources().getDrawable(R.drawable.dude(i)), 100);
}
animation.setOneShot(true);
ImageView imageView = (ImageView) findViewById(R.id.img);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 90);
params.alignWithParent = true;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
imageView.setImageDrawable(animation);
imageView.post(new Starter());
}
Thx!

I think the last answer is varely true but it may be:
for (int i=0; i <10;i++){
animation.addFrame(
getResources().getDrawable(getResources().getIdentifier("dude" + i,"drawable",
getPackageName()),100);
}

Try this. getResources() needs a context.
for (int i=0;i<10;i++){
animation.addFrame(getResources().getIdentifier("dude" + i,"drawable", getPackageName()),100);
}
Here, I have assumed 10 frames (i<10).

I have used Simon's answer to iterate through my drawables that are named "c1" through "c54" and placed in an array. Here is the code I just used that works for me.
private void getDeckDrawables() {
for (int i=1; i<53; i++){
intArrDeck[i-1] = getResources().getIdentifier("c"+i,"drawable",getPackageName());
}
}
Previously, I typed them manually which took up too much room for my taste.
private void getDeckDrawables() {
intArrDeck[0]=R.drawable.c1;
intArrDeck[1]=R.drawable.c2;
intArrDeck[2]=R.drawable.c3;
}

Related

How to dynamically get R value [duplicate]

This question already has answers here:
how to access the drawable resources by name in android
(7 answers)
Closed 6 years ago.
I have to dynamically get some resources from the R file.
For example, let's say I have to dynamically generate ImageView and dynamically get a drawable to put inside it.
I don't know how many ImageView I have to generate; there may be 10, 50 or 100 so I have to do everything dynamically.
My main problem is to get dynamically the drawable from the R file.
Lets say I have this drawable:
R.drawable.img1
R.drawable.img2
R.drawable.img3
R.drawable.img4
I should do something like this:
for(int i = 0; i < 10; i++){
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.img + i);
}
How can I "build" this line of code: R.drawable.img + i
How can I reach my goal?
At first make sure your image is jpg or png
You can try with this
for(int i = 0; i < 10; i++)
{
ImageView iv = new ImageView(this);
int imageResource = context.getResources().getIdentifier("#drawable/img "+i.replace(".jpg", ""), null,context.getPackageName());
iv.setImageResource(imageResource);
}
final int []imageArray=
{R.drawable.img1,R.drawable.img2,R.drawable.img3};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
//randomimg.setImageResource(imageArray[i]);
//hEAR CREATE dynamic image view and set image resource
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 5000); //for interval...
}
};
handler.postDelayed(runnable, 5000);
put above code in your activity. and set image resource it work for me.

Regenarate location if (X,Y) already contains ImageView there

I have an ImageViewArray and I am randomizing their locations, I want to check if the current location already contains an ImageView without keeping a lot of values of x,y for every ImageView in the array.
Here is my code:
ImageView[] imageViewArray = new ImageView[40];
for (int i = 0; i < 40; i++) {
imageViewArray[i] = new ImageView(this);
imageViewArray[i].setTag(i);
imageViewArray[i].setImageResource(R.mipmap.enemy);
rlt.addView(imageViewArray[i]);
imageViewArray[i].setX(rand.nextInt(rlt.getWidth()));
imageViewArray[i].setY(rand.nextInt(rlt.getHeight()));
if(imageViewArray[i].getX()=) // here I want to check if it already contains an ImageView.
}
Possible Solution
Creating IntArray and adding X value to it and also every Y value for it, then compare between them, is it the best solution?
Problem with the solution - nothing happens, the imageview doesn't change the place and the Toast is not executed.
code:
ImageView[] imageViewArray = new ImageView[20];
ArrayList<Float> xarray = new ArrayList<>();
ArrayList<Float> yarray = new ArrayList<>();
for (int i = 0; i < 20; i++) {
imageViewArray[i] = new ImageView(this);
imageViewArray[i].setTag(i);
imageViewArray[i].setImageResource(R.mipmap.enemy);
imageViewArray[i].setX((float)rand.nextInt(1 + layoutwidth));
imageViewArray[i].setY((float)rand.nextInt(1 + layoutheight));
xarray.add(imageViewArray[i].getX());
yarray.add(imageViewArray[i].getY());
rlt.addView(imageViewArray[i]);
Toast.makeText(MainActivity.this,imageViewArray[i].getX() +"blabla",Toast.LENGTH_LONG);
}
EDIT
layoutwidth is zero :
private int layoutwidth, layoutheight, randx, randy;
private RelativeLayout rlt;
....
rlt = (RelativeLayout) findViewById(R.id.layout);
rlt.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rlt.getViewTreeObserver().removeOnGlobalLayoutListener(this);
layoutwidth = rlt.getWidth();
layoutheight = rlt.getHeight();
}
});
Yes, that is the best solution. You need to load the rectangles from somewhere. You might merge rectangles if one contains the other, but then you would over-complicate your task and in your quest of writing a more performant and a clearer code, you would end up with a slow and complicated code. Write your code with storing pairs of X, Y points where X is the let-top corner position and Y is the right-bottom corner position.
Note, that I have assumed that the pictures are not rotated. If the images might be rotated, then you need a more general solution, using the inequalities defining the rectangles to see where a point set of a rectangle intersects the point set of the other rectangle. If the intersection is empty set, then the "space is not used up".

Issues with Graph ploting in android achrtengine

I am using achartengine for graph ploting . I have issues with matching points with a straight line . Like If I have to make a line for a week and let say I don't have points for any two days (Tuesday and wed.) Now how I am gonna match point of Monday directly to Thursday and so on .
This is the code I am using for making chart .
multiRenderer.addSeriesRenderer(dataRenderer);
mChart = ChartFactory.getLineChartView(PatientHome.this,dataset, multiRenderer);
// adding the view to the linearlayout
final LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
100));
l.addView(mChart);
l.setPadding(5, 10, 0, 0);
final int p = pos;
runOnUiThread(new Runnable() {
public void run() {
chartContainer.addView(l, p);
}
});
pos++;
}
I will assume that you are using TimeSeries, for your data points.
You could use instead regular XYSeries that will have consecutive numbers, and add your own labels to X axis like with something like this:
for (int i = 0; i < yourData.size(); i++) {
multiRenderer.addXTextLabel(i, yourDataLabel);
}

Android change visibility doesn't work

Simple card game setup creating rows and columns number of cards:
TableLayout tableCards = new TableLayout(this);
RelativeLayout.LayoutParams relLayParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tableCards.setLayoutParams(relLayParams);
int i = 0;
StringBuilder sbCardsDebug = new StringBuilder('\n');
for (int r = 0; r < rows; r++) {
// create a new row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int c = 0; c < columns; c++) {
ImageView card = new ImageView(this);
card.setTag("" + i);
card.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
card.setImageDrawable(bitmapCardBackScaled);
tr.addView(card);
card.setOnClickListener(cardClickListener);
i++;
}
// add the row:
tableCards.addView(tr);
}
relativeLayoutCardsHolder.addView(tableCards);
Somewhere in the listener I have a code part like this:
imageView.setVisibility(View.INVISIBLE);
Where the imageView is a card ( I have breakpoint set up also log and it shows the correct card.
The problem: it doesn't become invisible.
I am thinking maybe I am not in a good Thread or should I need something to do it after setting the invisible?
I have tried with :
imageView.post(new Runnable() {
public void run() {
imageView.setVisibility(View.INVISIBLE);
}
});
and
TableLayout parent = (TableLayout) imageView.getParent().getParent();
parent.invalidate();
parent.requestLayout();
and no success. Pls make any suggestion, I am out of ideas, thanks.
Edit1:
As suggestion I have tried with postDelayed too:
imageView.postDelayed(new Runnable() {
public void run() {
imageView.setVisibility(View.INVISIBLE);
}
}, 600);
Listener body is from line 353 to 424 calling several classes with animations, passing references and it will reveal my dirty work, also will show the business logic too (NDA agrement), which part are you interested? - min 1000 lines are executed.
If it's a correct ImageView, its must work.
imageView.setAlpha(0);
Hope it's help.

android- Placing two programatically created imageView next to each other in a RelativeLayout

I have created two imageViews promatically as shown below:
public void createImageViews(Integer count){
ImageView[] imageViewArray = new ImageView[count];
for (int i = 0; i < count; i++ ) {
imageViewArray[i] = new ImageView(getBaseContext());
imageViewArray[i].setId(i); // unique property for every imageView
if(i==0){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageViewArray[i].setLayoutParams(params);
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i]);
Log.v("first", "first"+i);
}
else if(i < 3){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i],params);
Log.v("second", "second"+i);
}
}
I just need to place the second imageView toRightOf first imageView. Can someone help me. This is eating away a lot of my time.
try https://stackoverflow.com/a/5191159/1436931
you are using wrong index values.
at line
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
you aligning current image RIGHT of current image. :)
you need to track the index of last imageView id i.e left Image view
you need something like this
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i-1].getId());

Categories

Resources