//利用ViewPager实现画廊效果
//一MainActivity:mainactivity没什么都东西 最主要的就是
//viewPager.setPageTransformer(true, new MyTransformation());
/** * Created by shichao on 2016/10/7. */ public class MovieFragment extends Fragment { private ViewPager viewPager; private List<MovieData> data; private MovieData movieData; private MovieAdapter movieAdapter; private MovieData movieData1; private MovieData movieData2; private MovieData movieData3; private MovieData movieData4; private MovieData movieData5; private MovieData movieData6; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_a, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); viewPager = (ViewPager) view.findViewById(R.id.viewpager); data = new ArrayList<>(); movieData = new MovieData(); movieData1 = new MovieData(); movieData2 = new MovieData(); movieData3 = new MovieData(); movieData4 = new MovieData(); movieData5 = new MovieData(); movieData6 = new MovieData(); movieData.setBitmap(Utils.getReverseBitmap(R.drawable.mainview_huodong, getContext())); movieData1.setBitmap(Utils.getReverseBitmap(R.drawable.mainview_cloudlist, getContext())); movieData2.setBitmap(Utils.getReverseBitmap(R.drawable.mainview_cloudlist, getContext())); movieData3.setBitmap(Utils.getReverseBitmap(R.drawable.mainview_huodong, getContext())); movieData4.setBitmap(Utils.getReverseBitmap(R.drawable.mainview_cloudlist, getContext())); movieData5.setBitmap(Utils.getReverseBitmap(R.drawable.mainview_huodong, getContext())); movieData6.setBitmap(Utils.getReverseBitmap(R.drawable.mainview_cloudlist, getContext())); data.add(movieData); data.add(movieData1); data.add(movieData2); data.add(movieData3); data.add(movieData4); data.add(movieData5); data.add(movieData6); List<ImageView> images = new ArrayList(); for (int i = 0; i < data.size(); i++) { ImageView image = new ImageView(getContext()); image.setImageBitmap(data.get(i).getBitmap()); images.add(image); } movieAdapter = new MovieAdapter(getContext(), data); viewPager.setAdapter(movieAdapter); viewPager.setPageMargin(-1540); viewPager.setOffscreenPageLimit(6); viewPager.setPageTransformer(true, new MyTransformation()); }
}
ViewPager的Adapter
public class MovieAdapter extends PagerAdapter { private Context context; private List<MovieData> data; List<ImageView> imagesList = new ArrayList(); public MovieAdapter(Context context, List<MovieData> data) { this.context = context; this.data = data; for (int i = 0; i < data.size(); i++) { ImageView vi = new ImageView(context); vi.setImageBitmap(data.get(i).getBitmap()); imagesList.add(vi); } } @Override public int getCount() { return data == null ? 0 : data.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imagesList.get(position)); return imagesList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(imagesList.get(position)); } }三:最重要的类public class MyTransformation implements ViewPager.PageTransformer { private static final float MIN_ALPHA = 0.85f;//page:这个参数是没一页 position:注意了这个是浮点型的 position的范围是 (-1,0] 和 0 和 [0,1)
//如果还是不明白position的具体含义 自己去打log @Override public void transformPage(View page, float position) { //设置垂直屏幕的高度 float scaleFactor = Math.max(MIN_ALPHA, 1 - Math.abs(position));//设置角度 float rotate = -150 * Math.abs(position); if (position == 0) { page.setRotationY(0); page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); } else if (position > 0 && position < 1) { if (position > 0.3) { page.setRotationY(0); // page.setScaleX(scaleFactor); // page.setScaleY(scaleFactor); } else { page.setRotationY(-rotate); page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); } } else if (position < 0 && position > -1) { if (position < -0.3) { page.setRotationY(0); // page.setScaleX(scaleFactor); // page.setScaleY(scaleFactor); } else { page.setRotationY(rotate); page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); } } } }//还有一个类ImageUtils负责处理图片的倒影public class Utils { /** * 设置图片倒影 * @param resId * @param context * @return */ public static Bitmap getReverseBitmap(int resId, Context context) { Bitmap sourceBitmap = BitmapFactory.decodeResource(context.getResources(), resId); //绘制原图的下一半图片 Matrix matrix = new Matrix(); //设置倒影,图片倒转 matrix.setScale(1, -1); Bitmap inverseBitmap = Bitmap.createBitmap(sourceBitmap, 0, sourceBitmap.getHeight() / 2, sourceBitmap.getWidth(), sourceBitmap.getHeight() / 3, matrix, false); //合成图片 Bitmap groupbBitmap = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight() + sourceBitmap.getHeight() / 3 + 60, sourceBitmap.getConfig()); //以合成图片为画布 Canvas canvas = new Canvas(groupbBitmap); //将原图和倒影图片画在合成图片上 canvas.drawBitmap(sourceBitmap, 0, 0, null); canvas.drawBitmap(inverseBitmap, 0, sourceBitmap.getHeight() + 50, null); //添加遮罩 Paint paint = new Paint(); Shader.TileMode tileMode = Shader.TileMode.CLAMP; LinearGradient shader = new LinearGradient(0, sourceBitmap.getHeight() + 50, 0, groupbBitmap.getHeight(), Color.BLACK, Color.TRANSPARENT, tileMode); paint.setShader(shader); //这里取矩形渐变区和图片的交集 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawRect(0, sourceBitmap.getHeight() + 50, sourceBitmap.getWidth(), groupbBitmap.getHeight(), paint); return groupbBitmap; } }就完事了...很简单的哦