通过给RecycleView设置滚动监听,在 onScrolled 中完成功能。

具体的算法是:

1.规定缩放比例的最大值和最小值 MAX_SCALE(eg: 1.2f) 和 MIN_SCALE(eg:0.85f)

2.当滑动的时候,计算recycleview的所有可用的子view的左边界(left = View.getLeft())和右边界(left = View.getRight())的距离比例 percent = left <0|| right <0?0: Math.min(left, right) *1f/ Math.max(left, right);

3.计算每个子view的缩放比例:floatscaleFactor =MIN_SCALE+ Math.abs(percent) * (MAX_SCALE-MIN_SCALE);

child.setScaleY(scaleFactor);

具体代码如下:

ScrollingActivity.class

public class ScrollingActivity extends AppCompatActivity {private RecyclerView mRecycleView;private RecyclerView.Adapter mAdapter;private int mScreenWidth;private static final float MIN_SCALE = .95f;private static final float MAX_SCALE = 1.15f;private LinearLayoutManager mLinearLayoutManager;private int mMinWidth;private int mMaxWidth;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_scrolling);mRecycleView = (RecyclerView) findViewById(R.id.rv);mLinearLayoutManager = new LinearLayoutManager(this);mLinearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);mRecycleView.setLayoutManager(mLinearLayoutManager);mScreenWidth = getResources().getDisplayMetrics().widthPixels;mMinWidth = (int) (mScreenWidth * 0.28f);mMaxWidth = mScreenWidth - 2 * mMinWidth;String[] names = new String[100];for (int i = 0; i < names.length; i++) {names[i] = "name -" + (i + 1);}mAdapter = new MyAdapter(names);mRecycleView.setAdapter(mAdapter);mRecycleView.addOnScrollListener(mOnScrollListener);}private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {@Overridepublic void onScrolled(RecyclerView recyclerView, int dx, int dy) {final int childCount = recyclerView.getChildCount();Log.e("tag", childCount + "");for (int i = 0; i < childCount; i++) {RelativeLayout child = (RelativeLayout) recyclerView.getChildAt(i);RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();lp.rightMargin = 5;lp.height = 200;int left = child.getLeft();int right = mScreenWidth - child.getRight();final float percent = left < 0 || right < 0 ? 0 : Math.min(left, right) * 1f / Math.max(left, right);Log.e("tag", "percent = " + percent);float scaleFactor = MIN_SCALE + Math.abs(percent) * (MAX_SCALE - MIN_SCALE);int width = (int) (mMinWidth + Math.abs(percent) * (mMaxWidth - mMinWidth));lp.width = width;child.setLayoutParams(lp);child.setScaleY(scaleFactor);if (percent > 1f / 3) {((TextView) child.getChildAt(1)).setTextColor(Color.BLUE);} else {((TextView) child.getChildAt(1)).setTextColor(Color.BLACK);}}}};private class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {private String[] mDatas;public MyAdapter(String[] mDatas) {this.mDatas = mDatas;}@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(ScrollingActivity.this).inflate(R.layout.item_list, null);return new RecyclerView.ViewHolder(view) {};}@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {Log.e("tuanhui", "bind view");ImageView iv = (ImageView) holder.itemView.findViewById(R.id.iv_pic);TextView tv = (TextView) holder.itemView.findViewById(R.id.tv_title);iv.setImageResource(R.drawable.pic);tv.setText(mDatas[position]);}@Overridepublic int getItemCount() {return mDatas.length;}}
}
activity_scrolling.xml


具体实现就这样了
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:clipChildren="false"android:orientation="vertical"><ImageViewandroid:id="@+id/iv_pic"android:layout_width="200dp"android:layout_height="150dp"android:layout_centerHorizontal="true"android:scaleType="fitXY"android:src="@mipmap/ic_launcher"/><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/iv_pic"android:layout_centerHorizontal="true"android:layout_marginTop="10dp"android:textColor="#323232"android:textSize="16sp"/>
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:clipChildren="false"tools:context=".ScrollingActivity"><RelativeLayoutandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="50dp"android:background="@color/colorPrimary"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="画廊"android:textColor="#fff"android:textSize="24sp"/></RelativeLayout><android.support.v7.widget.RecyclerViewandroid:id="@+id/rv"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerVertical="true"android:clipChildren="false"></android.support.v7.widget.RecyclerView></RelativeLayout>

item_list.xml

