怎么使用TextSthe witcher 小说文本切换器

TextSwitcher集成了ViewSwitcher, 因此它具有与ViewSwitcher相同的特性:可以在切换View组件时使用动画效果。与ImageSwitcher相似的是,使用TextSwitcher也需要设置一个ViewFactory。与ImageSwitcher不同的是,TextSwitcher所需要的ViewFactory的makeView()方法必须返回一个TextView组件。&TextSwitcher与TextView的功能有点类似,它们都可用于显示文本内容,区别在于TextSwitcher的效果更炫,它可以指定文本切换时的动画效果。&不多说,直接上代码了。界面布局文件如下:&RelativeLayout xmlns:android=&/apk/res/android& xmlns:tools=&/tools& android:layout_width=&match_parent& android:layout_height=&match_parent& &
&!-- 定义一个TextSwitcher,并制定了文本切换时的动画效果 --& &TextSwitcher
android:id=&@+id/textSwitcher&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:textAlignment=&center&
android:layout_centerHorizontal=&true&
android:layout_centerVertical=&true&
android:inAnimation=&@android:anim/slide_in_left&
android:outAnimation=&@android:anim/slide_out_right&
android:onClick=&next&
& &/TextSwitcher&&/RelativeLayout&上面的布局文件中定义了一个TextSwitcher,并为该文本切换指定了文本切换时的动画效果,接下来Activity只要为该TextSwitcher设置ViewFactory,该TextSwitcher即可正常工作。如下是Activity代码:/** * TextSwitcher practice. * @author peter. * */public class MainActivity extends Activity { private TextSwitcher textS
// 要显示的文本 String[] strs = new String[]
}; private int curS
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
textSwitcher.setFactory(new ViewFactory() {
public View makeView() {
TextView tv = new TextView(MainActivity.this);
tv.setTextSize(40);
// 字体颜色品红
tv.setTextColor(Color.MAGENTA);
//调用next方法显示下一个字符串
next(null); }
// 事件处理函数,控制显示下一个字符串 public void next(View source) {
textSwitcher.setText(strs[curStr++ % strs.length]); }}怎样制作一个 customized view,使它可以在 stroyboard editor 中也可以像内置控件_百度知道
怎样制作一个 customized view,使它可以在 stroyboard editor 中也可以像内置控件
提问者采纳
  创建自定义组件customized view,使其可以在stroyboard editor 中也可以像内置:  Android提供了一个精致而强大的组件化模式来创建你的用户界面,基于基础的布局类:视图 View和视图组ViewGroup。平台包含了多种预定义视图和视图组子类-分别称为部件和布局-这些可以用来构造你的用户界面。  一部分可用部件包括按钮Button,文本视图TextView,编辑文本框EditText,列表视图ListView,组合框CheckBox, 单选按钮RadioButton, 画廊Gallery, 微调器Spinner, 以及一些用于特定场合的自动补全文本视图AutoCompleteTextView, 图片切换器ImageSwitcher, 和文本切换器TextSwitcher.  可用布局有线性布局LinearLayout, 框架布局FrameLayout, 相对布局RelativeLayout, 以及其他。更多例子,参见常用布局对象Common Layout Objects.  如果这些预定义的部件或布局不能满足你的需求,你可以创建你自己的视图类。如果你只需要在现有的部件或布局上做些调整,你只需要子类化这个部件或布局并重写它的方法。  创建自己的视图子类让你可以精确控制界面元素的外形和功能。为了对这种控制有个大概的印象,下面是一些例子说明你可以用它们做什么:  ·你可以创建一个完全自定义绘制的视图类型,比如一个用2D图形绘制的“音量控制”旋钮,使它看上去像一个模拟电子件。  ·你可以把一组视图组件组合进一个单独的组件里,也许像一个组合框(是弹出列表和自由输入的文本段的组合),一个双窗格选择器控件(一个左窗格和右窗格,里面各有一个列表,你可以选择哪个项应该在哪个列表中),如此等等。  ·你可以重写一个编辑文本EditText组件的绘制方式(记事本指南Notepad Tutorial中使用这个方法创建了一个条纹状的记事本页面效果)。  ·你可以捕获其他事件如按键然后以自定义的方式处理。(比如一个游戏所做的那样)
