简体   繁体   English

在RelativeLayout内旋转到ImageView

[英]Rotation to an ImageView, inside a RelativeLayout

I have an ImageView in a RelativeLayout : 我在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" />

This xml defines a "Tab bar" ontop of the application, just beneath the notification bar. 此xml在应用程序的顶部定义了一个“标签栏”,位于通知栏下方。 I want the ImageView to rotate around its center. 我希望ImageView围绕其中心旋转。 I tried it this way in my onCreate method: 我在onCreate方法中尝试过这种方式:

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

And then the ApplyAnimationToObject class: 然后是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);
  }
}

But the ImageView will not rotate or have any animation at all. 但ImageView根本不会旋转或有任何动画。 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 til LogCat tells me that the animation hasn't ended yet, but there is no animation to the ImageView 输出直到LogCat告诉我动画还没有结束,但ImageView没有动画

try the following code: 尝试以下代码:

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

Try another way also: 尝试另一种方式:

Use RotateAnimation on the View you want to Rotate, and make sure the Animation set to fillAfter=true, duration=0, and fromDegrees=toDgrees 在要旋转的视图上使用RotateAnimation,并确保将Animation设置为fillAfter = true,duration = 0和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);

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

To call for rotation : 要求轮换:

rotateView(imgView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM