2016-09-30 87 views
0

我試圖使用Drawable Animation在我的一個Viewpager中。但是當我嘗試使用getDrawable()方法時,它給了我空指針異常。我的ViewPager有4頁。我認爲這是因爲我的XML文件是在Viewpager中創建的,我無法通過此方法訪問它們。我已經在我的Viewpager中設置了一個ImageView的源代碼,但它仍然無效。請看看我的代碼並幫助我。如何在使用ViewPager時使用getDrawable()

我的主要活動:

import android.graphics.drawable.AnimationDrawable; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.ImageView; 

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    setContentView(R.layout.activity_main); 

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 
    viewPager.setAdapter(new CustomPagerAdapter(this)); 

    ImageView myAnimation = (ImageView)findViewById(R.id.imageView2); 
    final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable(); 
    myAnimationDrawable.start(); 
    } 
} 

我ViewPager適配器:

import android.content.Context; 
import android.graphics.drawable.AnimationDrawable; 
import android.graphics.drawable.Drawable; 
import android.support.v4.view.PagerAdapter; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class CustomPagerAdapter extends PagerAdapter { 
    private Context mContext; 

    public CustomPagerAdapter(Context context) { 
    mContext = context; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup collection, int position) { 
    ModelObject modelObject = ModelObject.values()[position]; 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false); 
    collection.addView(layout); 
    return layout; 
    } 

    @Override 
    public void destroyItem(ViewGroup collection, int position, Object view) { 
    collection.removeView((View) view); 
    } 

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

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
    return view == object; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
    ModelObject customPagerEnum = ModelObject.values()[position]; 
    return mContext.getString(customPagerEnum.getTitleResId()); 
    } 
} 

enum添加新的頁面:

public enum ModelObject { 
    RED(R.string.wp1, R.layout.wp1), 
    BLUE(R.string.wp2, R.layout.wp2), 
    LOL(R.string.wp3, R.layout.wp3), 
    WP(R.string.wp5,R.layout.wp4), 
    GREEN(R.string.wp4, R.layout.wp4); 

    private int mTitleResId; 
    private int mLayoutResId; 

    ModelObject(int titleResId, int layoutResId) { 
    mTitleResId = titleResId; 
    mLayoutResId = layoutResId; 
    } 

    public int getTitleResId() { 
    return mTitleResId; 
    } 

    public int getLayoutResId() { 
    return mLayoutResId; 
    } 
} 

如果可以,請幫助我。
對不起英語感到抱歉。

回答

0
@Override 
     public void onPageSelected(int pos) { 


      //find your layout in with the position & then implement 
      ImageView myAnimation = (ImageView)findViewById(R.id.imageView2); // this imageview for particular viewpager page 
      final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable(); 
      myAnimationDrawable.start(); 


     }