Android 启动页面gif动态图添加(gif图启动一次)

废话不多说,上代码:

首先使用viewpager +view 的方式进行显示启动页面布局:

mainactivity界面布局(LinearLayout)

   <ImageView
       android:layout_gravity="center"
       android:id="@+id/ImGas"
       android:layout_width="match_parent"
       android:layout_height="260dp">
   </ImageView>
   <android.support.v4.view.ViewPager
       android:id="@+id/sp_viewpager"
       android:layout_width="match_parent"
       android:layout_height="300dp"
       android:overScrollMode="never" />

   <LinearLayout
       android:id="@+id/ll_dot"
       android:layout_marginTop="10dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:orientation="horizontal" />
   <TextView
       android:gravity="center"
       android:background="@drawable/yuanjiao"
       android:textColor="@color/white"
       android:layout_gravity="center"
       android:id="@+id/but_tg"
       android:layout_marginTop="@dimen/dp20"
       android:layout_centerInParent="true"
       android:text="立即体验"
       android:layout_below="@id/sp_viewpager"
       android:layout_width="110dp"
       android:layout_height="@dimen/dp30">
   </TextView>

然后在onCreate方法中获取Id啥的:

        viewPager = findViewById(R.id.sp_viewpager);
        mLlDot = findViewById(R.id.ll_dot);
        imgs = findViewById(R.id.ImGas);
        but_tg = findViewById(R.id.but_tg);
       
       views = new ArrayList<>();
        View inflate1 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager1, null);
        View inflate2 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager2, null);
        View inflate3 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager3, null);
        View inflate4 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager4, null);
        View inflate5 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager5, null);

 Glide.with(this).load(R.drawable.gif1).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imgs, 1));
        views.add(inflate1);
        views.add(inflate2);
        views.add(inflate3);
        views.add(inflate4);
        views.add(inflate5);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(views);

        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(curIndex);
        viewPager.setOffscreenPageLimit(5);
        viewPager.addOnPageChangeListener(this);
 but_tg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump();
            }
        });


       setOvalLayout();
        

然后在oncreate方法外:

 @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        // 取消圆点选中
        mLlDot.getChildAt(curIndex)
                .findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_normal);
        // 圆点选中
        mLlDot.getChildAt(position)
                .findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_selected);
        curIndex = position;
        if (position==0){
            Glide.with(this).load(R.drawable.gif1).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imgs, 1));
        }else if (position==1){
            Glide.with(this).load(R.drawable.gif2).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imgs, 1));
        }else if (position==2){
            Glide.with(this).load(R.drawable.gif3).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imgs, 1));
        }else if(position==3){
            Glide.with(this).load(R.drawable.gif4).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imgs, 1));
        }else{
            Glide.with(this).load(R.drawable.gif5).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imgs, 1));
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 设置圆点
     */
    public void setOvalLayout() {
        for (int i = 0; i < views.size(); i++) {
            mLlDot.addView( LayoutInflater.from(getApplicationContext()).inflate(R.layout.dot, null));

        }
        // 默认显示第一页
        mLlDot.getChildAt(0).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_selected);

    }

然后是viewpager下边圆点的布局 dot.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 小圆点View -->
    <View
        android:id="@+id/v_dot"
        android:layout_width="5dp"
        android:layout_height="5dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:background="@drawable/dot_normal"/>

</RelativeLayout>

dot_normal.xml:

<?xml version="1.0" encoding="utf-8"?><!-- 圆点未选中 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/gray" />

    <corners android:radius="5dp" />
</shape>

dot_selected:

<?xml version="1.0" encoding="utf-8"?><!-- 圆点选中 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/white" />
    <corners android:radius="5dp" />
</shape>

然后就齐活了,总结的不好,大佬勿喷!!感谢!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值