如何在ViewPager指标之间添加动画效果? [等候接听]

我使用ViewPager.OnPageChangeListener接口创建了一个Onboarding Screen,使用ViewPager和FragmentPagerAdapter以及附加的指示器 . 以下是我的代码:

public class MainActivity extends AppCompatActivity {

private ViewPager myviewpager;
private View indicator1;
private View indicator2;
private View indicator3;
private OnboardingAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myviewpager = (ViewPager) findViewById(R.id.viewpager);
    indicator1 = (View) findViewById(R.id.indicator1);
    indicator2 = (View) findViewById(R.id.indicator2);
    indicator3 = (View) findViewById(R.id.indicator3);
    myviewpager.addOnPageChangeListener(new PagerListener());
    adapter = new OnboardingAdapter(getSupportFragmentManager());
    myviewpager.setAdapter(adapter);

}

private class OnboardingAdapter extends FragmentPagerAdapter{

    public OnboardingAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int i) {

        switch (i){
            case 0:
                Fragment1 tab1 = new Fragment1();
                return tab1;

            case 1:
                Fragment2 tab2 = new Fragment2();
                return tab2;

            case 2:
                Fragment3 tab3 = new Fragment3();
                return tab3;

        }

        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

public void updateIndicators(int position) {
    switch (position) {
        case 0:
            indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot));
            indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            break;
        case 1:
            indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot));
            indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            break;
        case 2:
            indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot));
            break;
        case 3:
            indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
            break;
    }
}

private class PagerListener extends ViewPager.SimpleOnPageChangeListener{
    public PagerListener() {
        super();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        Log.d("ViewPagerListner", "onPageScrolled: " +"Position = "+String.valueOf(position) + " PositionOffset = "+String.valueOf(positionOffset)+" PositionOffsetPixels = "+String.valueOf(positionOffsetPixels));

    }

    @Override
    public void onPageSelected(int position) {
        super.onPageSelected(position);
        Log.d("ViewPagerListner", "onPageSelected: " + String.valueOf(position));
        updateIndicators(position);

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        super.onPageScrollStateChanged(state);
        Log.d("ViewPagerListner", "onPageScrollStateChanged: " + String.valueOf(state));
    }
}

我想要实现的是在PageChange时为指标添加动画 . 我希望在指标之间实现以下类型的过渡

Normal State:-

Screen Shot

Animation State while changing Screen:-

Screen Shot

我不明白他们如何合并2个指标drawables并完成它们之间的转换

回答(0)