android学习——实现欢迎界面图片动画滑动切换

来源:互联网 发布:六孔陶笛淘宝 编辑:程序博客网 时间:2024/05/16 12:04

刚学习的时候想做两个activity之间滑动切换的效果,就查了几个博客写了之前的一篇“activity实现动画切换”,现在看来这个功能好水,难看且基本不会有用到的。

但第一次启动APP欢迎界面滑动进入的效果很常见,所以就查资料写了这么一个。

当然这是跟程序第一次启动结合着用的,具体判断是否第一次启动,请看上一篇android学习——判断APP(程序)是否第一次启动


首先要了解一下ViewPager,简单的说它的的功能就是可以使视图左右滑动那样。下面详细介绍这个效果步骤:

1,在activity_main中加载ViewPager
,其他不需要布局

<android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="fill_parent"        android:layout_height="fill_parent" />
</pre><pre name="code" class="java">
<pre name="code" class="java" style="color: rgb(51, 51, 51); line-height: 26px;"><span style="font-family: Arial;">2,创建几个layout,用来显示需要滑动的图片,我这里是三个,每个layout中可以做自己需要的布局,背景图片设置为自己设计好的,放在drawable文件夹中,我又在右上角加了一个透明且有边框的“跳过”按钮,代码如下</span>

<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/welcome1"    android:orientation="vertical" >    <Button        android:id="@+id/pass1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_marginRight="10dp"        android:layout_marginTop="10dp"        android:background="@drawable/shape"        android:text="@string/welcome_pass"        android:textColor="#ffffff" /></RelativeLayout>


其他两个跟这个一样,背景图片换了,button按钮id换成pass2 和in

3,创建一个WelcomeActivity,里面的代码如下

public class WelcomeActivity extends Activity implements OnClickListener {<span style="white-space:pre"></span>private ViewPager mViewPager;<span style="white-space:pre"></span>List<View> viewList;<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>protected void onCreate(Bundle savedInstanceState) {<span style="white-space:pre"></span>super.onCreate(savedInstanceState);<span style="white-space:pre"></span>setContentView(R.layout.splash_activity);<span style="white-space:pre"></span><span style="white-space:pre"></span>@SuppressWarnings("static-access")<span style="white-space:pre"></span>// 用LayoutInflater中的getLayoutInflater加载要显示的界面<span style="white-space:pre"></span>// 获得 LayoutInflater 实例<span style="white-space:pre"></span>LayoutInflater mInflater = getLayoutInflater().from(this);<span style="white-space:pre"></span>// 加载要显示的界面<span style="white-space:pre"></span>View v1 = mInflater.inflate(R.layout.welcome1, null);<span style="white-space:pre"></span>View v2 = mInflater.inflate(R.layout.welcome2, null);<span style="white-space:pre"></span>View v3 = mInflater.inflate(R.layout.welcome3, null);<span style="white-space:pre"></span>// 将要分页显示的View装入数组中<span style="white-space:pre"></span>viewList = new ArrayList<View>();<span style="white-space:pre"></span>viewList.add(v1);<span style="white-space:pre"></span>viewList.add(v2);<span style="white-space:pre"></span>viewList.add(v3);<span style="white-space:pre"></span>// 实例化ViewPager组件,并设置它的PagerAdapter,在这里一般需要重写PagerAdapter,具体设置和重写见WelcomeActivityAdapter<span style="white-space:pre"></span>mViewPager = (ViewPager) findViewById(R.id.viewpager);<span style="white-space:pre"></span>mViewPager.setAdapter(new WelcomePagerAdapter(viewList));<span style="white-space:pre"></span>mViewPager.setCurrentItem(0);<span style="white-space:pre"></span>/*<span style="white-space:pre"></span> * 另外一种初始化写法:<span style="white-space:pre"></span> *  // 初始化Adapter <span style="white-space:pre"></span> * private ViewPagerAdapter vpAdapter; <span style="white-space:pre"></span> * <span style="white-space:pre"></span> * vpAdapter = new ViewPagerAdapter(viewList, this); <span style="white-space:pre"></span> * vp = (ViewPager)findViewById(R.id.viewpager); <span style="white-space:pre"></span> * vp.setAdapter(vpAdapter); <span style="white-space:pre"></span> * // 绑定回调<span style="white-space:pre"></span> * vp.setOnPageChangeListener(this);<span style="white-space:pre"></span> */<span style="white-space:pre"></span>//获取列表中的每个界面<span style="white-space:pre"></span>View view0 = viewList.get(0);<span style="white-space:pre"></span>View view1 = viewList.get(1);<span style="white-space:pre"></span>View view2 = viewList.get(2);<span style="white-space:pre"></span>// 找到界面中的“跳过”按钮<span style="white-space:pre"></span>Button pass1 = (Button) view0.findViewById(R.id.pass1);<span style="white-space:pre"></span>Button pass2 = (Button) view1.findViewById(R.id.pass2);<span style="white-space:pre"></span>Button in = (Button) view2.findViewById(R.id.in);<span style="white-space:pre"></span><span style="white-space:pre"></span>//设置onClick<span style="white-space:pre"></span>pass1.setOnClickListener(this);<span style="white-space:pre"></span>pass2.setOnClickListener(this);<span style="white-space:pre"></span>in.setOnClickListener(this);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>//设置onClick方法,点击跳转到主界面,因为每一个按钮的点击事件都是进入程序,所以不需要switch了<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void onClick(View v) {<span style="white-space:pre"></span><span style="white-space:pre"></span>Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);<span style="white-space:pre"></span>WelcomeActivity.this.startActivity(intent);<span style="white-space:pre"></span>WelcomeActivity.this.finish();<span style="white-space:pre"></span>}

4,创建WelcomePagerAdapterActivity,代码如下:

//PagerAdapter是viewpager的适配器,必须继承它public class WelcomePagerAdapter extends PagerAdapter {//界面列表private List<View> mListView;public WelcomePagerAdapter(List<View> mListView) {super();this.mListView = mListView;}// 销毁arg1位置的界面public void destroyItem(View arg0, int arg1, Object arg2) {((ViewGroup) arg0).removeView(mListView.get(arg1));}@Overridepublic void finishUpdate(View arg0) {}// 获取当前窗体界面数public int getCount() {return mListView.size();}// 初始化arg1位置的界面@Overridepublic Object instantiateItem(View arg0, int arg1) {((ViewGroup) arg0).addView(mListView.get(arg1), 0);return mListView.get(arg1);}// 判断是否由对象生成界面public boolean isViewFromObject(View arg0, Object arg1) {return arg0 == (arg1);}@Overridepublic void restoreState(Parcelable arg0, ClassLoader arg1) {}@Overridepublic Parcelable saveState() {return null;}@Overridepublic void startUpdate(View arg0) {}}

5,创建一个欢迎界面结束后要进入的Activity,我设置的是一个登陆界面,这个根据自己的需要编写,下一篇写,登陆功能的实现。

0 0
原创粉丝点击