2010-11-11 16 views
20

मैं http://developer.android.com/guide/topics/resources/animation-resource.html ("एनीमेशन रिसोर्सेज") पर वर्णित "हाइपरस्पेस" ट्विन एनीमेशन को लागू करने की कोशिश कर रहा हूं - हालांकि यह लिखित के रूप में काम नहीं करता है। जब मैं एप्लिकेशन चलाता हूं, तो मुझे एप्लिकेशन टाइटल बार के नीचे एक खाली दृश्य मिलता है। मैं क्या गलत कर रहा हूं?सरल ट्विन एनीमेशन उदाहरण

उदाहरण के अनुसार, मेरा कोड यहां है। मेरे द्वारा बनाए गए रेस/anim/hyperspace_jump.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <scale 
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
     android:fromXScale="1.0" 
     android:toXScale="1.4" 
     android:fromYScale="1.0" 
     android:toYScale="0.6" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:fillAfter="false" 
     android:duration="700" /> 
    <set 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:startOffset="700"> 
     <scale 
      android:fromXScale="1.4" 
      android:toXScale="0.0" 
      android:fromYScale="0.6" 
      android:toYScale="0.0" 
      android:pivotX="50%" 
      android:pivotY="50%" 
      android:duration="400" /> 
     <rotate 
      android:fromDegrees="0" 
      android:toDegrees="-45" 
      android:toYScale="0.0" 
      android:pivotX="50%" 
      android:pivotY="50%" 
      android:duration="400" /> 
    </set> 
</set> 

मैं भी एक लेआउट/main.xml बना लिया है:

package com.tomoreilly.geology; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView image = (ImageView) findViewById(R.id.ImageView01); 
     Animation hyperspaceJump = 
      AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); 
     image.startAnimation(hyperspaceJump); 
    } 
} 
:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 

</LinearLayout> 

अंत में मैं एक गतिविधि है

फिर भी जब मैं ऐप चलाता हूं तो मुझे कोई एनीमेशन दिखाई नहीं देता है। क्या मुझे कुछ विवरण याद आ रहा है जो "एनिमेशन संसाधन" उदाहरण में शामिल नहीं है?

धन्यवाद, टॉम

उत्तर

-1

मैं लगता ViewFlipper की आवश्यकता है। क्योंकि जब आप एनीमेशन के बीच एनीमेशन करना चाहते हैं तो आपको एनीमेशन करने के लिए ViewFlipper की आवश्यकता होती है।

निम्नलिखित बातें करें। मुझे लगता है कि समस्या हल करें।

नई एक्सएमएल फ़ाइल बनाएं और उसे में ViewFlipper टैब जोड़ने ...

उपयोग अन्य सभी लेआउट शामिल करने के लिए टैब शामिल है जब आप एनीमेशन करना चाहते हैं।

और की तुलना में निम्न कोड

flipper = (ViewFlipper) findViewById(R.id.flipper); 

Button button1 = (Button) findViewById(R.id.Button01); // Button in one activity 

Button button2 = (Button) findViewById(R.id.Button02); // Button in second activity 

// Other Methods 

