salih
salih

Reputation: 725

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.

enter image description here

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++;


            }
        });
    }
}

Upvotes: 1

Views: 1692

Answers (1)

The Original Android
The Original Android

Reputation: 6215

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.

Upvotes: 1

Related Questions