您的位置:首页 > 移动开发

app引导功能界面+广告轮播控件,两句代码搞定

2016-12-10 20:04 801 查看


广告轮播控件

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);

}
}


有个内存泄露的问题。。。有空解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