来自团队:
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁5118人阅读
先看看继承关系,ImageSwitcher和TextSwitcher的继承关系是一样的。两个重要的父类:ViewSwitcher和ViewAnimator
继承于ViewSwitcher,说明具备了切换功能
继承于ViewAnimator,说明具备了动画功能
ImageSwitcher原理
ImageSwitcher的内容在Gallery中已经有所讲解,这边系统的详解一下
ImageSwitcher粗略的理解就是ImageView的选择器
ImageSwitcher的原理:ImageSwitcher有两个子View:ImageView,当左右滑动的时候,就在这两个ImageView之间来回切换来显示图片
下面我们来看看Android自带的source,以便更深的理解这个原理:
既然有两个子ImageView,那么我们要创建两个ImageView给ImageSwitcher。创建ImageSwitcher是通过工厂来实现的,看下面代码
imageSwicher.setFactory(this);为imageSwitcher设置ViewFactory
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setImageResource(arrayPictures[pictureIndex]);
return imageV
}实现ViewFactory的makeView()方法,makeView()方法就是负责给ImageSwitcher创建两个字ImageView
下面再来看看setFactory()方法的具体代码
public void setFactory(ViewFactory factory) {
mFactory =
obtainView();
obtainView();
}可以看到在setFactory的同时,调用了两遍obtainView()方法,obtainView()方法就是给ImageSwitcher添加子ImageView的,调用两遍就是添加了两个子ImageView
再来看看obtainView()方法的具体代码
private View obtainView() {
View child = mFactory.makeView();
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp == null) {
lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
addView(child, lp);
}可以看到obtainView()方法的的职责就是:通过makeView()方法创建View,然后把创建出来的View添加到ImageSwitcher上
再来看看下面的方法
public void setImageResource(int resid)
ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);
showNext();
}此方法就是用来显示下一张图片的,我们可以看到这个方法里面调用了getNextView()方法和showNext()方法,那么我们来看看这两个方法的具体代码
public View getNextView() {
int which = mWhichChild == 0 ? 1 : 0;
return getChildAt(which);
}public void showNext() {
setDisplayedChild(mWhichChild + 1);
}getNextView()方法是在两个子ImageView之间切换,showNext()方法是负责显示这两个子View中的哪一个
也就是说,现用getNextView()方法得到下一个View,然后重新设置这个View的imageResource,最后通过showNext()方法将下一个View显示出来
好了,ImageSwitcher的原理讲完了。下面附上一个Demo
ImageSwitcher实例
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical& &
&ImageSwitcher
android:id=&@+id/imageSwicher&
android:layout_width=&fill_parent&
android:layout_height=&0dip&
android:layout_weight=&1&
android:background=&@android:color/white&
android:gravity=&center& &
&/ImageSwitcher&
&/LinearLayout&
ImageSwicherDemoActivity.java
package com.
import android.app.A
import android.os.B
import android.view.MotionE
import android.view.V
import android.view.View.OnTouchL
import android.view.animation.AnimationU
import android.widget.ImageS
import android.widget.ImageV
import android.widget.ViewSwitcher.ViewF
* 一个左右滑动浏览图片的Demo
* @author tianjf
public class ImageSwicherDemoActivity extends Activity implements ViewFactory,
OnTouchListener {
private ImageSwitcher imageS
// 图片数组
private int[] arrayPictures = { R.drawable.bg001, R.drawable.bg002,
R.drawable.bg003, R.drawable.bg004 };
// 要显示的图片在图片数组中的Index
private int pictureI
// 左右滑动时手指按下的X坐标
private float touchDownX;
// 左右滑动时手指松开的X坐标
private float touchUpX;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwicher = (ImageSwitcher) findViewById(R.id.imageSwicher);
// 为ImageSwicher设置Factory,用来为ImageSwicher制造ImageView
imageSwicher.setFactory(this);
// 设置ImageSwitcher左右滑动事件
imageSwicher.setOnTouchListener(this);
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setImageResource(arrayPictures[pictureIndex]);
return imageV
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 取得左右滑动时手指按下的X坐标
touchDownX = event.getX();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// 取得左右滑动时手指松开的X坐标
touchUpX = event.getX();
// 从左往右,看前一张
if (touchUpX - touchDownX & 100) {
// 取得当前要看的图片的index
pictureIndex = pictureIndex == 0 ? arrayPictures.length - 1
: pictureIndex - 1;
// 设置图片切换的动画
imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
imageSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
// 设置当前要看的图片
imageSwicher.setImageResource(arrayPictures[pictureIndex]);
// 从右往左,看下一张
} else if (touchDownX - touchUpX & 100) {
// 取得当前要看的图片的index
pictureIndex = pictureIndex == arrayPictures.length - 1 ? 0
: pictureIndex + 1;
// 设置图片切换的动画
// 由于Android没有提供slide_out_left和slide_in_right,所以仿照slide_in_left和slide_out_right编写了slide_out_left和slide_in_right
imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.slide_out_left));
imageSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.slide_in_right));
// 设置当前要看的图片
imageSwicher.setImageResource(arrayPictures[pictureIndex]);
由于Android没有提供slide_out_left和slide_in_right,所以仿照slide_in_left和slide_out_right编写了slide_out_left和slide_in_right,代码如下:
slide_in_right.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&&
&translate android:fromXDelta=&50%p& android:toXDelta=&0& android:duration=&300&/&
&alpha android:fromAlpha=&0.0& android:toAlpha=&1.0& android:duration=&300& /&
slide_out_left.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&&
&translate android:fromXDelta=&0& android:toXDelta=&-50%p& android:duration=&300&/&
&alpha android:fromAlpha=&1.0& android:toAlpha=&0.0& android:duration=&300& /&
好了ImageSwitcher的讲解到此结束,下面附上一个和ImageSwitcher差不多的TextSwitcher的Demo。
由于TextSwitcher的原理和ImageSwitcher一样,只是一个是ImageView,一个是TextView。那么在此就不多说,直接上代码
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical& &
&TextSwitcher
android:id=&@+id/textSwicher&
android:layout_width=&fill_parent&
android:layout_height=&0dip&
android:layout_weight=&1&
android:background=&@android:color/white&
android:gravity=&center& &
&/TextSwitcher&
&/LinearLayout&
TextSwitcherDemoActivity.java
package com.
import android.app.A
import android.os.B
import android.view.G
import android.view.MotionE
import android.view.V
import android.view.View.OnTouchL
import android.view.ViewGroup.LayoutP
import android.view.animation.AnimationU
import android.widget.TextS
import android.widget.TextV
import android.widget.ViewSwitcher.ViewF
* 一个左右滑动浏览文本的Demo
* @author tianjf
public class TextSwitcherDemoActivity extends Activity implements ViewFactory,
OnTouchListener {
private TextSwitcher textS
// 图片数组
private String[] arrayTexts = { &文本01&, &文本02&, &文本03&, &文本04& };
// 要显示的图片在图片数组中的Index
private int textI
// 左右滑动时手指按下的X坐标
private float touchDownX;
// 左右滑动时手指松开的X坐标
private float touchUpX;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textSwicher = (TextSwitcher) findViewById(R.id.textSwicher);
// 为TextSwitcher设置Factory,用来为TextSwitcher制造TextView
textSwicher.setFactory(this);
// 设置TextSwitcher左右滑动事件
textSwicher.setOnTouchListener(this);
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(100);
textView.setLayoutParams(new TextSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
textView.setGravity(Gravity.CENTER);
textView.setText(arrayTexts[textIndex]);
return textV
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 取得左右滑动时手指按下的X坐标
touchDownX = event.getX();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// 取得左右滑动时手指松开的X坐标
touchUpX = event.getX();
// 从左往右,看前一文本
if (touchUpX - touchDownX & 100) {
// 取得当前要看的文本的index
textIndex = textIndex == 0 ? arrayTexts.length - 1
: textIndex - 1;
// 设置文本切换的动画
textSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
textSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
// 设置当前要看的文本
textSwicher.setText(arrayTexts[textIndex]);
// 从右往左,看下一张
} else if (touchDownX - touchUpX & 100) {
// 取得当前要看的文本的index
textIndex = textIndex == arrayTexts.length - 1 ? 0
: textIndex + 1;
// 设置文本切换的动画
// 由于Android没有提供slide_out_left和slide_in_right,所以仿照slide_in_left和slide_out_right编写了slide_out_left和slide_in_right
textSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.slide_out_left));
textSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.slide_in_right));
// 设置当前要看的文本
textSwicher.setText(arrayTexts[textIndex]);
slide_in_right.xml和slide_out_left.xml可以参照ImageSwitcher的Demo,在此就不重复了。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:229575次
积分:2965
积分:2965
排名:第6081名
原创:67篇
转载:16篇
评论:47条
(1)(3)(1)(2)(8)(1)(2)(13)(1)(2)(10)(4)(6)(15)(14)> 最近应用切换Last App Switcher汉化版app切换器
分辨率:320x480,480x854,480x800,960x540,0x1080
推荐下载:
Last App Switcher是一款app切换器,运用桌面起浮按钮,点击后能够开启你近来运用的一个使用。 Last App Switcher本来还挺好用的,当然关于那些需要在几个app之间来回切换的来说能够觉得不行,假如这个app能在这方面再优化一下,这东东我会一向用。1.99更新日志:完全更改算法浮动按钮不再自己移动添加锁定按钮选项添加打开设置和关闭按钮选项添加黑色&白色图标修复bug<FONT color=#ff更新日志:在通知栏中添加 显示/隐藏 浮动按钮选项添加更改浮动按钮颜色选项添加浮动按钮盒快捷方式之间切换的选项UI细微更改<FONT color=#ff更新日志:修复多个bug为应用桌面浮动按钮添加进行中程序通知,浮动按钮不再偶尔消失修复布局bug
0 && image.height>0){if(image.height>=300){this.height=300;this.width=image.width*300/image.}}" />
0 && image.height>0){if(image.height>=300){this.height=300;this.width=image.width*300/image.}}" />
0 && image.height>0){if(image.height>=300){this.height=300;this.width=image.width*300/image.}}" />
0 && image.height>0){if(image.height>=300){this.height=300;this.width=image.width*300/image.}}" />
相关标签:
统统是一款由盛大网络推出,根据通讯录的多人语音通话软件。它不只供给....
Advanced Task Manager Pro是一款进程管理软....
消费者发现最值得信赖的商家。
简要介绍:美团网包括餐厅、酒吧、K....
小伙伴喜欢自拍的可以来了,有的手机系统再带的照相机应用不支持蓝牙和....
陌陌是一款根据地理方位的挪动交际东西,你能够经过陌陌知道周围的生疏....
360手机助手最新版集合了手机系统综合管理,海量应用轻松下载,升级....
360手机助手集合了手机系统综合管理,海量应用轻松下载,升级手机应....
免费软件,游戏下载,精选安卓手机和平板优秀软件供下载,安卓软件园W....
注意:本软件是在PC(电脑)上安装运行的哦.手机软件下载和安装服务....
提示:本软件下载以后是在电脑上(PC)上面运行的。
安卓软件园联....
商务合作:QQ:
电子邮箱:
安卓软件园手机版(扫描下图二维码下载)

我要回帖

更多关于 witcher 3 的文章

 

随机推荐