RecycleView实现Gallery画廊效果,中间放大两边缩小相关推荐

  1. Android开发学习之基于ViewPager实现Gallery画廊效果

    通过我们前面的学习,我们知道ViewPager是可以做出近乎完美的滑动体验,回顾整个Android,我们发现Gallery具备同样的特点,于是我们大胆地猜想,Gallery是否和ViewPager之间 ...

  2. Swift - 使用CollectionView实现图片Gallery画廊效果(左右滑动浏览图片)

    1,效果图 (1)图片从左至右横向排列(只有一行),通过手指拖动可以前后浏览图片. (2)视图滚动时,每张图片根据其与屏幕中心距离的不同,显示尺寸也会相应地变化.越靠近屏幕中心尺寸就越大,远离屏幕中心 ...

  3. ViewPager实现Gallery画廊效果——仿慕课网app-求职路线计划-效果(一)

    效果图: 大体上就是这个样子,可能不太清楚 但是大家知道是什么效果就好啦~ (PS: 图中没有加高斯模糊图,后边 我会加上) 下面来分析一下这个布局 底层用一个ImageView 上层用一个定制的Vi ...

  4. ViewPager实现Gallery画廊效果——仿慕课网app-求职路线计划-效果(二)

    今晚给之前的写的收个尾 http://blog.csdn.net/codenoodles/article/details/50992113 前边没有看的可以先看一下效果. 之前写的没有背景的模糊图片效 ...

  5. uniapp-swiper轮播图中间放大两边缩小

    有个新需求,做一个轮播图,但是需要轮播到中间的时候,中间放大. 是采用uniapp开发的小程序 下面贴代码和效果图哈 代码块: <view class="con-part2-con&q ...

  6. Android Gallery画廊 兼容4.0以上版本

    最近在做Gallery画廊效果时,搜索大量资料,发现很多博主都是2012年写的文章.对于现在的sdk版本,发现拿过来都没有用,效果变形: 非常遗憾,中间的图变形了,或者说没有把转角恢复. 查阅了大量资 ...

  7. Android自带组件之Gallery 实现3D画廊效果

    1: 首先我们要了解到这个该控件的常用属性: 如图: 2:通过该组件定义属于我们自己的组件 iphone 中的coverflow中图片切换是有旋转和缩放效果的,而自带的gallery中并没有实现.因此 ...

  8. android 滚动画画,Android利用ViewPager实现可滑动放大缩小画廊效果

    画廊在很多的App设计中都有,如下图所示: 该例子是我没事的时候写的一个小项目,具体源码地址请访问https://www.easck.com/> 使用方式 布局中添加该自定义控件 xmlns:t ...

  9. 使用RecyclerView实现旋转3D画廊效果

    3D旋转画廊效果实现有哪些方式? 1.Gallery实现(官方已不推荐使用). 2.RecyclerView通过自定义LayoutManager实现. 一.简介 RecyclerView是google ...

最新文章

  1. 你应该知道的高性能无锁队列Disruptor
  2. 网页设计简约_简约设计指南
  3. 黑客数字雨html单页,Hei客帝国数字雨.html
  4. 如何让 dotnetcore 在 Linux 上后台运行?
  5. 《小狗钱钱》:理财首先应该有一种强烈的意识
  6. 如何在 Python 数据中清洗常用 4 板斧?
  7. exchange server 2013 owa界面语言修改为中文
  8. 解决accuracy_score报错Classification metrics can‘t handle a mix of continuous and multiclass targets
  9. Winrar去广告图文教程
  10. 大学生集体恶搞学士服猥琐拍照,谁羞辱了谁?!(图)
  11. Rockchip_双屏显示旋转方向调试文档
  12. 【课程设计】基于图像处理的一维条形码识别-含matlab源码
  13. 传感器实验——LCD屏幕测试
  14. 回溯法——回溯法的算法思想
  15. [收集]仿163邮箱的JS编辑器
  16. 深度学习之BatchNorm(批量标准化)
  17. EasyNVR H5无插件摄像机直播解决方案前端解析之:引用videojs无法自动播放
  18. UAV-5--链接飞控以及配置SITL以及ardupilot环境
  19. 使用基于Boost的预处理器元编程实现变长类型列表的参数化
  20. PostgreSQL+PostGIS实现两坐标点之间最短路径查询算法函数(地图工具篇.12)

热门文章

  1. 百度地图路线规划(途经点)
  2. 南京邮电大学计算机学院讲师黄璞,我所与南京邮电大学建立“计算机技术联合实验室”...
  3. MacBook Pro 时间机器备份(完美解决连接移动硬盘无反应)
  4. 手机163邮箱怎么登录?163手机邮箱登录页面是?
  5. Windows扫雷游戏秘籍
  6. mc服务器 领地插件配置文件,《我的世界》领地插件 领地插件详细使用教程
  7. cs1.6 linux,在Ubuntu 8.04下玩CS1.6
  8. Unbrick wr703n wifi router
  9. xss labs 挑战之旅
  10. YTU 3386 哈希查找2