根据ViewPager中图片的宽度,算出滚动条的宽度,并且在ViewPager上执行平移动画


上面的黄色进度条,是根据,服务器返回图片张数的个数,进行算出来的宽度,好吧,下面开始代码

首先请看黄色滚动条的布局文件,

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="4dp">
<ImageView
    android:id="@+id/ll_imageview_line"
    android:layout_width="match_parent"
    android:layout_height="4dp"
     android:background="#808081"
    />
    <ImageView
        android:id="@+id/iv_line_blue"
        android:layout_width="wrap_content"
        android:layout_height="4dp"
        android:background="@color/line_blue"
        />

</FrameLayout>
其实下面滚动条哪块的宽度,其实就是一个帧布局,高度为4,第一个ImageView是哪个灰色条,然后下面的哪个ImageView是哪个黄色的条的图片背景,因为是帧布局,所以它正好在灰色条的上面,到时候,我们会根据服务器,图片的张数,在代码中去动态的设置其宽度
写此代码分三步去做:
1:联网去获得数据,得出张数,(联网代码不写,相信大家都会的哦)
2:得出张数,之后,算出屏幕的宽度,然后除以张数,就是黄条,该占屏幕的几分之一
代码如下:
  1,算手机屏幕宽度的代码,
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenW = dm.widthPixels;// 获取分辨率宽度
  2: 设置长条的宽度,其实长条的宽度就是它每次,跟随图片滑动的偏移量,固可以先求出偏移量
  偏移量为
//有几张图片就让其便宜屏幕的多少分之一
 int offset = screenW / 服务器返回图片的张数即size;// 计算每次进行平移动画的偏移量
3://给黄色的滚动条设置宽度
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) iv_line_blue.getLayoutParams();
params.width = offset;
iv_line_blue.setLayoutParams(params);
4:根据图片的滑动,黄色进度条,在下面执行响应的平移动画
  原理就是,比如现在是第一张,它有可能便宜到第二张,或者最后一张(反向翻页的时候)那么他便宜到第二张得偏移量就是一个0ffset(刚才算出的一个偏移量),偏移到最后的便宜量,根据你们项目需求自己定,我定义的时候是从最右边过去,即大于屏幕右边的坐标位置向左过去,效果就是,直接当从第一张到最后一张的时候,基本上是直接到了最后面,并没有看到对应的平移在屏幕上面,
   代码:
    //选中了第几张图片
