ImageSwitcher的应用
在android的开发中很多时候都会用到图片的切换,我相信大家在安装完apk后,都会遇到弹出用户向导,介绍本版本apk的新功能,如果你用的是平板电脑或手机的话,可以左右滑动以切换视图,如果你用的是android机顶盒的话,可以按遥控器的左右键以切换视图。那么这种用户向导切换图片是怎么做的呢? 当然答案有很多种,应该android中有太多方式可以实现了,比如ViewFilper ,ViewPager。这里我就介绍一种最简单的方式,那就是采用ImageSwitcher 图片切换器!
首先看一下ImageSwitcher的类继承结构:
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.FrameLayout
↳ android.widget.ViewAnimator
↳ android.widget.ViewSwitcher
↳ android.widget.ImageSwitcher
比较复杂! 和这个类等同继续结构的还有一个叫做TextSwitcher。他们两个是ViewSwitcher的唯一的两个子类,在ViewSwitcher中定义了一个接口叫做ViewFactory,所以在使用ImageSwitcher和TextSwitcher时必须要实现这个接口,其实在这个接口中,就只有一个方法,如下:
public abstract View makeView ()
Function:Creates a new View to be added in a ViewSwitcher.
Returns: a View
好了,到这里介绍ImageSwitcher就差不多了,咋门先来看看最后的实现结果
最下面的4个小点可以理解为位置指示吧,当前在那一页,最明显的小点,就会留在那个位置。这是通过LinearLayout加入TextView来实现的,其实这些小点都是TextView,只是背景颜色不一样而已,这里后面会具体介绍。
我们先来看看布局文件:act_guide.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ImageSwitcher
android:id="@+id/guide_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white" /> <LinearLayout
android:id="@+id/guide_indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" android:gravity="center_horizontal"
android:orientation="horizontal" />
</FrameLayout>
由于美观,要实现图片滑动效果,所有这里添加了2个动画,在res下新建anim文件夹,添加如下文件
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="500"
android:fromXDelta="50%p"
android:toXDelta="0" /> <alpha
android:duration="700"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="-50%p" /> <alpha
android:duration="700"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
好,之前提过的个4个小圆点,是怎么画上去的呢?这里就需要在res下新建一个drawable文件夹,在里面添加两个绘图的配置文件:
indicator_n.xml 即没有选中的颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thickness="10dp"
android:useLevel="false" >
<size
android:height="40dp"
android:width="40dp" />
<solid android:color="#88666666" />
</shape>
indicator_s.xml 即选中了的颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thickness="10dp"
android:useLevel="false" >
<size
android:height="40dp"
android:width="40dp" />
<solid android:color="#ff0b7fe4" />
</shape>
好,接下来,我们来看看主activity是怎样调用的:
Act_guide.java
package dxd.android.imageswitcher; import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory; public class ActGuide extends Activity implements ViewFactory { protected LayoutInflater mInflater;
protected ImageSwitcher switcher;
protected int[] itemIDs;
protected int pos;
protected int last;
protected LinearLayout indicator;
protected ViewGroup mParent;
protected boolean intercept = false; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInflater = LayoutInflater.from(this);
setContentView(R.layout.act_help); addViews() ;
} protected void addViews() {
itemIDs = new int[] { R.drawable.s1, R.drawable.s2, R.drawable.s3,R.drawable.s4};
switcher = (ImageSwitcher)findViewById(R.id.guide_content);
switcher.setFactory(this);
switcher.setImageResource(itemIDs[pos]); indicator = (LinearLayout) findViewById(R.id.guide_indicator);
for (int i = 0; i < itemIDs.length; i++) {
TextView item = new TextView(this);
item.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
if (i == 0) {
item.setBackgroundResource(R.drawable.indicator_s);
} else {
item.setBackgroundResource(R.drawable.indicator_n);
}
indicator.addView(item);
}
// 响应 触摸事件
switcher.setOnTouchListener(new OnTouchListener() {
private float x;
private boolean lock; @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
lock = true;
break;
case MotionEvent.ACTION_MOVE:
if (lock) {
float dltaX = x - event.getX();
if (dltaX > 100) {
showNext();
lock = false;
} else if (dltaX < -100) {
showPre();
lock = false;
}
}
break;
}
return true;
}
});
} @Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setScaleType(ScaleType.FIT_XY);
return imageView;
}
// 响应左右按键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
showPre();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
showNext();
return true;
}
return super.onKeyDown(keyCode, event);
} protected void showNext() {
last = pos++;
if (pos == itemIDs.length) {
pos = itemIDs.length - 1;
return;
}
switcher.setInAnimation(this, R.anim.slide_in_right);
switcher.setOutAnimation(this, R.anim.slide_out_left);
switcher.setImageResource(itemIDs[pos]);
indicator.getChildAt(last).setBackgroundResource(R.drawable.indicator_n);
indicator.getChildAt(pos).setBackgroundResource(R.drawable.indicator_s);
} protected void showPre() {
last = pos--;
if (pos < 0) {
pos = 0;
return;
}
switcher.setInAnimation(this, android.R.anim.slide_in_left);
switcher.setOutAnimation(this, android.R.anim.slide_out_right);
switcher.setImageResource(itemIDs[pos]);
indicator.getChildAt(last).setBackgroundResource(R.drawable.indicator_n);
indicator.getChildAt(pos).setBackgroundResource(R.drawable.indicator_s);
}
}
主要的代码就是这样的了。当然,之前说了,这不是唯一能实现的方式,还有很多方式都可以实现这样的效果。但是,如果这个用户向导或其他什么向导,只要是以图片切换为主,那么就选用这个ImageSwitcher吧! 方便快捷地就能够搭建好应用程序的外观。当然如果是需要View中有控件的切换的话,用ViewPager也是很好的选择。这里就不多介绍了,以后专门写出专题。
ImageSwitcher的应用的更多相关文章
- ImageLoader配合ImageSwitcher的使用
先在MyApplication中初始化ImageLoader initImageLoader(getApplicationContext()); /** * 初始化ImageLoader * 如果你经 ...
- ImageSwitcher图片切换的简单用例
ImageSwitcher的原理:ImageSwitcher有两个子View:ImageView,当左右滑动的时候,就在这两个ImageView之间来回切换来显示图片 实现左右滑动切换图片 BaseA ...
- Android之ImageSwitcher
要点: (查看Api总结) 1:ImageSwitcher 继承 ViewSwitcher, (ViewSwitcher 有继承FrameLayout ) 2: 要实现切图必须实现 ViewSwitc ...
- ImageSwitcher自定意效果+定时切换图片
Activity实现 1 import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; ...
- Xamarin开发Android笔记:图片切换ImageSwitcher
在移动应用开发过程中经常会使用到图片展示场景,例如利用多张图片说明一个产品的特点,此处就会使用到ImageSwithcher,当然也可以使用ViewFliper来实现,但使用ViewFliper的时候 ...
- android ImageSwitcher
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...
- android学习笔记14——GridView、ImageSwitcher
GridView--网格视图.ImageSwitcher--图像切换器 ==> GridView,用于在界面上按行.列的分布形式显示多个组件:GridView和ListView父类相同——Abs ...
- Android——ImageSwitcher 图片切换
public class ImageSwitcherActivity extends Activity implements OnClickListener, ViewFactory { ...
- Android 高级UI设计笔记12:ImageSwitcher图片切换器
1. ImageSwitcher ImageSwitcher是Android中控制图片展示效果的一个控件,如:幻灯片效果...,颇有感觉啊.做相册一绝 2. 重要方法 setImageURI(Uri ...
随机推荐
- HDU 1372 (搜索方向稍有改变) Knight Moves
其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...
- ab做压力测试
ab是apache 自带的一个压力测试的小工具,可用于接口简单的压力测试. 以下是AB的简要介绍 格式:ab [options] [http://]hostname[:port]/path 参数说明: ...
- [转]Git介绍
Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重点在于介绍Git的基本命令和使用技巧,让你尝试使用Git的同时,体验到原来一个版 本控制工具可以对开发产生如此之多的影响,文章分为两部分, ...
- Eclipse 打开编辑文件所在文件夹方法
一个便捷的方法在eclipse的菜单中,依次点击Run->External Tools-> External Tools configurations添加一个新的工具 OpenContai ...
- 【转】gcc中-pthread和-lpthread的区别
原文网址:http://chaoslawful.iteye.com/blog/568602 用gcc编译使用了POSIX thread的程序时通常需要加额外的选项,以便使用thread-safe的库及 ...
- Oracle RAC 负载均衡测试(结合服务器端与客户端)
Oracle RAC 负载均衡使得从客户端发起的连接能够有效地分配到监听器负载较小的实例上.有两种方式实现客户端负载均衡,一是通过配置客户端的load_balance,一是通过配置服务器端的remot ...
- JBPM4入门——3.JBPM4开发环境的搭建
本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...
- Linux下基于HTTP协议带用户认证的GIT开发环境设置
Git 的访问可以采用 HTTP 或 SSH 协议安全的访问,通常我们使用 gitlib 进行 Web 管理,但是在 Linux 命令行开发环境下,基本都是使用 SSH 协议,只需要在 gitlib ...
- 使用Spring 3的@value简化配置文件的读取
Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在applicationContext.xml文件中配置properties文件 & ...
- PhoneGap与Jquery Mobile结合开发android应用配置
由于工作需要,用到phonegap与jquery moblie搭配,开发android应用程序. 这些技术自己之前也都没接触过,可以说是压根没听说过,真是感慨,在开发领域,技术日新月异,知识真是永远学 ...