安卓 - 旋转动画 - fromDegrees 和 toDegrees 变量

3

我是 Android 的新手,有一个小问题。 我找到了这段 RotateAnimation 的代码:

所有 RotateAnimation 数据都存储在 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="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="20000"
        android:startOffset="0"/>
</set>

Java文件:

package com.example.helloword;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class Rotation_test extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotation_test);
//        getActionBar().setDisplayHomeAsUpEnabled(true);


        Button buttonRotateCenter = (Button) findViewById(R.id.rotatecenter);
        final ImageView floatingImage = (ImageView) findViewById(R.id.floatingimage);


        final Animation animationRotateCenter = AnimationUtils.loadAnimation(
                this, R.anim.rotate_center);
        buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                floatingImage.startAnimation(animationRotateCenter);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_rotation_test, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

我该如何创建一个包含在xml文件中的这两个值的变量?
    android:fromDegrees="0"
    android:toDegrees="360"
5个回答

3
根据RotateAnimation类的参考文档(http://developer.android.com/reference/android/view/animation/RotateAnimation.html),该类并没有为fromDegrees和toDegrees提供setter方法。因此,如果您需要在代码中设置这些值,您将需要在代码中创建RotateAnimation对象,并将fromDegrees和toDegrees的值传入构造函数中。
RotateAnimation rotateAnimation = new RotateAnimation(fromDegrees, toDegrees);

1
明白了。我如何将这个想法实现在最终的动画中呢?myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator);? - Machado

0

使用预定义整数的xml文件存储在values文件夹中。Android声称文件名不重要,但最好将其命名为res/values/integers.xml。关于这种类型的xml文件可以在这里找到更多信息。简单来说,在integers.xml中将您的度数值作为整数放置,在您的旋转xml代码中使用它们,并使用getResources().getInteger()在活动代码中检索它们。以下是示例代码:

integers.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="from_degrees">0</integer>
    <integer name="to_degrees">360</integer>
</resources>

在旋转的 XML 中:

...
android:fromDegrees="@integer/from_degrees"
android:toDegrees="@integer/to_degrees"
...

而在Java代码中:

int fromDegrees = getResources().getInteger(R.integer.from_degrees);
int toDegrees = getResources().getInteger(R.integer.to_degrees);

0
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="0"

        >
  <shape 
    android:shape="ring"
    android:innerRadiusRatio="2"
    android:thicknessRatio="10"
    android:useLevel="false">
    <shape
      android:width="76dip"
      android:height="76dip"/>
    <gradient android:type="sweep"
              android:useLevel="false"
              android:startColor="#F57847"
              android:endColor="#E67E55"
              android:angle="0"/>    
  </shape>



</rotate>**strong text**

3
虽然这段代码可能回答了问题,但是提供有关它如何以及为什么解决问题的额外上下文,可以提高答案的长期价值。 - Rüdiger Herrmann

0

尝试以下代码,可能会奏效:

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

主要代码:

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

我认为你没有理解我的问题... 我想把fromDegree和toDegree的值放在一个变量中... 无论如何,谢谢。 - user52252

0

请尝试另一个:

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

谢谢,但我宁愿使用“我的”代码,如果可能的话将值放入变量中...无论如何还是谢谢。 - user52252

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接