int index = position % bannerDomain.results.size();
switch (index) {
    case 0:   
          //当前第几张图片,即从当前图片的位置,平移到选中的下一个图片的地方
        if (currintPosition == 0) {
            animation = new TranslateAnimation(0, 0, 0, 0);
        }
           //这句话是防止,服务器只返回了一张图片,那么黄色的条的宽度就是整个屏幕,不用进行相应的平移
        if (bannerDomain.results.size() == 1) {
            animation = new TranslateAnimation(0, 0, 0, 0);
            //直接break;
        }
        if (currintPosition == count) {
            //从最后一页,直接跳到第一页
            animation = new TranslateAnimation(-10, 0, 0, 0);

        } else if (currintPosition == 1) {
            //从0到1

            animation = new TranslateAnimation(offset, 0, 0, 0);
        }
        break;
    case 1:
        if (currintPosition == 0) {
            //从最后0页,直接跳到第一页
            animation = new TranslateAnimation(0, offset, 0, 0);

        } else if (currintPosition == 2) {
            //从2到1
            animation = new TranslateAnimation(offset * 2, offset, 0, 0);
        }
        break;
    case 2:
        if (currintPosition == 0) {
            //还有可能从0直接挑到2
            animation = new TranslateAnimation(offset * 2 + 10, offset * 2, 0, 0);

        }
        if (currintPosition == 1) {
            //从最后1页,直接跳到第2页
            animation = new TranslateAnimation(offset, offset * 2, 0, 0);

        } else if (currintPosition == 3) {
            //从3到2
            animation = new TranslateAnimation(offset * 3, offset * 2, 0, 0);
        }
        break;
    case 3:
        if (currintPosition == 0) {
            //还有可能从0直接挑到3
            animation = new TranslateAnimation(offset * 3 + 10, offset * 3, 0, 0);
        }
        if (currintPosition == 2) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 2, offset * 3, 0, 0);

        } else if (currintPosition == 4) {
            //从4到3
            animation = new TranslateAnimation(offset * 4, offset * 3, 0, 0);
        }
        break;
    case 4:
        if (currintPosition == 0) {
            //还有可能从0直接挑到4
            animation = new TranslateAnimation(offset * 4 + 10, offset * 4, 0, 0);
        }
        if (currintPosition == 3) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 3, offset * 4, 0, 0);

        } else if (currintPosition == 5) {
            //从4到3
            animation = new TranslateAnimation(offset * 5, offset * 4, 0, 0);
        }
        break;
    case 5:
        if (currintPosition == 0) {
            //还有可能从0直接挑到3
            animation = new TranslateAnimation(offset * 5 + 10, offset * 5, 0, 0);
        }
        if (currintPosition == 4) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 4, offset * 5, 0, 0);

        } else if (currintPosition == 6) {
            //从4到3
            animation = new TranslateAnimation(offset * 6, offset * 5, 0, 0);
        }

        break;
    case 6:
        if (currintPosition == 0) {
            //还有可能从0直接挑到3
            animation = new TranslateAnimation(offset * 6 + 10, offset * 6, 0, 0);
        }
        if (currintPosition == 5) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 5, offset * 6, 0, 0);

        } else if (currintPosition == 7) {
            //从4到3
            animation = new TranslateAnimation(offset * 7, offset * 6, 0, 0);
        }
        break;
    case 7:
        if (currintPosition == 0) {
            //还有可能从0直接挑到3
            animation = new TranslateAnimation(offset * 7 + 10, offset * 7, 0, 0);
        }
        if (currintPosition == 6) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 6, offset * 7, 0, 0);

        } else if (currintPosition == 8) {
            //从4到3
            animation = new TranslateAnimation(offset * 8, offset * 7, 0, 0);
        }
        break;
    case 8:
        if (currintPosition == 0) {
            //还有可能从0直接挑到3
            animation = new TranslateAnimation(offset * 8 + 10, offset * 8, 0, 0);
        }
        if (currintPosition == 7) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 7, offset * 8, 0, 0);

        } else if (currintPosition == 9) {
            //从4到3
            animation = new TranslateAnimation(offset * 9, offset * 8, 0, 0);
        }
        break;
    case 9:
        if (currintPosition == 0) {
            //还有可能从0直接挑到3
            animation = new TranslateAnimation(offset * 9 + 10, offset * 9, 0, 0);
        }
        if (currintPosition == 8) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 8, offset * 9, 0, 0);

        } else if (currintPosition == 10) {
            //从4到3
            animation = new TranslateAnimation(offset * 10, offset * 9, 0, 0);
        }
        break;
    case 10:
        if (currintPosition == 0) {
            //还有可能从0直接挑到3
            animation = new TranslateAnimation(offset * 10 + 10, offset * 10, 0, 0);
        }
        if (currintPosition == 9) {
            //从最后2页,直接跳到第3页
            animation = new TranslateAnimation(offset * 9, offset * 10, 0, 0);

        } else if (currintPosition == 11) {
            //从4到3
            animation = new TranslateAnimation(offset * 11, offset * 10, 0, 0);
        }
        break;
    default:
        animation = new TranslateAnimation(0, 0, 0, 0);
        break;

}
//把当前选中的位置,复制给当前位置的变量,其中
//currintPosition是用来记录当前位置的变量
currintPosition = index;
//保持平移后变量的状态
animation.setFillAfter(true);
animation.setDuration(300);
iv_line_blue.startAnimation(animation);

 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值