2013-07-25 3 views
2

J'ai créé une application, qui utilise InfiniteViewpager par antonyt.Fragment Android Oncreateview Loadanimation

Je veux ajouter une animation sur un Imageview lorsque ma vue est créée, mais mon animation fonctionne plusieurs fois, pas toujours. Voici le code:

public class ColourFragment extends Fragment { 


private int identifier; 
private String logo; 




@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
Bundle args = getArguments(); 

identifier = args.getInt("identifier"); 
logo = args.getString("logo"); 
} 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View V = inflater.inflate(R.layout.radiologok, container, false); 

Le ImageView Je veux animer.

ImageView imageView = (ImageView)V.findViewById(R.id.radiologovalaszto); 


    ProgressBar progressBar = (ProgressBar) V.findViewById(R.id.progressBar1); 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int height = displaymetrics.heightPixels/4 * 3; 
    int width = displaymetrics.widthPixels/4 * 3; 


    imageView.setAdjustViewBounds(true); 
    imageView.getLayoutParams().height = height; 
    imageView.getLayoutParams().width = width; 

Charger la bibliothèque de requêtes Android pour obtenir une image à partir du Web.

AQuery aq = new AQuery(getActivity()); 
    AQUtility.setDebug(true); 
    aq.hardwareAccelerated11(); 

L'image est définie.

aq.id(imageView).progress(progressBar).image(logo, true, true, width, 0); 

Ici, je veux ajouter le Animation

Animation pulse = AnimationUtils.loadAnimation(getActivity(), R.anim.pulse); 
imageView.startAnimation(pulse); 

    Return V; 

}

Voici mon animation:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="false" > 
<scale 
    android:duration="1000" 
    android:fromXScale="1" 
    android:fromYScale="1" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:toXScale="0.5" 
    android:toYScale="0.5" /> 
<scale 
    android:duration="1000" 
    android:fromXScale="1" 
    android:fromYScale="1" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:startOffset="1000" 
    android:toXScale="2" 
    android:toYScale="2" /> 
    </set> 

que je charge la vue en utilisant un fragmentadapter:

final PagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) { 

       @Override 
       public int getCount() { 
       return Radiologos.length; 
       } 

       @Override 
       public Fragment getItem(int position) { 
       Fragment fragment = new ColourFragment(); 
       Bundle args = new Bundle(); 

       args.putString("logo", Radiologos[position]); 
       args.putInt("identifier", position); 
       fragment.setArguments(args); 
       return fragment; 
       } 
      }; 
+0

réutilisez-vous la vue ? Est-ce dans n'importe quelle liste? –

+0

C'est dans un fragment. – user2617730

+0

Mais où montrez-vous ce fragment? dans la liste ou Activité ou Viewpager, et s'il vous plaît poster le code où vous l'utilisez –

Répondre

1

Essayez cette chose, il pourrait fonctionner

1er Créer une classe qui contient certaines variables statiques afin de pouvoir l'utiliser plus tard

StaticVariables.java

public static ArrayList<ImageView> listOfImageViews = new ArrayList<ImageView>(); 

2ème Maintenant sur le onCreateView où vous créez le imageview comme d'habitude et maintenant le passer à la stat variables ic que nous venons de créer

imageView.setAdjustViewBounds(true); 
imageView.getLayoutParams().height = height; 
imageView.getLayoutParams().width = width; 
.... 
StaticVariable.listOfImageViews.add(imageview); 

maintenant sur le téléavertisseur de vue que votre utilisant ajouter un pageListener et sur le onPageSelected ajouter l'animation sur le imageview et démarrer l'animation

mPager.setOnPageChangeListener(new OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int arg0) { 
      Log.d(LOG_TAG, "onPageSelected " + arg0); 
      //Create your animation 
      Animation pulse = AnimationUtils.loadAnimation(getActivity(), R.anim.pulse); 
      //Start your animation 
      StaticVariable.listOfImageViews.get(arg0).startAnimation(pulse); 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) {} 

     @Override 
     public void onPageScrollStateChanged(int arg0) {} 
    }); 
Questions connexes