2012-10-14 15 views
17

घुमाएं मैं घूर्णन छवि एनीमेशन करने की कोशिश कर रहा हूं। मुझे के आसपास एक आइकन को घुमाए जाने की आवश्यकता है जैसे कि वे प्रोग्रेसबार में करते हैं, लेकिन मुझे जो मिल रहा है वह एक सर्कल के चारों ओर घूर्णन वाली छवि है। यहाँ मेरी एनीमेशन कोड हैएनीमेशन एंड्रॉइड

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:fromDegrees="0" 
android:toDegrees="360" 
android:interpolator="@android:anim/linear_interpolator" 
android:pivotX="50%" 
android:pivotY="50%" 
android:duration="2500" 
android:repeatCount="infinite" 
android:repeatMode="restart" 
/> 

कहाँ मैं यहाँ में गलत हो रहा हूँ?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/testButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="animationText" 
     android:onClick="AnimClick"/> 

    <ImageView 
     android:id="@+id/testImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:contentDescription="image_desc" 
     android:scaleType="fitCenter" 
     android:src="@drawable/cat2" /> 

</RelativeLayout> 

रोटेशन एनीमेशन को लागू करने के लिए, हम XML या जावा कोड द्वारा एनीमेशन को परिभाषित कर सकते हैं: धन्यवाद

उत्तर

5

खैर मुझे मिल गया है, जहां मैं गलत था। मैंने अपनी छवि को पैडिंग दिया जिसने इसे हर बार घूमने के लिए थोड़ा अलग करने के लिए प्रेरित किया। किसी भी तरह मैंने पैडिंग को हटा दिया और अब यह ठीक काम कर रहा है।

59

यह लेआउट main.xml के लिए स्रोत कोड है। अगर हम एक्सएमएल में एनीमेशन लिखना चाहते हैं, तो हमें एनीमेशन एक्सएमएल फाइल को/res/anim फ़ोल्डर के तहत बनाना होगा। यहाँ, हम rotate_around_center_point.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false" > 

    <rotate 
     android:duration="2500" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:repeatCount="infinite" 
     android:repeatMode="restart" 
     android:toDegrees="360" /> 

</set> 

नाम के एक xml फ़ाइल बना सकते हैं और यह मेरा गतिविधि है:

public class MainActivity extends Activity implements OnClickListener { 

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

     Button btn = (Button) findViewById(R.id.testButton); 
     btn.setOnClickListener(this); 

    } 

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

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage); 

     Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point); 
     animationTarget.startAnimation(animation); 

    } 


} 
+0

धन्यवाद। मैंने छवि में स्केलटाइप जोड़ने की कोशिश की है और फिर भी इससे मदद नहीं मिली है। – orelzion

+0

धन्यवाद, यह काम करता है! – validcat

+1

यह स्वीकार्य उत्तर होना चाहिए था! – Machado

27

आप बल्कि XML में यह कर की तुलना में, निम्न कोड के साथ की कोशिश कर सकते:

RotateAnimation rotate = new RotateAnimation(0, 360, 
     Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 
     0.5f); 

rotate.setDuration(4000); 
rotate.setRepeatCount(Animation.INFINITE); 
yourView.setAnimation(rotate); 
+0

क्या आप जानते हैं कि लूप पूर्ण होने के बाद विराम (स्टॉल) से कैसे बचें और इसे दोहराने से पहले? – portfoliobuilder

+6

rotate.setInterpolator (नया LinearInterpolator()) जोड़ने का प्रयास करें; –

+0

धन्यवाद आदमी, यह काम किया! – Recomer

0

एनीमेशन फ़ोल्डर में निम्नलिखित एक्सएमएल जोड़े

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false" 
    android:duration="4000" 
    android:fromdegrees="0" 
    android:pivotx="50%" 
    android:pivoty="50%" 
    android:todegrees="360" 
    android:toyscale="0.0"> 
</set> 

आशा इस तुम्हारी मदद करेगा ..

अधिक जानकारी के लिए

http://www.blazin.in/2013/09/simple-rotate-animation-in-android.html

संबंधित मुद्दे