android - 在 RelativeLayout 中旋转到 ImageView

标签 android android-layout android-widget android-imageview

我在 RelativeLayout 中有一个 ImageView:

<?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" />

此 xml 在应用程序顶部定义了一个“选项卡栏”,就在通知栏下方。我希望 ImageView 围绕其中心旋转。我在我的 onCreate 方法中尝试过这种方式:

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

然后是 ApplyAnimationToObject 类:

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);
  }
}

但是 ImageView 不会旋转或根本没有任何动画。谁能帮帮我?

编辑

我试过使用这段代码:

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");
}

LogCat 的输出告诉我动画还没有结束,但是 ImageView 没有动画

最佳答案

试试下面的代码:

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

关于android - 在 RelativeLayout 中旋转到 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12617632/

相关文章:

android - Phonegap 屏幕尺寸因状态栏高度而异?

android - 如何在 Firebase 中为首次使用的用户定义实验?

android - getWidth() & getHeight() 在 RecyclerView/Fragment 中返回 0

java - ListView 滚动不起作用

android - 这段代码为什么不能在onCreate之后填充ListView?

android - 带有自定义 View 的对话框,setCanceledOnTouchOutside 不起作用

android - 在低于 19 的 api 级别上使用 setUpdateListener

java - 在 Android 中使用四向滑动的 RecyclerView

java - 框架布局在底部导航栏后面?

android - R.styleable 无法解决,为什么?