Created
November 20, 2014 20:32
-
-
Save rajivnarayana/46611e740613c8d7208e to your computer and use it in GitHub Desktop.
An android activity that sets up a view page that changes background color as you scroll. As seen in https://play.google.com/store/apps/details?id=ch.bitspin.timely
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends ActionBarActivity { | |
private ViewPager mViewPager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final int[] mColors = new int[3]; | |
mColors[0] = Color.BLUE; | |
mColors[1] = Color.RED; | |
mColors[2] = Color.GREEN; | |
mViewPager = new ViewPager(this); | |
mViewPager.setOffscreenPageLimit(mColors.length/2); | |
mViewPager.setAdapter(new PagerAdapter() { | |
@Override | |
public int getCount() { | |
return mColors.length; | |
} | |
@Override | |
public boolean isViewFromObject(View view, Object o) { | |
return view == o; | |
} | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
TextView view = new TextView(MainActivity.this); | |
view.setText("Page position: "+position); | |
view.setBackgroundColor(Color.TRANSPARENT); | |
container.addView(view); | |
return view; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
View view = (View) object; | |
container.removeView(view); | |
} | |
}); | |
setContentView(mViewPager); | |
mViewPager.setBackgroundColor(mColors[mViewPager.getCurrentItem()]); | |
final ArgbEvaluator argbEvaluator = new ArgbEvaluator(); | |
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
if (positionOffset < 1.f && positionOffset > 0) { | |
mViewPager.setBackgroundColor((Integer) (argbEvaluator.evaluate(positionOffset, mColors[position], mColors[position + 1]))); | |
} | |
} | |
@Override | |
public void onPageSelected(int position) { | |
mViewPager.setBackgroundColor(mColors[position]); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment