app引導功能界面+廣告輪播控件,兩句代碼搞定



  • 廣告輪播控件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.orandnot.demo.MainActivity">

    <com.orandnot.rtviewpager.RecycleTimeViewPage
        android:id="@+id/rtPageView"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>

    <com.orandnot.rtviewpager.PageIndicatorView
        android:id="@+id/pageIndicatorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/rtPageView"
        android:layout_marginBottom="5dp"
        android:layout_centerHorizontal="true"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="引導頁"/>
</RelativeLayout>

MainActivity.java

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.orandnot.rtviewpager.PageIndicatorView;
import com.orandnot.rtviewpager.RecycleTimePageAdptar;
import com.orandnot.rtviewpager.RecycleTimeViewPage;
import com.orandnot.rtviewpager.animation.AnimationType;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RecycleTimePageAdptar.OnPageClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initWeclome();
    }

    private void initWeclome() {
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,WelcomeActivity.class));
            }
        });
    }

    private void init() {
        RecycleTimeViewPage rtPageView = (RecycleTimeViewPage) findViewById(R.id.rtPageView);
        List list = new ArrayList<>();
        int[] imgId = new int[]{R.mipmap.img_0, R.mipmap.img_1, R.mipmap.img_2, R.mipmap.img_3};
        for (int id : imgId) {
            ImageView view = new ImageView(this);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setImageResource(id);
            list.add(view);
        }
        RecycleTimePageAdptar mAdpter = new RecycleTimePageAdptar(this,list);
        mAdpter.setListener(this);
        rtPageView.setAdapter(mAdpter);
        rtPageView.setIntervalTime(3).startAutoPlay();

        PageIndicatorView indicatorView = (PageIndicatorView) findViewById(R.id.pageIndicatorView);
        indicatorView.setRadius(3)
                .setPadding(5)
                .setAnimationType(AnimationType.THIN_WORM)
                .setInteractiveAnimation(true)
                .setSelectedColor(Color.GREEN)
                .setUnselectedColor(Color.WHITE)
                .setViewPager(rtPageView);

    }

    @Override
    public void onPagePosition(int position) {
        Toast.makeText(this,">>>click>>>position="+position,Toast.LENGTH_SHORT).show();
    }
}


基本就實現了大部分app的廣告輪播的需求了,主要步驟就是,給RecycleTimeViewPage填充數據,rtPageView.setIntervalTime(3).startAutoPlay()設置時間,自動輪播就可以了。這里需要導入包 rtviewpager.jar,這個包是我自己寫的,結合了開源項目 PageIndicatorView就是底部的指引控件(具體的使用方法自己可以去找)。

  • 引導功能界面

第一次安裝app往往需要引導界面,實現方式很多種,一般都是ViewPager來實現,除了加載動畫效果和做點視差動畫,沒什么特別的。和上面定時輪播區別就是把界面設成全屏,然后去掉定時器。還有就是viewpager里的view要自定義一下。

welcome3.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">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@mipmap/img_6"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:text="開啟app之旅"
        />
</RelativeLayout>

activity_welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_welcome"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.orandnot.demo.WelcomeActivity">


    <com.orandnot.rtviewpager.RecycleTimeViewPage
        android:id="@+id/rtPageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.orandnot.rtviewpager.PageIndicatorView
        android:id="@+id/pageIndicatorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
Welcome.Activity
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.orandnot.rtviewpager.PageIndicatorView;
import com.orandnot.rtviewpager.RecycleTimePageAdptar;
import com.orandnot.rtviewpager.RecycleTimeViewPage;
import com.orandnot.rtviewpager.animation.AnimationType;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        init();
    }


    private void init() {
        RecycleTimeViewPage rtPageView = (RecycleTimeViewPage) findViewById(R.id.rtPageView);
        List list = new ArrayList<>();
        int[] imgId = new int[]{R.mipmap.img_4, R.mipmap.img_5};
        for (int id : imgId) {
            ImageView view = new ImageView(this);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setImageResource(id);
            list.add(view);
        }
        View view = getLayoutInflater().inflate(R.layout.welcome3,null);
        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        list.add(view);//加入第三個View,之前的view也可以類似自己設計
        RecycleTimePageAdptar mAdpter = new RecycleTimePageAdptar(this,list);
        rtPageView.setAdapter(mAdpter);

        PageIndicatorView indicatorView = (PageIndicatorView) findViewById(R.id.pageIndicatorView);
        indicatorView.setRadius(3)
                .setPadding(5)
                .setAnimationType(AnimationType.THIN_WORM)
                .setSelectedColor(Color.GREEN)
                .setUnselectedColor(Color.WHITE)
                .setInteractiveAnimation(true)
                .setViewPager(rtPageView);

    }
}


有個內存泄露的問題。。。有空解決





注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: