当第一次打开一个app时,通常有一个使用向导介绍本APK的基本功能和用法,这个向导是很重要的,方便用户能高速知道和适应该app如何用。

实现此使用向导有非常多种方法,比方用ImageSwitcher,ViewPager。当然要用ViewSwitcher或是ViewGroup去实现也是能够的,仅仅只是有点大材小用了。

用ImageSwitcher和ViewPager的差别就在于ImageSwitcher能轻松地指定页面切换的动画!这里先总结下ImageSwitcher的实现。

首先,定义布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
</ImageSwitcher>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dots"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:gravity="center_horizontal"
></LinearLayout>
</RelativeLayout>

布局文件里有一个id为dots的LinearLayout就是一会儿能够切换显示的小圆点。

让你的Activity实现ViewFactory接口,做为imageSwitcher.setFactory的參数,小圆点的实现是依据可切换图片的张数动态加入到dots中的。详细的代码例如以下:

package com.example.imageswitcher;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity implements ViewFactory { private ImageSwitcher mSwitcher ;
private LinearLayout mLinearLayout ;
private ImageView[] mImageViewDots ;
private int mIndex = 0 ;
private int mImageRes[] = new int[]{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e,
R.drawable.g
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout = (LinearLayout)findViewById(R.id.dots);
mSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
initDots();
mSwitcher.setFactory(this);
mSwitcher.setImageResource(mImageRes[0]);
mImageViewDots[0].setBackgroundResource(R.drawable.dot_focus);
} @SuppressLint("NewApi")
public void initDots(){
mLinearLayout.removeAllViews() ;
mImageViewDots = new ImageView[mImageRes.length] ;
for(int i=0 ;i<mImageRes.length ;i++){
ImageView dot = new ImageView(this);
dot.setBackgroundResource(R.drawable.dot_nomal);
dot.setLayoutParams(new LayoutParams(30,30)) ;
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(30,30));
mImageViewDots[i] = dot ;
mLinearLayout.addView(dot);
mLinearLayout.addView(tv);
}
} @Override
public View makeView() {
return new ImageView(this);
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
if(mIndex == 0){
mIndex = mImageRes.length -1 ;
}else{
--mIndex ;
}
mSwitcher.setInAnimation(MainActivity.this, R.anim.left_in) ;
mSwitcher.setOutAnimation(MainActivity.this, R.anim.right_out) ;
mSwitcher.setImageResource(mImageRes[mIndex%mImageRes.length]);
}else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
if(mIndex == mImageRes.length - 1){
mIndex = 0 ;
}else{
++mIndex ;
}
mSwitcher.setInAnimation(MainActivity.this, R.anim.rigth_in) ;
mSwitcher.setOutAnimation(MainActivity.this, R.anim.left_out) ;
mSwitcher.setImageResource(mImageRes[mIndex%mImageRes.length]);
}
for(int i=0 ;i<mImageViewDots.length ;i++){
if(i == mIndex%mImageRes.length){
mImageViewDots[i].setBackgroundResource(R.drawable.dot_focus);
}else{
mImageViewDots[i].setBackgroundResource(R.drawable.dot_nomal);
}
}
return super.onKeyDown(keyCode, event);
}
}

这里可切换的图片可任意制定。这里来说一下切换动画,这里总有4个动画,分为两组。第一组是当ImageSwitcher向左切换图片的时候,首先setInAnimation是设置即将出现的那张图片的动画,setOutAnimation是设置即将消息的那张图片的动画,各自是left_in,rigth_out。第二组则是与之相反的。四个动画分别例如以下(放在res/anim文件夹下):

left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="1000" />
</set>

rigth_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="1000" />
</set>

rigth_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:toXDelta="0"
android:duration="1000" />
</set>

left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="1000" />
</set>

针对小圆点,我是用drawable.xml文件去画的,在程序中能够指定它的大小。这里设置了两种颜色,用于差别当前在第几个页面。

dot_focus.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<corners android:radius="5dip" />
<solid android:color="#33FF00" />
</shape>

dot_nomal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<corners android:radius="5dip" />
<solid android:color="#FFFF99" />
</shape>

我是在Android机顶盒上开发的此Demo,全部捕捉的是左键和右键。假设是手机端的话,能够捕捉手势。上一张大概的图:





接下来将用ViewPager实现此效果。

