Rotate an ImageButton on orientation change - android

I have an image button that I want to rotate when the orientation of the device changes. How can I rotate the image with some animation or transition ?

try this code snippet.
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="2000" />
</set>
rorate_anticlockwise.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="2000" />
</set>
for checking orientation of the phone use this code
int orientation =this.getResources().getConfiguration().orientation;
the complete code for MainActivity.java is
public class MainActivity extends Activity{
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
ImageButton image_btn;
Animation ranim_clockwise, ranim_anticlockwise;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image_btn= (ImageButton)findViewById(R.id.imageButton1);
ranim_clockwise = AnimationUtils.loadAnimation(this,R.anim.rotate);
ranim_anticlockwise = AnimationUtils.loadAnimation(this,R.anim.rotate_anticlock);
int orientation =this.getResources().getConfiguration().orientation;
if(orientation==1){ // portrait mode
image_btn.setAnimation(ranim_clockwise);
}
if(orientation==2){ //landscape mode
image_btn.setAnimation(ranim_anticlockwise);
}
}
}
hopefully it will help you.

Related

Infinite loop of sequential animation of ImageButton

I'm new to android studio and I want to animate an imageButton with a sequential animation set. The animation set (animation_boutons.xml)is in res/anim.
I've tried with animationSet in java but the app crashed every time I launched the emulator.
I've spent a long time looking for a solution. I hope someone can help me !
I apologize if it's something obvious.
java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
configureCodeurBouton();
}
private void configureCodeurBouton() {
ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
animBoutons.setRepeatCount(Animation.INFINITE);
boutonCodeur.setAnimation(animBoutons);
boutonCodeur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, codeur.class));
}
});
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:fillAfter="true">
<rotate
android:fromDegrees="0"
android:toDegrees="20"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="1000"
/>
<rotate
android:startOffset="1000"
android:fromDegrees="20"
android:toDegrees="-20"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
<rotate
android:fromDegrees="-20"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="3000"
android:duration="1000"
/>
</set>
Also, Vedprakash Wagh give me the advice to try animBoutons.setRepeatCount(Animation.INFINITE) but it has no effect).
Your app is crashing every time because you're trying to find your ImageButton when the class is created first, and not after the layout is set.
You're getting NullPointerException, as there is no ImageButton with id R.id.boutoncodeur in your View hierarchy when you're trying to find it.
You need to find your ImageView AFTER it is available in your View hierarchy i.e. after your setContentView();
You can either do this:
Remove your second line
ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
as you've already found your ImageView in your configureCodeurButton() function.
Or, you can keep one class variable of ImageView, and make findViewById call after setContentView like below.
public class MainActivity extends AppCompatActivity {
ImageButton boutonCodeur;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boutonCodeur = findViewById(R.id.boutoncodeur);
configureCodeurBouton();
}
private void configureCodeurBouton() {
Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
boutonCodeur.setAnimation(animBoutons);
boutonCodeur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, codeur.class));
}
});
}
}
You can learn more about NullPointerException here. Also, learn how to read the errors from tutorials that are available. Or, simply open the logcat tab in Android Studio when the error occurs to know what Error you're getting.
To make your animation run infinitely, you can add this in your code.
animation.setRepeatCount(Animation.INFINITE)
I just had to change the whole xml anim_boutons file so I have only one animation and not three rotate animations. the repeatMode line says to repeat the animation backwards at each repeat. This gives the expected effect.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:fillAfter="true">
<rotate
android:fromDegrees="-20"
android:toDegrees="20"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
</set>

android animation without delay

I have anim file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="60000"
android:fillAfter="true"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="false" >
<rotate
android:startOffset="0"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
</set>
and I want animation to start immediately. But is starts after 2-3 sec delay. What can be the reason?
Its working properly in my code. So there are no error in XML. Please check your Java. My Java code is:
void startAnim() {
view.clearAnimation();
Animation anim= AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim);
view.startAnimation(anim);
}
private ImageView logo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo=(ImageView)findViewById(R.id.logo);
Animation myanim = AnimationUtils.loadAnimation(this,R.anim.anim);
logo.startAnimation(myanim);
}

