6

I want to create images which will go down from the upper part of the screen.

For up to today I have this:

ImageView mario = (ImageView) findViewById(R.id.mario);
TranslateAnimation anim = new TranslateAnimation(0f, 0f, 0, 400);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(800);
mario.startAnimation(anim);

The problems are that I have to set the imageview on the xml file on the layout and this code creates just 1 picture only.

I want to program the app to create a couple of images (for example in a loop) in the upper part of the screen and have them drop down the screen. (here I use TranslateAnimation here). I found something like this:

ImageView mario = (ImageView) findViewById(R.drawable.mario);

But I don't know how to set the position of the ImageView which isn't in the xml file (is it possible?). I though about creating LinearLayout and add it to the ImageView. But how to add the linearlayout to the existing layout?

Thanks in advance :)

2 Answers 2

10

You can create a layout with something like

View view = (View) findViewById(R.layout.current_layout); //the layout you set in `setContentView()`
LinearLayout picLL = new LinearLayout(CurrentActivity.this);
picLL.layout(0, 0, 100, 0);
picLL.setLayoutParams(new LayoutParams(1000, 60));
picLL.setOrientation(LinearLayout.HORIZONTAL);
((ViewGroup) view).addView(picLL);

What parameters you pass in layout() are obviously going to depend on what you want. Then you can create separate Views to add to the Layout you just created. But I highly advise reading through the docs to understand what all can be done here.

ViewGroup

View

Edit

ImageView myImage = new ImageView(this);
picLL.addView(myImage);
//set attributes for myImage;
Sign up to request clarification or add additional context in comments.

2 Comments

The last sentence ((ViewGroup) view).addView(picLL); doesn't work, because of: "Type ViewGroup cannot be resolved to a type". Also why we don't use the view? And the last question - how to add the ImageView to this view?
The ViewGroup allows you to add additionally items to that View. You can try just View but I don't believe it will work to add things to it but I will check into it when I get a few more minutes. I will edit my answer to add the ImageView to the layout
5

Using following code you can add image dynamically

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 ImageView imageview = new ImageView(MainActivity.this);
 RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativeLayout);
 LinearLayout.LayoutParams params = new LinearLayout
 .LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

 // Add image path from drawable folder.
 imageview.setImageResource(R.drawable.demo_new_image);
 imageview.setLayoutParams(params); 
 relativelayout.addView(imageview); 

 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.