Rotate in ImageView, inside RelativeLayout

I have an ImageView in RelativeLayout :

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#272727" android:orientation="horizontal" > <ImageView android:id="@+id/logoMobile" android:layout_width="100dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:gravity="center" android:paddingTop="2.2dp" android:src="@drawable/logo" /> <ImageButton android:id="@+id/BtnSlide" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_centerVertical="true" android:background="@null" android:src="@drawable/lin" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/BtnSlide" android:gravity="center" android:paddingLeft="30dp" android:text="HEADER" android:textAppearance="?android:attr/textAppearanceMedium" /> 

This xml defines a โ€œtab barโ€ on top of the application, right below the notification bar. I want ImageView rotate around its center. I tried this in my onCreate method:

 logo = (ImageView)findViewById(R.id.logoMobile); ApplyAnimationToObject doAnimation = new ApplyAnimationToObject(logo); doAnimation.startRotationAnimation(); 

And then the ApplyAnimationToObject class:

 package Logic; public class ApplyAnimationToObject { final ImageView image; public ApplyAnimationToObject(ImageView image) { this.image = image; } public void startRotationAnimation() { RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f); anim.setInterpolator(new LinearInterpolator()); anim.setRepeatCount(Animation.INFINITE); anim.setDuration(7000); image.startAnimation(anim); } } 

But ImageView will not rotate or have no animation at all. Can anyone help me out?

EDIT

I tried using this code:

 public void rotateView(View v){ Animation rotateAnimation = new RotateAnimation(0, 360, v.getWidth() / 2, v.getWidth() / 2); rotateAnimation.setDuration(10000); v.startAnimation(rotateAnimation); if(rotateAnimation.hasEnded()) Log.i("+++++ANIMATION+++++", "ENDED!!"); else Log.i("+++++++ANIMATION++++++", "NOT ENDED"); } 

And the output of til LogCat tells me that the animation is not over yet, but the ImageView animation

is absent.
+4
source share
3 answers

try the following code:

  Matrix matrix=new Matrix(); imageView.setScaleType(ScaleType.MATRIX); //required matrix.postRotate((float) angle, pivX, pivY); imagView.setImageMatrix(matrix); 
+3
source

Try another way:

Use RotateAnimation in the view you want to rotate and make sure the animation is set to fill after = true, duration = 0 and fromDegrees = toDgrees

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="45" android:toDegrees="45" android:pivotX="50%" android:pivotY="50%" android:duration="0" android:startOffset="0" /> 

and inflate the animation in code:

 Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); myView.startAnimation(rotation); 
+3
source

Try using the following code:

 public void rotateView(View v){ Animation rotateAnimation = new RotateAnimation(0, 360, v.getWidth() / 2, v.getWidth() / 2); rotateAnimation.setDuration(3000); v.startAnimation(rotateAnimation); } 

Call for rotation:

 rotateView(imgView); 
+2
source

Source: https://habr.com/ru/post/1436551/


All Articles