PercentRelativeLayout

PercentRelativeLayout百分比参数

ImageView

* app:layout_widthPercent=”50%”
* app:layout_heightPercent=”50%”
* app:layout_marginTopPercent=”25%”
* app:layout_marginLeftPercent=”25%”
*

  • {@code layout_heightPercent}
    *
  • {@code layout_marginPercent}
    *
  • {@code layout_marginLeftPercent}
    *
  • {@code layout_marginTopPercent}
    *
  • {@code layout_marginRightPercent}
    *
  • {@code layout_marginBottomPercent}
    *
  • {@code layout_marginStartPercent}
    *
  • {@code layout_marginEndPercent}
    *
    rowCount行数
    columnCount列数
  • 百分比布局

    加依赖 compile ‘com.android.support:percent:25.3.0’

    
    
    
    
        
    
    
    
        
    

    实现scrollview

     
    
        
    
            
    
                
    
                
    
                
    
                
            
    
            
    
                
    
                
    
                
    
            
    
            
    
                

    ViewPager轮播

    Handler发送消息

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int currentItem = vp.getCurrentItem();
            currentItem++;
            if (currentItem == 10) {
                currentItem = 0;
            }
            vp.setCurrentItem(currentItem);
            handler.sendEmptyMessageDelayed(100, 2000);
        }
    };
    

    ViewPager

        获取手机屏幕的宽和高
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(UIUtil.getScreenWidth() * 4 / 10, UIUtil.getScreenHeight() * 6 / 10);
        //设置居中
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    
        /**** 重要部分  ******/
        //clipChild用来定义他的子控件是否要在他应有的边界内进行绘制。 默认情况下,clipChild被设置为true。 也就是不允许进行扩展绘制。
        vp.setClipChildren(false);
        //父容器一定要设置这个,否则看不出效果
        rl.setClipChildren(false);
    
        vp.setLayoutParams(params);
        //为ViewPager设置PagerAdapter
        vp.setAdapter(new MyAdapter());
        //设置ViewPager切换效果,即实现画廊效果
        vp.setPageTransformer(true, new ZoomOutPageTransformer());
        //设置预加载数量
        vp.setOffscreenPageLimit(2);
        //设置每页之间的左右间隔
        vp.setPageMargin(100);
    
        handler.sendEmptyMessageDelayed(100, 2000);
        //将容器的触摸事件反馈给ViewPager
        rl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 在按下时 取消轮播
                        handler.removeMessages(100);
                        break;
                    // 触摸事件消失 (被抢走)
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        // 抬起后继续轮播
                        handler.sendEmptyMessageDelayed(100, 2000);
                        break;
    
                }
                return false;
    
            }
        });
    }
    
    
    private class MyAdapter extends PagerAdapter {
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    
    
        @Override
        public int getCount() {
            return 10;
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(ViewpagerActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setImageResource(R.mipmap.abc);
            container.addView(imageView);
            return imageView;
        }
    }
    
    /**
     * 实现的原理是,在当前显示页面放大至原来的MAX_SCALE
     * 其他页面才是正常的的大小MIN_SCALE
     */
    private class ZoomOutPageTransformer implements ViewPager.PageTransformer {
    
        private static final float MAX_SCALE = 1.4f;
        private static final float MIN_SCALE = 0.8f;//0.85f
    
        @Override
        public void transformPage(View view, float position) {
            //setScaleY只支持api11以上
            if (position < -1) {
                view.setScaleX(MIN_SCALE);
                view.setScaleY(MIN_SCALE);
            } else if (position <= 1) //a页滑动至b页 ; a页从 0.0 -1 ;b页从1 ~ 0.0
            { // [-1,1]
                //              Log.e("TAG", view + " , " + position + "");
                float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);
                view.setScaleX(scaleFactor);
                //每次滑动后进行微小的移动目的是为了防止在三星的某些手机上出现两边的页面为显示的情况
                if (position > 0) {
                    view.setTranslationX(-scaleFactor * 2);
                } else if (position < 0) {
                    view.setTranslationX(scaleFactor * 2);
                }
                view.setScaleY(scaleFactor);
    
            } else { // (1,+Infinity]
    
                view.setScaleX(MIN_SCALE);
                view.setScaleY(MIN_SCALE);
    
            }
        }
    }
    

    你可能感兴趣的:(PercentRelativeLayout)