user3409897
user3409897

Reputation: 143

I want to display the image view at different position on the screen randomly

Here the ImageView is displaying only at one position, after closing the activity the next time the activity is opened the ImageView will be on another position... I want to display the ImageView randomly at diff position on the same activity itself. The image view should appear on one point suddenly the next second ImageView should disappear from that position and appear on the next position. How can i do it?

public class page2 extends ActionBarActivity {
ImageView b2;
int count = 0;
Handler handler = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page2);

    Intent c = getIntent();
    String name = c.getStringExtra("t");
    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();


    b2 = (ImageView) findViewById(R.id.redball);

    AbsoluteLayout.LayoutParams absParams =
            (AbsoluteLayout.LayoutParams)b2.getLayoutParams();

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int height = displaymetrics.heightPixels;


    Random r = new Random();

    absParams.x =  r.nextInt(width ) ;
    absParams.y =  r.nextInt(height );
    b2.setLayoutParams(absParams);

    Animation animation = AnimationUtils.loadAnimation(page2.this, R.anim.fade);
   // Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.activity_move);


    b2.startAnimation(animation);
   // b2.startAnimation(animation1);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            count = count + 1;

        }
    });


    handler = new Handler();

    final Runnable t = new Runnable() {
        public void run() {

            Intent d = new Intent(getApplicationContext(), Page3.class);
            d.putExtra("count", count);
            d.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(d);


        }
    };

    handler.postDelayed(t, 4000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_page2, menu);

    return true;
}

Upvotes: 0

Views: 1042

Answers (5)

Laur Ivan
Laur Ivan

Reputation: 4177

Your solution is almost correct. Unfortunately, it looks like you're restarting the activity from your timer. Instead, you should just trigger the redraw.

This question has a couple of solutions on how to create a recurring timer. The solution with runOnUiThread() should allow you to execute the randomisation and re-displaying of the ImageView.

Upvotes: 0

andy miao
andy miao

Reputation: 11

Don't use AbsoluteLayout, Why not use a custom view draw it?

Upvotes: 1

SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

for calucation

ContainerHeight = blinkerContainer.getHeight(); // total height of screen
        ContainerWidth = blinkerContainer.getWidth(); //total width
        blinkerHeight = blinkView.getHeight();
        blinkerWidth = blinkView.getWidth();
        minTopMargin = 30;
        minLeftMargin = 30;

        maxTopMargin = ContainerHeight - blinkerHeight - 30;
        maxLeftMargin = ContainerWidth - blinkerWidth - 30;

for positioning

LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) blinkView
                    .getLayoutParams();
            params.leftMargin = minLeftMargin
                    + new Random().nextInt(maxLeftMargin - minLeftMargin);
            params.topMargin = minTopMargin
                    + new Random().nextInt(maxTopMargin - minTopMargin);

and you can use AlaramManager for Scheduling

Upvotes: 0

ArBel
ArBel

Reputation: 89

As I understand it, you want that each time the activity is opened, so f you dont want to actually view to the user that the ImageView moves, why are you using Animation? You may just dynamically add the ImageView to the activity each time, and each time assign it different Margin attributes.

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 0, 10, 0); //substitute parameters for left, top, right, bottom
    ImageView iv = new ImageView(getActivity());
    iv.setLayoutParams(params);
    iv.setImageResource(R.drawable.yourimage);
    layout.addView(iv);

Upvotes: 0

Anitha Manikandan
Anitha Manikandan

Reputation: 1170

You can achieve this by using Imageview within FrameLayout. Just change the layoutParams of the image to change its position.

Upvotes: 0

Related Questions