I have a question similar to this but I am wanting to make only the text on the button flash. I don't want the button background to also flash.
This is my R.anim.blink.xml file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500"
android:startOffset="20"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
But this code...
Animation blinkingAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
myButton.setAnimation(blinkingAnimation);
...makes the whole button blink. So how to make just the text blink (so the button background is shown all the time)?
Simple way:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button) findViewById(R.id.btn);
final ObjectAnimator colorAnim = ObjectAnimator.ofInt(btn, "textColor", Color.BLACK, Color.TRANSPARENT); //you can change colors
colorAnim.setDuration(500); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
colorAnim.end();
colorAnim.cancel();
}
});
}
It will finish flashing after pressing.
EDIT:
You can define your animation in xml (use objectAnimator):
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="textColor"
android:duration="500"
android:valueFrom="#000000"
android:valueTo="#android:color/transparent"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:interpolator="#android:anim/accelerate_interpolator" />
and use it in your code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.blink);
final Button btn = (Button) findViewById(R.id.btn);
animator.setTarget(btn);
animator.start();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
animator.end();
animator.cancel();
}
});
}
XML must be in the 'animator' folder.
Try this code in oncreate method of Activity
final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
final Button btn = (Button) findViewById(R.id.your_btn);
btn.startAnimation(animation);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View view) {
view.clearAnimation();
//also your extra work here
}
});
I suggest you use FrameLayout instead.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="any color" >
<TextView
android:id="#+id/bn1"
android:layout_width="wrap_content"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_height="wrap_content"
android:text="some_txt"/>
</FrameLayout>
Now Apply Blink Animation that TextView
in kotlin we can do that.
blink the element
res\animator\blink.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="alpha"
android:duration="1000"
android:valueFrom="1.0"
android:valueTo="0.1"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:interpolator="#android:anim/accelerate_interpolator" />
in the activity.kt
lateinit var animator : ObjectAnimator
onCreate
animator = AnimatorInflater.loadAnimator(this, R.animator.blink) as ObjectAnimator
animator.target = targetElement
animator.start()
to stop and cancel
animator.end()
animator.cancel()
targetElement?.alpha = 1.0f
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
blink only text
val valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f)
valueAnimator.duration = 1000
valueAnimator.repeatCount = ValueAnimator.INFINITE
valueAnimator.repeatMode = ValueAnimator.REVERSE
valueAnimator.addUpdateListener { it ->
val fractionAnim = it.animatedValue as Float
targetElement?.setTextColor(
ColorUtils.blendARGB(Color.parseColor("#00cc00"),
resources.getColor(R.color.transparent), fractionAnim))
}
valueAnimator.start()
targetElement?.setOnClickListener{
valueAnimator.cancel()
targetElement?.setTextColor(Color.parseColor("#00cc00"))
}
blink_effect.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
MainActivity.java
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink_effect);
yourWidget.startAnimation(animation1);
Related
I am trying to create a drop-down animation. When the user taps on a specified button #+id/btnToHideView, I would like a view #+id/relativeLayoutControls to drop down/slide up (VISIBLE / GONE).
Here is how the layout file looks:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnToHideView"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/hide_btn"
/>
<RelativeLayout
android:id="#+id/relativeLayoutControls"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_marginRight="6dp"
android:layout_marginEnd="6dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
//I have buttons in this layoout
</RelativeLayout>
</LinearLayout>
and this is what I have tried:
I created 2 animation files in res/anim
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
Then I tried handling this by doing:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
controlsHide = (RelativeLayout) findViewById(R.id.relativeLayoutControls);
final Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_down);
final Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
btnToHideView = (Button) findViewById(R.id.btnToHideView);
btnToHideView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//I just did slide_up to test if its working
controlsHide.startAnimation(slide_up);
}
});
I followed this post, but when I tap on the button nothing happens. In logcat, it only prints ACTION_DOWN.
Please try this:
SlideUp:
Animation slideUp = AnimationUtils.loadAnimation(activity, R.anim.slide_up);
view.startAnimation(slideUp);
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
or
view.animate().translationY(0);
Dropdown:
view.animate().translationY(view.getHeight());
Here is my source code implementation (refer here), You can use them
// Initially hide/show the content view.
redLayout = mView.findViewById(R.id.history_operation);
//Load animation
slide_down = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
slide_up = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
Create an animation xml for slide down and up. You can feel free to set the duration for the animation if you like. Here I set 500 milli:
<translate
android:duration="500"
android:fromYDelta="-100%"
android:toYDelta="0" />
and
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="-100%" />
Implementing the animation in the source code:
slide_up.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//When the animation was finished, set gone to the view
redLayout.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
//When the animation start, set visible to the view
redLayout.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Finally, calling the toggle function to start the defined animations
private void toggle1() {
// Start animation
if(isFadeOut){
redLayout.startAnimation(slide_down);
}else {
redLayout.startAnimation(slide_up);
}
isFadeOut = !isFadeOut;
}
Usually I don't like to use dependencies to keep app light as much as possible. But Animation library has fun various animations:
Java:
Import render animations
import render.animations.*;
Start animation
// Declare yourlayout
yourlayout AppleText = findViewById(R.id.yourlayout);
// Create Render Class
Render render = new Render(MainActivity.this);
// Set Animation
render.setAnimation(Attention.Wobble(AppleText));
render.start();
Kotlin:
Import render animations
import render.animations.*
Start animation
// Declare yourlayout
val yourlayout: View = findViewById(R.id.layout_tag)
// Create Render Class
val render = Render(this)
// Set Animation
render.setAnimation(Bounce().InDown(yourlayout))
render.start()
Bonuse().InUp(yourview) and try Bonuse().InDown(yourview)
Slide().InUp(yourview) and try Slide().InDown(yourlayout)
Here is what in the animation xml :
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="4500"
and here where I call it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
setContentView(R.layout.prayerlayout);
txt = (TextView) findViewById(R.id.textView2);
txt.setText(getString(R.string.eighth));
//apply animation
fade1 = AnimationUtils.loadAnimation(this, R.anim.fade10);
txt.startAnimation(fade1);
}
The issue is that the text doesn't show up for the duration of the animation, then it appears suddenly. I didn't notice this problem in older versions of android. It was working fine, but with Lollipop or Jellybeans it doesn't work. Thanks for any help.
It turned out that my text was too long causing the fade-in animation not to work. I applied the animation on the scrollView instead, and it did the effect I wanted.
I have this in the fadein XML.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
And the code
public class MainActivity extends Activity {
Animation animFadein,animFadeout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
animFadeout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);
Update();
}
public void Update(){
TextView lblEstadoPuerta = (TextView) findViewById(R.id.lblEstadoPuerta);
ImageButton btnabrirpuerta = (ImageButton) findViewById(R.id.btndoorstate);
btnabrirpuerta.startAnimation(animFadeout);
btnabrirpuerta.setVisibility(View.GONE);
btnabrirpuerta.setImageResource(R.drawable.go_down);
btnabrirpuerta.setVisibility(View.VISIBLE);
btnabrirpuerta.startAnimation(animFadein);
}
}
Need to import this..
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
Did you add some Interpolator?
You can read this:
http://developer.android.com/intl/es/guide/topics/resources/animation-resource.html
Your fade10.xml animation file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="4500"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
Your Activity file
public class FadeActivity extends Activity {
private TextView txt;
private Animation fade1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prayerlayout);
txt = (TextView) findViewById(R.id.textView2);
txt.setText(getString(R.string.eighth));
//apply animation
fade1 = AnimationUtils.loadAnimation(this, R.anim.fade10);
txt.startAnimation(fade1);
}
}
I wrote code which can change background image random every 5 second.now i want to use fade in/out animation to change background image,but I do not know how I can use this animation.
This is a my source:
void handlechange() {
Handler hand = new Handler();
hand.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// change image here
change();
}
private void change() {
// TODO Auto-generated method stub
Random rand = new Random();
int index = rand.nextInt(image_rundow.length);
mapimg.setBackgroundResource(image_rundow[index]);
handlechange();
}
}, 4000);
}
At the moment everything is OK. I can change background image random,but I don't know how can I use animation fade in/out.
If anyone knows solution please help me,
thanks.
You need to call these codes.
First, you have to make two xml files for fade in & out animation like this.
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:fillAfter="true"
android:duration="2000"
/>
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:fillAfter="true"
android:duration="2000"
/>
</set>
Second, you have to run animation of imageView like below and also you have to set AnimationListener to change fadeout when fadein finish.
Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out);
imageView.startAnimation(fadeOut);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Animation fadeIn = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
imageView.startAnimation(fadeIn);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
For fade in animation , create new folder named 'anim' in your res folder and inside it, create 'fade_in.xml' with following code
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="#android:integer/config_mediumAnimTime"
android:fromAlpha="0.0"
android:interpolator="#android:anim/decelerate_interpolator"
android:toAlpha="1.0" />
</set>
now to run this animation on your imageview, use following code in your activity
Animation anim = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
imageView.setAnimation(anim);
anim.start();
for fadeout animation, just swap values of android:fromAlpha and android:toAlpha
Hope this helps.
You can use relativeLayout, and add one layer of background view which set both height and width match_parent. All other UI element should put on top of this view. In your code. Define fadeOut and fadeIn animation. Find that background view by id, then do this:
xxxBackground.startAnimation(fadeOut);
xxxBackground.setBackgroundResource(R.drawable.your_random_pic);
xxxBackground.startAnimation(fadeIn);
You can use some interpolator to tune the performance.
You need AnimationDrawable with animation.
First step AnimationDrawable
-Create a file /res/anim/anim_android.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false">
<item android:drawable="#drawable/android_1"
android:duration="100"/>
<item android:drawable="#drawable/android_2"
android:duration="100"/>
<item android:drawable="#drawable/android_3"
android:duration="100"/>
<item android:drawable="#drawable/android_4"
android:duration="100"/>
<item android:drawable="#drawable/android_5"
android:duration="100"/>
<item android:drawable="#drawable/android_6"
android:duration="100"/>
<item android:drawable="#drawable/android_7"
android:duration="100"/>
</animation-list>
-Add a ImageView with android:src="#anim/anim_android".
<ImageView
android:id="#+id/myanimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#anim/anim_android" />
Second step
-In your activity create AnimationDrawable and Animation
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.setOneShot(true);
animationDrawable.start();
Animation animation = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_in);
imageView.setAnimation(animation);
animation.start();
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_out);
imageView.startAnimation(fadeOut);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
you don't need Handler.
Answer by kimkevin animates a View (be it an ImageView, TextView etc.) and not a background of a View. Which means if you want to just highlight any View, you'll have to add another View behind it (lets call it backgroundView) and animate that backgroundView.
Use the following code for animating background of a view
val startColor = view.solidColor
val endColor = ContextCompat.getColor(context, R.color.your_color)
val colorAnim = ObjectAnimator.ofInt(view, "backgroundColor", startColor, endColor, startColor)
colorAnim.duration = 2000
colorAnim.setEvaluator(ArgbEvaluator())
colorAnim.start()
I am trying to make a TextView grow and then shrink. This process should be repeated infinitely. How do I achieve that? My scale.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="1400" />
</set>
Here's my animation method:
public void runAnimation() {
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
a.setRepeatMode(Animation.REVERSE);
a.setRepeatCount(Animation.INFINITE);
a.reset();
textView.clearAnimation();
textView.startAnimation(a);
}
Now my problem is that the TextView grows, but then immediately reverses to the starting appereance. Why won't it go reverse, shrink and repeat the process?
Try this :
public void runAnimation() {
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
a.setRepeatMode(Animation.REVERSE);
a.setRepeatCount(-1);
textView.clearAnimation();
textView.startAnimation(a);
}
EDIT
Here is my own code that is working right now :
Call animate like animate(textView)
public void animate (View view) {
mAnimation = new ScaleAnimation(0.3f,0.5f,0.3f,0.5f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.45f);
mAnimation.setDuration(300);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.REVERSE);
mAnimation.setInterpolator(new AccelerateInterpolator());
mAnimation.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
view.setAnimation(mAnimation);
}
Try this found on some blog. Works perfect for me.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<scale
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" />
</set>
Here is a simple way to do so
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(textView,
PropertyValuesHolder.ofFloat("scaleX", 2.0f),
PropertyValuesHolder.ofFloat("scaleY", 2.0f));
scaleDown.setDuration(410);
scaleDown.setRepeatCount(ObjectAnimator.INFINITE);
scaleDown.setRepeatMode(ObjectAnimator.REVERSE);
scaleDown.start();
Hope it helps.
In your animation XML file add:
android:repeatCount="infinite"
And on your animation activity:
textmain.startAnimation(animationMainText);
animationMainText.setRepeatMode(Animation.REVERSE);
I have no idea for this animation.
How can I do it via XML like that? Or another solution?
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true">
......
</set>
Thanks for your help
This code shakes a view in horizontal direction
shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:interpolator="#anim/cycle_5"
android:toXDelta="10" />
cycle_5.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="5" />
Method to shake ImageView
public void onShakeImage() {
Animation shake;
shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
ImageView image;
image = (ImageView) findViewById(R.id.image_view);
image.startAnimation(shake); // starts animation
}
1) vibrate or
2) shake
(using property animation)the following code working for me.
ObjectAnimator rotate = ObjectAnimator.ofFloat(animateView, "rotation", 0f, 20f, 0f, -20f, 0f); // rotate o degree then 20 degree and so on for one loop of rotation.
// animateView (View object)
rotate.setRepeatCount(20); // repeat the loop 20 times
rotate.setDuration(100); // animation play time 100 ms
rotate.start();
Create shake.xml inside anim directory
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="0"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toDegrees="0" />
<translate
android:duration="70"
android:fromXDelta="40"
android:interpolator="#android:anim/linear_interpolator"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="-40" />
inside your java file add below method
public void animateView(View view){
Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
view.startAnimation(shake);
}
and pass your view inside method for animation
animateView(yourView);
Create animation file in anim directory:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromDegrees="-10"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="10" />
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
your_view.startAnimation(shake);
This works for me smoothly as i Expected
private void Shake(View view)
{
RotateAnimation rotate = new RotateAnimation(-5, 5,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(250);
rotate.setStartOffset(50);
rotate.setRepeatMode(Animation.REVERSE);
rotate.setInterpolator(new CycleInterpolator(5));
view.startAnimation(rotate);
}
I did create the BUZZ like animation for imageView. Here I also have added delay to make it really like Buzzy effect
In your Activity:
Here, animShake is android.view.animation
animShake = AnimationUtils.loadAnimation(this, R.anim.shake)
imageView.startAnimation(animShake)
Don't forget to add AnimationListener for little delay of BUZZ effect
animShake.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
Handler().postDelayed({
imageView.startAnimation(animShake)
}, 1000)
}
override fun onAnimationStart(animation: Animation?) {
}
})
anim shake XML file having only translate:
android:duration="75"
android:fromXDelta="-18%"
android:repeatCount="11"
android:repeatMode="reverse"
android:toXDelta="18%" />