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

RecycleView实现Gallery画廊效果,中间放大两边缩小_第1张图片   

 通过给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;

    @Override
    protected 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() {
        @Override
        public 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 {
        private String[] mDatas;

        public MyAdapter(String[] mDatas) {
            this.mDatas = mDatas;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(ScrollingActivity.this).inflate(R.layout.item_list, null);
            return new RecyclerView.ViewHolder(view) {
            };
        }

        @Override
        public 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]);
        }

        @Override
        public int getItemCount() {
            return mDatas.length;
        }
    }
}



 
  
 
  
activity_scrolling.xml




    

        
    

    


item_list.xml




    

    




具体实现就这样了



你可能感兴趣的:(android)