The Translate animation works, but I cant scale it. What happens when I add it in, the ImageView for it disappears
TranslateAnimation translateMeteor = new TranslateAnimation(32,221,40,300);
translateMeteor.setDuration(5000);
translateMeteor.setRepeatCount(Animation.INFINITE);
translateMeteor.setInterpolator(new LinearInterpolator());
ScaleAnimation scaleMeteor = new ScaleAnimation(1500,200,1300,100);
scaleMeteor.setDuration(5000);
scaleMeteor.setRepeatCount(Animation.INFINITE);
scaleMeteor.setInterpolator(new LinearInterpolator());
AnimationSet meteorAnimation = new AnimationSet(false);
meteorAnimation.addAnimation(translateMeteor);
meteorAnimation.addAnimation(scaleMeteor);
meteor.startAnimation(translateMeteor);
meteor.startAnimation(meteorAnimation);
For those wondering, I have tried to run it without TranslateAnimation, but same issue happens
Related
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 :)
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);
I'm trying to have two animations occur in one activity with the first happening first and only once it's done, does the second start. The ObjectAnimator moves the imageView up the screen using the .ofFloat() method and then I want a loading animation which loops through several image files to create a loading-like effect.
This is my code:
//Animations
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
//Change cloud from sleeping to awake/happy
g2ndSleepingClouds.setBackgroundResource(R.drawable.clouds_happy_formatted);
//Move image view 250 pixels up the screen vertically
ObjectAnimator animation1 = ObjectAnimator.ofFloat(g2ndSleepingClouds, "translationY", -250f);
animation1.setDuration(1000);
animation1.start();
//Loading animation
loadingImageView.setBackgroundResource(R.drawable.loading_1);
loadingAnimation = (AnimationDrawable) loadingImageView.getBackground();
loadingAnimation.stop();
loadingAnimation.start();
}
}, 1000);
The error I get in Logcat is:
"android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable"
Any help?
I have a spinning wheel, which is an ImageView, in my app, and the animation is working well, but the issue is for just a split second it snaps back to it's original position (0 degree rotation). The .gif below shows what I am talking about.
The last split second of the rotation it has that awkward snap back that makes the animation look extremely unsmooth.
I am looking for a way to fix this but I cannot quite figure it out. This is my code right now that animates the ImageView and rotates it to the final position of the animation
ImageView readyButton = findViewById(R.id.spinnerready);
final ImageView spinnerSpin = findViewById(R.id.spinnerspin);
final Animation animRotate = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animRotate.setDuration(5000);
animRotate.setRepeatCount(0);
readyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float newDeg = ROTATE_TO;
spinnerSpin.startAnimation(animRotate);
spinnerSpin.postDelayed(new Runnable() {
#Override
public void run() {
spinnerSpin.setRotation(ROTATE_TO);
}
}, 5000);
while(newDeg > 360) {
newDeg = newDeg - 360;
}
}
});
I have also tried changing the delay of the rotation to 4500 instead of 5000 but the issue is not fixed. Is there a way to eliminate the ImageView from going back to its original position after the animation?
I am trying to execute an animation in which I have two fragments stacked on top of each other.
The top fragment is a details fragment.
the bottom fragment is a menu list view fragment.
I did this by creating two overlapping framelayouts in the activity layout. I want to be able to do an animation in which the background fragment would be revealed in a fashion similar to a door opening leaving only 20 percent of the edge of the top fragment in view.
I tried doing this animation with the standard view animation library available to API 9 but it seemed that only the pixels were moved and but the touch mapping still corresponded to the top fragment and the bottom menu fragment could not be accessed.
So I downloaded the nineoldandroids library and tried to user AnimatorSet with ObjectAnimators to do the animation... except this time when the fragment is animated away it reveals only a gray background rather than the fragment in the back like before.
This is a code snippet on how I tried to implement a simple translation to reveal the background fragment
private void animateFragmentOut() {
activeFragment = (Fragment)getSupportFragmentManager().findFragmentById(R.id.nav_item_fragment_container);
View myView = activeFragment.getView();
AnimatorSet set = new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(myView, "translationX", 0, 230)
);
set.setDuration(500).start();
}
Why is the background fragment not shown when I use this animation?
How do I use nineoldandroids to reveal the background fragment properly?
I ended up solving this problem by using a listener to inflate the background view right at the start of the animation using the following code
private void animateFragmentOut() {
activeFragment = (Fragment)getSupportFragmentManager().findFragmentById(R.id.nav_item_fragment_container);
View myView = activeFragment.getView();
Animator.AnimatorListener listener = new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
launchNavigationFragment();
}
#Override
public void onAnimationEnd(Animator animation) {
}
};
ObjectAnimator rotateY = ObjectAnimator.ofFloat(myView,"rotationY",-15f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(myView,"scaleX",0.8f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(myView,"scaleY", 0.8f);
ObjectAnimator translateX = ObjectAnimator.ofFloat(myView,"translationX",400f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(rotateY,scaleX,scaleY,translateX);
animatorSet.addListener(listener);
animatorSet.setDuration(700);
animatorSet.start();
}
Saldy despite this working great on API levels 11> and up on my API 9 device I am still having the same problem I had with the standard animation library. The view pixels translate but the touch areas stay mapped in the same place. So I cannot interact with the background navigation menu fragment.
I was making this simple and cool looking animation.
And using this question - How to spin an android icon on its center point? I got my icon to rotate. But after the rotation I wish to move the image view upwards. How can I achieve this?
I'm using this code to move the imageview upwards.
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -700);
animation.setDuration(1*1500);
animation.setRepeatCount(0);
ImageButton logo_icon_two = (ImageButton) findViewById(R.id.logo_icon);
logo_icon_two.startAnimation(animation);
The problem is that when I add the code, the image view moves up but then goes back to the spot it was in, also the rotation animation stops. My full class below -
public class WelcomeActivity extends Activity {
private static final float ROTATE_FROM = 0.0f;
private static final float ROTATE_TO = -10.0f * 360.0f;// 3.141592654f * 32.0f;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_activity);
ImageButton logo_icon = (ImageButton) findViewById(R.id.logo_icon);
RotateAnimation r; // = new RotateAnimation(ROTATE_FROM, ROTATE_TO);
r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration((long) 1*1500);
r.setRepeatCount(0);
logo_icon.startAnimation(r);
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -700);
animation.setDuration(1*1500);
animation.setRepeatCount(0);
ImageButton logo_icon_two = (ImageButton) findViewById(R.id.logo_icon);
logo_icon_two.startAnimation(animation);
}
}
Question is, how can I make it that, when opening app. the image rotates and then stops, and then the image is then moved up.
You have to call setFillAfter(true) method for both your animations, and if necessary, change the layoutparams of imageview after animation end then invalidate once.