用户向导左右滑动页面实现之ImageSwitcher的更多相关文章

  1. 用户向导左右滑动页面实现之ViewPager

    接着上一篇博客.上一篇博客是用ImageSwitcher实现用户向导功能,如今用ViewPager实现同样的功能. 直接看代码: 布局文件activity_main.xml <RelativeL ...

  2. 用户向导页面实现左右滑动的ViewPager

    然后在一个博客,以前的博客ImageSwitcher实现用户向导,现在,随着ViewPager实现同样的功能.直接看代码: 布局文件activity_main.xml <RelativeLayo ...

  3. Centos系统创建用户oracle后,用该用户登陆系统,页面加载报错GConf error

    Linux 的 GConf error 解决办法 问题: Centos系统创建用户oracle后,用该用户登陆系统,页面加载报错,导致重新进入Centos系统后出现: GConf error:Fail ...

  4. 利用HTML5判断用户是否正在浏览页面技巧

    现在,HTML5里页面可见性接口就提供给了程序员一个方法,让他们使用visibilitychange页面事件来判断当前页面可见性的状态,并针对性的执行某些任务.同时还有新的document.hidde ...

  5. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

  6. (26)基于cookie的登陆认证(写入cookie、删除cookie、登陆后所有域下的网页都可访问、登陆成功跳转至用户开始访问的页面、使用装饰器完成所有页面的登陆认证)

    获取cookie request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt='', max_age ...

  7. js判断用户是否离开当前页面

    简介 VisibilityChange 事件:用于判断用户是否离开当前页面 Code // 页面的 visibility 属性可能返回三种状态 // prerender,visible 和 hidde ...

  8. 以+scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?

    这里强调一点:在主线程中以+scheduledTimerWithTimeInterval...的方式触发的timer默认是运行在NSDefaultRunLoopMode模式下的,当滑动页面上的列表时, ...

  9. Android之怎样实现滑动页面切换【Fragment】

    Fragment 页面切换不能滑动 所以对于listview 能够加入的左右滑动事件 .不会有冲突比如(QQ的好友列表的删除)  Fragment 和viewpager 的差别  Viewpager ...

随机推荐

  1. Collectio集合,List《ArrayList,LinkedList》

    集合: Collection类 package com.collection.demo; import java.util.ArrayList; import java.util.Arrays; im ...

  2. 关于Scrapy爬虫项目运行和调试的小技巧(上篇)

    扫除运行Scrapy爬虫程序的bug之后,现在便可以开始进行编写爬虫逻辑了.在正式开始爬虫编写之前,在这里介绍四种小技巧,可以方便我们操纵和调试爬虫. 一.建立main.py文件,直接在Pycharm ...

  3. js判断浏览器的环境(pc端,移动端,还是微信浏览器)

    window.navigator.userAgent用来区分设备和浏览器 <!DOCTYPE html> <html> <head> <meta charse ...

  4. 使用VUE开发微信小程序

    使用 mpvue 开发小程序,你将在小程序技术体系的基础上获取到这样一些能力: 彻底的组件化开发能力:提高代码复用性完整的 Vue.js 开发体验方便的 Vuex 数据管理方案:方便构建复杂应用快捷的 ...

  5. luogu P3795 钟氏映射(递推)

    题意 n<=107 20MB 题解 也就是给n个点,把他们一个分为一组,或两个分为一组,有多少种方法. 空间大点随便做. 我们靠递推. 一个新点,要不自己一组,要不和前面的一个点构成一组. 所以 ...

  6. [CTSC2016]单调上升路径

    题目:UOJ#201. 题目大意:给定n个点(n是偶数)的完全图,现在要你给每条边确定一个权值(互不相等),使得最长的单调上升路径最短.现在要你输出边的权值. 一条路径被称为单调上升的,如果沿着它走时 ...

  7. mysql 基础函数语句

    1:查看当前登陆用户 select user(): 2:切换数据库 use mysql; 查看该表用户 select user,host from user; 4:退出数据库 5:查看数据库版本 se ...

  8. sz xshell

    yum install lrzsz -y

  9. 洛谷P5087 数学

    DP. 设f[i][j]为前j个数中选i个数的所有组合的分数之和 决策: 不选这个数,得分为f[i][j - 1] 选这个数,得分为f[i - 1][j - 1] * a[j] 可以得到状态转移方程为 ...

  10. UVA 11020 Efficient Solutions+multiset的应用

    题目链接:点击进入 首先来讲,非常easy看到我们事实上仅仅要维护优势人群的集合:假设增加一个新的人,我们首先看一下优势人群中是否有人会让这个人失去优势,假设没有,则将这个人插入集合中.但要注意到这个 ...