Created
March 24, 2016 05:34
-
-
Save shau-lok/e04a7671a4010cc12d87 to your computer and use it in GitHub Desktop.
Yahoo News Digest ViewPager animation
This file contains hidden or 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
import android.support.v4.view.ViewPager; | |
public class MainActivity extends AppCompatActivity { | |
TextView tv; | |
ViewPager pager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
tv = (TextView) findViewById(R.id.tv); | |
pager = (ViewPager) findViewById(R.id.pager); | |
pager.setAdapter(new ParallaxPagerAdapter(getApplicationContext())); | |
pager.setPageMargin(20); | |
pager.setPageMarginDrawable(R.color.black); | |
// using tag find view | |
// pager.setPageTransformer(true, new ParallaxViewTransformer(ParallaxViewTransformer.IMAGE_TAG)); | |
// using id find view | |
pager.setPageTransformer(true, new ParallaxViewTransformer(R.id.imageview)); | |
} | |
} |
This file contains hidden or 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 ParallaxPagerAdapter extends PagerAdapter { | |
Context context; | |
static int[] imgRes = {R.mipmap.m1, R.mipmap.m2, R.mipmap.m3, R.mipmap.m4}; | |
public ParallaxPagerAdapter(Context context) { | |
this.context = context; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
container.removeView((View) object); | |
} | |
@Override | |
public int getCount() { | |
return imgRes.length; | |
} | |
@Override | |
public boolean isViewFromObject(View view, Object object) { | |
return view ==object; | |
} | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
LayoutInflater inflater = LayoutInflater.from(context); | |
View views = inflater.inflate(R.layout.pager_item, container,false); | |
ImageView image = (ImageView) views.findViewById(R.id.imageview); | |
image.setImageResource(imgRes[position]); | |
// if using tag find view | |
image.setTag(ParallaxViewTransformer.IMAGE_TAG); | |
container.addView(views); | |
return views; | |
} | |
} |
This file contains hidden or 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
import android.support.v4.view.ViewPager; | |
import android.text.TextUtils; | |
import android.view.View; | |
import com.nineoldandroids.animation.ObjectAnimator; | |
/** | |
* Created by shau-lok on 3/23/16. | |
*/ | |
public class ParallaxViewTransformer implements ViewPager.PageTransformer { | |
int mImageId = -1; | |
public static final String IMAGE_TAG = "parallaxViewTag"; | |
String imgTag = null; | |
@Override | |
public void transformPage(View page, float position) { | |
int pageWidth = page.getWidth(); | |
if (mImageId > 0) { | |
if (page.findViewById(mImageId) != null) { | |
page = page.findViewById(mImageId); | |
} | |
if (position <= 0) { //[-1,0] | |
if (page.getId() == mImageId) { | |
page.setTranslationX(pageWidth * -position / 1.4f); | |
} else { | |
page.setTranslationX(0); | |
} | |
} else {//(0,1] | |
page.setTranslationX(pageWidth * -position / 1.4f); | |
} | |
} | |
if (!TextUtils.isEmpty(imgTag)) { | |
if (page.findViewWithTag(imgTag) != null) { | |
page = page.findViewWithTag(imgTag); | |
} | |
if (position <= 0) { | |
if (page.getTag() == imgTag) { | |
// page.setTranslationX(pageWidth * -position / 1.4f); | |
ObjectAnimator.ofFloat(page,"translationX",pageWidth * -position / 1.4f).start(); | |
} else { | |
// page.setTranslationX(0); | |
ObjectAnimator.ofFloat(page,"translationX",0).start(); | |
} | |
} else { | |
// page.setTranslationX(pageWidth * -position / 1.4f); | |
ObjectAnimator.ofFloat(page,"translationX",pageWidth * -position / 1.4f).start(); | |
} | |
} | |
} | |
public ParallaxViewTransformer(int mImageId) { | |
this.mImageId = mImageId; | |
} | |
public ParallaxViewTransformer(String imgTag) { | |
this.imgTag = imgTag; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment