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); } }
有个内存泄露的问题。。。有空解决
相关文章推荐
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
- 两句代码搞定发送短信(smsManager)的功能
- 【Android】如何实现启动APP时引导页、欢迎页功能设置之(二)设置只在第一次启动APP时跳入引导界面
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
- 谈谈XCode代码自动完成Code Sense功能及XCode界面设置
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载续][重点推荐控件]
- 使用CTabCtrl控件实现属性页功能-界面类编程-VC
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载续][重点推荐控件]
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 如何将界面代码和功能代码分离(基于Delphi/VCL)
- asp.net2.0两句代码搞定文件上传
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 带关闭功能的Javascript对联广告代码
- Winform中代码Dialog控件实现(打开,保存,改变字体,颜色,打印功能)
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载续][重点推荐控件]
- 广告控件 XML 代码
- .net中用代码生成界面控件
- 抓取当前界面上任意控件图像的C#代码