3

I'm new in Android and I have a little problem. I found this code for RotateAnimation:

xml file where are stored all data of RotateAnimation:

<?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 file:

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

}

How can I create a variable of this two values that are inside xml file?

    android:fromDegrees="0"
    android:toDegrees="360"
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
user52252
  • 43
  • 1
  • 5

5 Answers5

3

As per RotateAnimation class reference (http://developer.android.com/reference/android/view/animation/RotateAnimation.html), this class does not provide setter methods for fromDegrees and toDegrees. So, if you need to set these values in code, you will have to create the RotateAnimation object in code and pass fromDegrees and toDegrees values into the constructor.

RotateAnimation rotateAnimation = new RotateAnimation(fromDegrees, toDegrees);
Sameer
  • 4,379
  • 1
  • 23
  • 23
  • 1
    Got the idea. How do I implement that on final Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator); ? – Machado Oct 28 '14 at 12:40
0

try the following code, it may work:

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

main code:

  Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
    myView.startAnimation(rotation);
G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
  • I think that you haven't understand my question... I want put fromDegree and toDegree values in a variable... Thanks anyway – user52252 Oct 05 '12 at 08:08
0

try the another one:

     Matrix matrix=new Matrix();
 imageView.setScaleType(ScaleType.MATRIX);   //required
 matrix.postRotate((float) angle, pivX, pivY);
 imagView.setImageMatrix(matrix);
G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
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
    While this code may answer the question, providing additional context regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. – Rüdiger Herrmann Nov 14 '15 at 12:24
0

Use an xml file with pre-defined integers in the values folder. Android says the name of the file doesn't matter, but you may as well put it in a file called res/values/integers.xml. More on this type of xml file here. Basically, place your degree values as integers in integers.xml, use them in your rotation xml code, and retrieve them in your activity code using getResources().getInteger(). Here's the example code:

In integers.xml:

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

In rotate xml:

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

And in java code:

int fromDegrees = getResources().getInteger(R.integer.from_degrees);
int toDegrees = getResources().getInteger(R.integer.to_degrees);
Daniel Handojo
  • 612
  • 5
  • 19