private Animation inFromRightAnimation() { 

     // Animation inFromRight = new TranslateAnimation(
     /* 
     * Animation inFromRight = new ScaleAnimation(
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 

     inFromRight.setDuration(500); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
    } 

    private Animation outToLeftAnimation() { 
     // Animation outtoLeft = new TranslateAnimation(
     /* 
     * Animation outtoLeft = new 
     * ScaleAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation outtoLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoLeft.setDuration(500); 
     outtoLeft.setInterpolator(new AccelerateInterpolator()); 
     return outtoLeft; 
    } 

    private Animation inFromLeftAnimation() { 
     // Animation inFromLeft = new TranslateAnimation(
     /* 
     * Animation inFromLeft = new 
     * ScaleAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation inFromLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     inFromLeft.setDuration(500); 
     inFromLeft.setInterpolator(new AccelerateInterpolator()); 
     return inFromLeft; 
    } 

    private Animation outToRightAnimation() { 
     // Animation outtoRight = new TranslateAnimation(
     /* 
     * Animation outtoRight = new 
     * ScaleAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation outtoRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoRight.setDuration(500); 
     outtoRight.setInterpolator(new AccelerateInterpolator()); 
     return outtoRight; 
    } 
+0

तुम यहाँ क्यों जवाब ऊपर पेस्ट कॉपी? – Sameer

5

आपका imageView एक स्रोत या तो xml या अपनी गतिविधि में परिभाषित किया गया है करने के लिए है का उपयोग करें।

xml:

<ImageView android:id="@+id/ImageView01" 
    android:src="@drawable/someimage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</ImageView> 

गतिविधि:

ImageView image = (ImageView) findViewById(R.id.ImageView01); 
image.setImageResource(R.drawable.some_image); 
-1
package com.example; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.view.animation.TranslateAnimation; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.ViewFlipper; 

public class TeeenAni extends Activity { 

ViewFlipper flipper; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ImageView image = (ImageView) findViewById(R.id.ImageView01); 
    Animation hyperspaceJump = 
    AnimationUtils.loadAnimation(this, R.anim.); 
    image.startAnimation(hyperspaceJump); 
    flipper = (ViewFlipper) findViewById(R.anim.hyperspace_jump); 

    Button button1 = (Button) findViewById(R.id.Button01); 

    Button button2 = (Button) findViewById(R.id.Button02); 
} 
    private Animation inFromRightAnimation() { 


     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 

     inFromRight.setDuration(500); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
    } 

    private Animation outToLeftAnimation() { 

     Animation outtoLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoLeft.setDuration(500); 
     outtoLeft.setInterpolator(new AccelerateInterpolator()); 
     return outtoLeft; 
    } 

    private Animation inFromLeftAnimation() { 

     Animation inFromLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     inFromLeft.setDuration(500); 
     inFromLeft.setInterpolator(new AccelerateInterpolator()); 
     return inFromLeft; 
    } 

    private Animation outToRightAnimation() { 

     Animation outtoRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoRight.setDuration(500); 
     outtoRight.setInterpolator(new AccelerateInterpolator()); 
     return outtoRight; 
    } 

[R.anim.hyperspace_jump] [1] }

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
/> 
<ImageView android:id="@+id/ImageView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
</ImageView> 

<ViewFlipper android:id="@+id/details" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 

<Button android:id="@+id/Button01" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="Home"></Button> 
<Button android:id="@+id/Button02" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="Gallary"></Button> 

+4

कृपया थोड़ी सी कोड की तुलना में अपने उत्तर में कुछ और पदार्थ जोड़ें। इस कोड को हल करने के तरीके के बारे में कुछ तर्क जोड़ें। – Matsemann

11

enter image description here

एक अलग जवाब जोड़ने के लिए, आप भी अपने Android UI के चेतन करने के लिए Universal Tween Engine की कोशिश कर सकते। दरअसल, अपने एनीमेशन है, जो अपनी XML स्वरूप में काफी कुछ लाइनों की आवश्यकता है, इस तरह वर्णित किया जाएगा:

Timeline.createSequence() 
    // First, set your pivot (Tween.set() works instantly) 
    .push(Tween.set(image, ViewAccessor.PIVOT_XY).target(0.5f, 0.5f)) 

    // Then, animate your scale and rotation as you want 
    .push(Tween.to(image, ViewAccessor.SCALE_XY, 0.7f).target(1.4f, 0.6f)) 
    .beginParallel() 
     .push(Tween.to(image, ViewAccessor.SCALE_XY, 0.4f).target(0, 0)) 
     .push(Tween.to(image, ViewAccessor.ROTATION, 0.4f).target(-45)) 
    .end() 

    // Finally, start the animation! 
    .start(); 

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

यह पूरी तरह से खुला स्रोत है, अत्यधिक दस्तावेज है, और अपाचे -2 लाइसेंस के साथ जारी किया गया है।

आप Android demo एक कोशिश दे सकते हैं अगर आप चाहते हैं :)

+0

वैसे यह एक अद्भुत पुस्तकालय है जो इस दिन खूबसूरती से काम करता है। – alvi

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