2015-08-12 5 views
1

Я работаю с обоими ролями ImageView. Когда я запускаю Project, анимация в onCreate() работает нормально, но когда я пытаюсь запустить анимацию на кнопке Click, она не работает.Android rotate imageview animation issue

Как это исправить?

XML код

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<EditText 
    android:id="@+id/getAngle" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:inputType="number" /> 

<ImageView 
    android:id="@+id/rotateImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:src="@drawable/spinner_new" /> 

<Button 
    android:id="@+id/startbutton" 
    android:layout_width="200dp" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="Start" /> 

Java Class Code

public class MainActivity extends Activity { 

EditText getAngle; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    getAngle = (EditText) findViewById(R.id.getAngle); 
    Button startbutton = (Button) findViewById(R.id.startbutton); 
    startbutton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      String endPointString = getAngle.getText().toString(); 
      int endPointInt = Integer.parseInt(endPointString); 
      ImageView rotateImage = (ImageView) findViewById(R.id.rotateImage); 
      Animation rotateanimation = new RotateAnimation(0, endPointInt, 
        Animation.RELATIVE_TO_SELF, 0.5f, 
        Animation.RELATIVE_TO_SELF, 0.5f); 
      rotateanimation.setDuration(1000); 
      rotateanimation.setRepeatCount(0); 
      rotateanimation.setRepeatMode(Animation.REVERSE); 
      rotateanimation.setFillAfter(true); 
      rotateImage.setAnimation(rotateanimation); 
     } 
    }); 

} 

} 

ответ

2

Выполните следующие действия, и он будет работать определенно 1) дают Id вашему XML RelativeLayout (родительский макет) [ я дал только относительный] 2) Сделайте код ниже:

public class MainActivity extends Activity { 

EditText getAngle; 
ImageView rotateImage; 
RelativeLayout relative; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 


getAngle = (EditText) findViewById(R.id.getAngle); 
rotateImage = (ImageView) findViewById(R.id.rotateImage); 
    relative = (RelativeLayout)findViewById(R.id.relative); 
Button startbutton = (Button) findViewById(R.id.startbutton); 
startbutton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     String endPointString = getAngle.getText().toString(); 
     int endPointInt = Integer.parseInt(endPointString); 

     Animation rotateanimation = new RotateAnimation(0, endPointInt, 
       Animation.RELATIVE_TO_SELF, 0.5f, 
       Animation.RELATIVE_TO_SELF, 0.5f); 
     rotateanimation.setDuration(1000); 
     rotateanimation.setRepeatCount(0); 
     rotateanimation.setRepeatMode(Animation.REVERSE); 
     rotateanimation.setFillAfter(true); 
     rotateImage.setAnimation(rotateanimation); 
     rotateanimation.start(); 
     relative.invalidate(); 

    } 
}); 

} 

} 
+0

его не работает @Shivani – user123456

+0

сделайте это сейчас @ user123456 –

+0

u отлично :) Спасибо :) – user123456