How to Rotate an Arrow Drawable about it's Center - android

I have an arrow drawable that I simply want to rotate without it moving either in the x or y direction.
To explain further, consider a ball spinning about a fixed point or the circular Android progressbar.
Additionally, the drawable will rotate -180 degree when clicked and 180 degress when clicked again.
Please, is there anyway this can be done programmatically?
Update
From Jason Wihardja's answer, I was able to achieve this:
#Override
public void onClick(View v) {
if (v.getId() == imageButton.getId()) {
int visibilty =newsBody.getVisibility();
if (visibilty == View.VISIBLE) {
Animation animation = new RotateAnimation(180.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new DecelerateInterpolator());
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setDuration(300);
imageButton.startAnimation(animation);
newsBody.setVisibility(View.GONE);
} else {
Animation animation = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new DecelerateInterpolator());
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setDuration(300);
imageButton.startAnimation(animation);
newsBody.setVisibility(View.VISIBLE);
}
}
}
But is there anyway I can avoid repeating the animation building? Like just build it once and toggle the degrees in each click.

You could use RotateAnimation class to do that. Here's an example on how to do that
RotateAnimation rotateAnimation = new RotateAnimation(180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new DecelerateInterpolator());
rotateAnimation.setRepeatCount(0);
rotateAnimation.setDuration(animationDuration);
rotateAnimation.setFillAfter(true);
arrowImageView.startAnimation(rotateAnimation);
To rotate in a different direction, just flip the angle parameters.

Related

ImageView clear animation

I have an animation in my code.
But I want when I click Button my ImageView return to original place(reset animation)
I have a method for that.
My code is :
private void returnToOriginalRotationState() {
RotateAnimation animation = new RotateAnimation(0.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration((long) 2*1000);
animation.setFillAfter(true);
animation.setFillEnabled(true);
imgHeade.startAnimation(animation);
}
It works as well
But
animation.setDuration((long) 2*1000);
not working !!!
try this for clear animation:
imgHeade.clearAnimation();

Android : Click event is not triggered at time of animation

I have a Button. On click of button i animate the TextView.
At the time of animation click is not working for texview.
buttonclick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 10.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 10.0f);
animation.setDuration(100000);
textView.startAnimation(animation);
}
});
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Click",Toast.LENGTH_SHORT).show();
}
});
Animations are only animating views, not changing, its an illusion... You are using TranslateAnimation, so you are moving your views "look", not the view itself. Ewentually you may use setFillAfter(true) method, then View will be clickable AFTER animation end (but your anim is long/infinite...).
Consider using Animator instead, which is changing "parameters" (in your case real position) of view with every frame of anim
ObjectAnimator animX = ObjectAnimator.ofFloat(textView, "x", newx);
ObjectAnimator animY = ObjectAnimator.ofFloat(textView, "y", newy);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();
try this
PropertyValuesHolder x = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder y = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(mTextView, x, y).start();
this way, you can animate whole view object--not just the contents.

Animation set for image view

I want multiple animations to be played on the same ImageView. I'm using an animation set but it never fades in. But it does rotate. Can somebody tell me what I'm missing ?
AnimationSet s = new AnimationSet(false);//false mean dont share interpolators
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(800);
s.addAnimation(fadeIn);
s.addAnimation(anim);
iv.startAnimation(s);
You are missing:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
fadeIn.setFillEnabled(true); // to apply setFillBefore and setFillAfter
fadeIn.setFillBefore(true); // it means that at start of animation you set alpha="0"
fadeIn.setFillAfter(false);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
Also, in your styles you must remove alpha="0"
AlphaAnimation is working on your image as it is. So if you set alpha="0" it works between this 0. If alpha="1" or without it (when style default is alpha="1"), it works between 0-1.

Animate subview after animate rootview

To summarize and simplify my problem I try to animate a root view and then animate subview.
The first animation work well, but the second most of the time, stay in place.
The layout :
<FrameLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
The sample code :
TranslateAnimation translateAnimation=new TranslateAnimation(0, 0, 0, 300);
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(true);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(750);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setRepeatMode(Animation.RESTART);
imageView.startAnimation(rotateAnimation);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
container.startAnimation(translateAnimation);
Sometimes I see the imageview tilted. Or rotate, but just one part.
Someone have an idea of what happen ?
I also tried with setFilterAfter(false) and move my container with setTranslationY() on onAnimationEnd but some frames are visible.
Thanks.
EDIT
In the real case, the TranslateAnimation is on a ViewPager (to QuickReturn pattern) and the RotateAnimation (to PullToRefresh pattern) inside fragment of ViewPager.
So animations is not necessarily sequential like I have done above, and strongly separated (ViewPager/Fragment).
EDIT2
I just see that the touch area don't move with the view.
I'm not saying it's going to work but let's give this a try:
Let's separate animations and not chain them with animationListener; so instead, let's use the ObjectAnimator and AnimationSet to help us sequence animations for us.
ObjectAnimator translateContainer = ObjectAnimator.ofFloat(container, "translationY", 300);
translateContainer.setDuration(2000);
ObjectAnimator rotateImage = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
rotateImage.setDuration(750);
AnimatorSet animSet = new AnimatorSet();
animSet.playSequentially(translateContainer, rotateImage);
animSet.start();

How to do webview animation in android

suppose i have three text as heading.Any how you can take textview or anything.one text on left,one in center and one in right side.so i want an animation when i will slide the screen left then left text will be gone,center text will come to left and right text will come to center.Same thing for right slide.Can anyone suggest how can i achieve this type of animation.Plz help.
You can try to create an animation variable :
private Animation leftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
then
set this action to your frame:
public void onLeftArrowClick(View v) {
frame.setAnimation(moveComingSoonToBottomAnim); // your Frame variable
animVariable.startNow();// your anime variable
frame.setVisibility(View.VISIBLE);
rightFrame.setVisibility(View.GONE); // hide your right text view
}

Categories

Resources