Custom animation to view

I am working with viewPager and fragments, and now I am trying to get a custom animation, transforming the zoom page accurately.

But I get a few errors. I will put errors in the comment using a code snippet.

class MainActivity

import android.support.v4.app.FragmentManager; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.view.Menu; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);/** Getting a reference to the ViewPager defined the layout file */ ViewPager pager = (ViewPager) findViewById(R.id.pager); /** Getting fragment manager */ FragmentManager fm = getSupportFragmentManager(); /** Instantiating FragmentPagerAdapter */ MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm); /** Setting the pagerAdapter to the pager object */ pager.setAdapter(pagerAdapter); // **ERROR:The method setPageTransformer(boolean, ViewPager.PageTransformer) in the type ViewPager is not applicable for the arguments (boolean, ZoomOutPageTransformer)** pager.setPageTransformer(true, new ZoomOutPageTransformer()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } 

Class ZoomOutPageTransformer

 import android.view.View; // **ERROR on 'ViewPager.PageTransformer --> ViewPager cannot be resolved to a type ** public class ZoomOutPageTransformer implements ViewPager.PageTransformer { private static float MIN_SCALE = 0.85f; private static float MIN_ALPHA = 0.5f; public void transformPage(View view, float position) { int pageWidth = view.getWidth(); int pageHeight = view.getHeight(); if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. view.setAlpha(0); } else if (position <= 1) { // [-1,1] // Modify the default slide transition to shrink the page as well float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); float vertMargin = pageHeight * (1 - scaleFactor) / 2; float horzMargin = pageWidth * (1 - scaleFactor) / 2; if (position < 0) { view.setTranslationX(horzMargin - vertMargin / 2); } else { view.setTranslationX(-horzMargin + vertMargin / 2); } // Scale the page down (between MIN_SCALE and 1) view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); // Fade the page relative to its size. view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA)); } else { // (1,+Infinity] // This page is way off-screen to the right. view.setAlpha(0); } } } 
+6
source share
5 answers

I solved the problem:

  • Download the latest Android SDK
  • Find android-sdk-windows \ extras \ android \ support \ v4
  • Copy android-support-v4.jar to the folder with your files in your project
  • Clean up project
  • Run your code

This worked for me, loading the support library via ecplise gave me these errors. This fixed the problem for me

+6
source

@mXX is almost right. But for those who still have problems (like me), I need to add an item to his procedure.

You need to update the Android Support. But if you have it now, you need to uninstall it first and then install it again to get a new one.

If you have IntellijIdea:

  • Open the SDK Manager window from the Tools / Android Manager / SDK
  • Find the Exteras folder and check out the Android support library. If its status is set, click "Delete 1 package ..." to remove it.
  • Check this package and click the install button.
  • Go to the \ extras \ android \ support \ v4 folder, then copy android-support-v4.jar and paste it into the Libs folder.
  • The idea is to automatically recompile the project, however, if it didn’t do that then you must recompile it manually by clicking on "Build / rebuild project"

What is it;)

+2
source

For those who still adhered to this, the current v4 support issue does not import the v4 ViewPager interface: it does not require a static instance of the ViewPager class. Just implement PageTransformer. The PageTransformer interface is already enclosed internally with the ViewPager.

 public class ZoomOutPageTransformer implements PageTransformer { private static float MIN_SCALE = 0.85f; private static float MIN_ALPHA = 0.5f; public void transformPage(View view, float position) { int pageWidth = view.getWidth(); int pageHeight = view.getHeight(); if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. view.setAlpha(0); } else if (position <= 1) { // [-1,1] // Modify the default slide transition to shrink the page as well float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); float vertMargin = pageHeight * (1 - scaleFactor) / 2; float horzMargin = pageWidth * (1 - scaleFactor) / 2; if (position < 0) { view.setTranslationX(horzMargin - vertMargin / 2); } else { view.setTranslationX(-horzMargin + vertMargin / 2); } // Scale the page down (between MIN_SCALE and 1) view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); // Fade the page relative to its size. view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA)); } else { // (1,+Infinity] // This page is way off-screen to the right. view.setAlpha(0); } } } 
+1
source

After updating the v4 support library for Android support, you can remove the project from eclipse. And then import the project again. Make a clean assembly, everything will be fine!

0
source

Just add compile 'com.android.support:support-v13:23.1.1' depending on build.gradle ( Screenshot )

0
source