ImageButton animation not applied to all states - android

I have an ImageButton whose image rotates with the device orientation so that the image/ button is always horizontal (note that the rotations is handled manually, not through the activity life-cycle).
This all works great, except when the user presses the button (state is pressed), the 90' rotation is lost temporarily.
Button Animation;
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200" android:fillAfter="true">
</rotate>
The image button drawable;
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/btn_go_back_pressed" />
<item android:state_enabled="false"
android:drawable="#drawable/btn_go_back_disabled" />
<item android:drawable="#drawable/btn_go_back_normal" />
</selector>
Loading the animation;
AnimationUtils.loadAnimation(mContext, R.anim.button_rotate90);
And then when the device orientation changes;
case MessageQue.SET_TO_PORTRAIT:
inPortrait = true;
for (View ib : buttonsToAnimate) {
if (ib instanceof TextView) {
ib.startAnimation(cntPortraitAnim);
} else {
ib.startAnimation(btnPortraitAnim);
}
}
Does anyone have any ideas on why, and more importantly, how to fix it?

Excellent pointer Whitney - this solved visibility issues I was having as well.
All the above code has been replaced with;
case MessageQue.SET_TO_LANDSCAPE:
for (View ib : buttonsToAnimate) ib.animate().rotation(0).setDuration(200);
break;
case MessageQue.SET_TO_PORTRAIT:
for (View ib : buttonsToAnimate) ib.animate().rotation(-90).setDuration(200);
break;
Thanks.

Related

How to rotate ImageButton source Image with Animation.

I need to rotate the drawable resource of ImageButton. I've succeeded rotating functionality to my Button but rotating functionality affect whole button. All I want to do is rotate only drawable inside ImageButton.
How to handle this situation ?
PS: I accessed drawable inside ImageButton but I wasn't able give any animation functionality.
Thanks in helpings
Here my ImageButton xml;
<ImageButton
android:id="#+id/button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="-40dp"
android:background="#color/titlebackground_color"
android:src="#drawable/open" />
Rotate first xml;
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<rotate
android:fromDegrees="-180"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:startOffset="0"
/>
</set>
Rotate Second xml;
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<rotate
android:fromDegrees="-180"
android:toDegrees="-0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:startOffset="0"
/>
</set>
Animation functionality;
public class LayerInfoFragment extends Fragment {
int count = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layer_info_main, container, false);
btnClose = (ImageButton) v.findViewById(R.id.button);
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation rotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_first);
Animation rotatex = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_last);
if (count % 2 == 0) {
v.setRotation(180);
v.setAnimation(rotate);
} else {
v.setRotation(0);
v.setAnimation(rotatex);
}
count++;
}
});
}
}
There are 2 things to try I think.
1) Add code v.startAnimation(rotate); after v.setAnimation(). I suspect your animation never got started. I was thinking before that android:startOffset in the settings would trigger a start but that is not clear to me.
2) Try RotateAnimation, a direct subclass of Animation, instead of Animation objects. It seems many used RotateAnimation more than anything else. Besides that, the other issue I suspect is in the layout file.
Example:
RotateAnimation rotate = (RotateAnimation) AnimationUtils.loadAnimation...
Tell us what happens.

Apply two alternate rotations to a View in Android

I'd like to apply 2 alternate rotation animations to a View. Each rotation should start after the click on the same View.
The result I'm looking for is:
the user clicks on the View and it rotates from 0° to 135°
the user clicks again on the View and it rotates from 135° to 0° (to the initial state)
The problem is that, when the user performs the second click, the Button resets to the initial aspect before starting the animation correctly.
I'm targeting Android APIs<11 so I'm using the startAnimation() method. The animations are applied to a Button view defined as follows:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The animations are the following
rotate_cw.xml (0° to 135° rotation):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
android:duration="300"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="135" />
</set>
rotate_ccw.xml (135° to 0° rotation):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="300"
android:fromDegrees="135"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="0"/>
</set>
The animations are then applied in this way, where flag is a boolean global variable:
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (flag) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate_cw);
b.startAnimation(a);
} else {
Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate_ccw);
b.startAnimation(a);
}
flag = !flag;
}
});
Am I missing anything?
Thanks in advance for your help.

Can't get Actionbar Icon to animate

I'm trying to get my refresh button on my Actionbar to rotate, but I'm having some trouble getting it to work. The View just seems to lose it's horziontal margin/padding (which I haven't set anywhere), and does not rotate. I get no null errors, crashes, or anything else that could point me in the right direction. What am I doing wrong? Any help would be much apreciated.
Main.java:
public class Main extends ActionBarActivity{
...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
refresh();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
public void refresh() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView = (ImageView) inflater.inflate(R.layout.action_refresh, null);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
rotation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(rotation);
MenuItem item = menu.findItem(R.id.action_refresh);
item.setActionView(R.layout.action_refresh);
}
}
Menu.XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="refresh"
android:actionLayout="#layout/action_refresh"
yourapp:showAsAction="ifRoom"
/>
<item
android:id="#+id/category_spinner_item"
android:showAsAction="ifRoom"
android:actionLayout="#layout/action_sort"
/>
</menu>
action_refresh.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_refresh"
/>
anim/rotate.xml
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="10000"
android:interpolator="#android:anim/linear_interpolator" />
Just change this line:
item.setActionView(R.layout.action_refresh);
To this:
MenuItemCompat.setActionView(item, imageView);
The reason it doesn't animate with the original line is because you are setting the action view to the static image resource, not the imageView that you set up to animate.

Stop animation until specific drawable is displayed

I have a AnimationDrawable which shows the numbers 0-9.
When the Activity is started, the animation starts, too.
it starts with 0, then 1,2,3 ....... and stops at picture "9";
How can I stop this little animation when f.e. "5" is displayed?
Is there any solution with a while-loop?
ImageView view = (ImageView)findViewById(R.id.iv);
AnimationDrawable animation = (AnimationDrawable)iv.getBackground();
animation.start();
You can try to do it the following way:
final ImageView imageView = (ImageView)findViewById(R.id.image);
if (null != imageView) {
final AnimationDrawable bgAnimation = (AnimationDrawable) imageView.getBackground();
bgAnimation.start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
bgAnimation.stop();
}
}, 5500);
}
Assuming that You have animation xml like the following:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/choose12"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_1"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_2"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_3"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_4"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_5"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_6"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_7"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_8"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_9"
android:duration="1000"/>
</animation-list>
However, this way will work not stable with small delays. So, I would suggest to take a look at PropertyAnimation if You need better control over animation process.

Android AnimationDrawable starts, and when done hangs on first frame

I'm trying to get the AnimationDrawable on my Android app to repeat.
I configured the android:oneshot on false. (tried both with java and with XML)
Still, whenever the animation is played, once it's done, it goes back to the first frame, and stops
This is my XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/network_scanning" android:oneshot="false">
<item android:drawable="#drawable/network_wireless_0" android:duration="300" />
<item android:drawable="#drawable/network_wireless_1" android:duration="300" />
<item android:drawable="#drawable/network_wireless_2" android:duration="300" />
<item android:drawable="#drawable/network_wireless_3" android:duration="300" />
<item android:drawable="#drawable/network_wireless_4" android:duration="300" />
</animation-list>
and this is the code to start the animation:
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
LinearLayout network = (LinearLayout) findViewById(R.id.wifi_anim);
AnimationDrawable anim = (AnimationDrawable) network.getBackground();
if (hasFocus)
{
anim.setOneShot(false);
anim.setCallback(network);
anim.setVisible(true, true);
anim.start();
}
}
hi you can get solution from here
http://docs.mono-android.net/Android.Graphics.Drawables.AnimationDrawable

Categories

Resources