0

so my screen has an image view which I dragged down the screen with animation using this code:

    float bottomOfScreen = context.getResources().getDisplayMetrics()
    .heightPixels - (myImageView.getHeight() * 2);
   
    myImageView.animate()
    .translationY(bottomOfScreen)
    .setDuration(durationOfFalling); 

so basically, I had an image and it was dragged down and out of the screen and now my image has disappeared but I need it to come back to the same place that it was before I dragged it down (at the top of the screen) so I can drag it down again whenever I want. Do you have any solutions as to how I can make the image appear again at the top of the screen so I can re-use it?

I also tried adding it through ConstraintLayout and then constraintLayout.addView(myImageView) like that (and probably did it wrong):

    ConstraintLayout ly = findViewById(R.id.ly);
    noteiv1.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT));
    noteiv1.setId(View.generateViewId());
    ly.addView(noteiv1);
    ConstraintSet set = new ConstraintSet();
    set.clone(ly);
    set.connect(noteiv1.getId(), ConstraintSet.LEFT, ly.getId(), ConstraintSet.LEFT);
    set.connect(noteiv1.getId(), ConstraintSet.RIGHT, ly.getId(), ConstraintSet.RIGHT);
    set.connect(noteiv1.getId(), ConstraintSet.TOP, ly.getId(), ConstraintSet.TOP);
    set.connect(noteiv1.getId(), ConstraintSet.BOTTOM, ly.getId(), ConstraintSet.BOTTOM);
    set.applyTo(ly);

*noteiv1 is the image view I need to re-use

I'd be glad for some help :)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

This has already been asked and answered.

Here's an answer from there.

I think you should clone the layout after adding your ImageView.

ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();

ImageView childView = new ImageView(this);
// set view id, else getId() returns -1
childView.setId(View.generateViewId());
layout.addView(childView, 0);

set.clone(parentLayout);
// connect start and end point of views, in this case top of child to top of parent.
set.connect(childView.getId(), ConstraintSet.TOP, parentLayout.getId(), ConstraintSet.TOP, 60);
// ... similarly add other constraints
set.applyTo(parentLayout);
Calphonic
  • 51
  • 1
  • 5
  • But that's exactly what I did, I added my ImageView and then cloned the layout - ly.addView(noteiv1); ConstraintSet set = new ConstraintSet(); set.clone(ly); – barzilay kfir Nov 23 '20 at 07:01