How to change direction of object in move transition in animation in android?

I want to change direction of an image in android animation.My fragment is
public class IconAnimation extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.icon_animation, container,false);
return v;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.comedy:
Animation animation1 AnimationUtils.loadAnimation(getActivity(),
R.anim.slide);
ImageView image= (ImageView)v.findViewById(R.id.image);
image.startAnimation(animation1);
break;
}
}
}
and My animation XML is
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="150%p"
android:duration="800"
/>
Its output is something like this:Output
But I want something like this:expected outout
final TranslateAnimation animationt1 = new TranslateAnimation(fromXoffset, toXOffset, fromYoffset,toYoffset);
animationt1.setDuration(300);
animationt1.setFillAfter(true);
animationt1.setAnimationListener(this);
yourView.startAnimation(animation1);
You can do it also by XML :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="#android:anim/linear_interpolator" >
<!-- Use startOffset to give delay between animations -->
<!-- Move -->
<translate
android:duration="800"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="700"
android:toXDelta="50%p" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="800"
android:toYDelta="-10%p" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="1300"
android:toXDelta="75%p" />

Rotate animation android

I'm trying to do a rotating image animation.
I need to rotate an icon around itself just like they do in a progressbar, but what I'm getting is an image rotating around a circle.
Here is my animation code
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2500"
android:repeatCount="infinite"
android:repeatMode="restart"
/>
Where am I going wrong in here?
Thanks
This is the source code for the layout main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="animationText"
android:onClick="AnimClick"/>
<ImageView
android:id="#+id/testImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="image_desc"
android:scaleType="fitCenter"
android:src="#drawable/cat2" />
</RelativeLayout>
To implement the rotation animation, we can define the animation by XML or Java code. If we want to write the animation in the xml, we need to create an animation xml file under /res/anim folder. Here, we create a xml file named rotate_around_center_point.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<rotate
android:duration="2500"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
and this is my Activity:
public class MainActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.testButton);
btn.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point);
animationTarget.startAnimation(animation);
}
}
You could try with the following code, rather than doing it in XML:
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(4000);
rotate.setRepeatCount(Animation.INFINITE);
yourView.setAnimation(rotate);
Well I got where I was wrong. I gave padding to my image which caused it to move aside a little every time it rotated.
Anyhow I removed the padding and now it's working just fine.
Add the following xml in the animation folder
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:duration="4000"
android:fromdegrees="0"
android:pivotx="50%"
android:pivoty="50%"
android:todegrees="360"
android:toyscale="0.0">
</set>
Hope this will help you..
for more information http://www.blazin.in/2013/09/simple-rotate-animation-in-android.html

How can I animate a custom view (ImageView) programmatically?

I want to animate a custom view programmatically. I want it to only animate in the position where it is declare in the xml file, and I don't want it to mess the layout. Also this animation is affected by other factor so the android Animation class is out of the question.
I have test this sample tutorial and watch this videos. My problem is that most tutorial is for animating a sprite into a canvas where you will trace your sprite's location.
How can you animate the custom view without affecting layout and without using the Android Animation class?
Edited:
The custom view will act as a animated-gif and will toggle another set of frames whenever an event activates it.
you can create animation XML and apply that animation from program to your ImageView check following example
you can start animation for your customized view just call viewobject.startAnimation(animationobject);
animation.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
<set
android:interpolator="#android:anim/accelerate_interpolator"
android:startOffset="700">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
</set>
your main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<ImageView android:id="#+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
then your activity class myActivity.java
public class myActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.ImageView01);
//create animation class object
Animation hyperspaceJump =
AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
//start animation for image
image.startAnimation(hyperspaceJump);
}
}
If you want "frame by frame" animation that just "runs" like an animated gif, you can follow:
drawable-animation official tutorial.
Quoting from the tutorial:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust3" android:duration="200" />
</animation-list>
and in your code
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}

Categories

Resources