TextSwitcher,ImageSwitcher切换器
2014-06-19 22:29
274 查看
TextSwitcher和ImageSwitcher可以用来实现文本或图片的切换功能。
这两个组件使用上基本类似,先来看TextSwitcher的使用。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextSwitcher>
</LinearLayout>
通过程序来动态建立切换器中显示文字的TextView。
如果想改为ImageSwitcher,只需要将设置的内容变为图片,建立的组件变为ImageView即可。
这两个组件使用上基本类似,先来看TextSwitcher的使用。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextSwitcher>
</LinearLayout>
通过程序来动态建立切换器中显示文字的TextView。
public class MainActivity extends Activity { private TextSwitcher switcher; // 准备一组要显示的文字 private String[] allTexts = { "Specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.Summary[Expand", "]Inherited XML AttributesFrom class android.widget.ViewAnimatorFrom class android.widget.FrameLayoutFrom", " class android.view.ViewGroupFrom class android.view.View[Expand]Inherited ConstantsFrom class android.", "view.ViewGroupFrom class android.view.View[Expand]Inherited FieldsFrom class android.view.ViewPublic", " ConstructorsTextSwitcher(Context context)Creates a new empty TextSwitcher.TextSwitcher(Context context", ", AttributeSet attrs)Creates a new empty TextSwitcher for the given context and with the specified set ", "attributes.Public Methodsvoid addView(View child, int index, ViewGroup.LayoutParams params)Adds a child", " view with the specified layout parameters.void onInitializeAccessibilityEvent(AccessibilityEvent " }; // 准备当前要显示的索引 private int index = 0; // 保存按下时的坐标和时间 private float startX; private long startTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Globals.init(this); // 设置默认使用的布局文件 setContentView(R.layout.activity_main); switcher = (TextSwitcher) findViewById(R.id.switcher); // 设置建立TextView的工厂类 switcher.setFactory(new ViewFactory() { @Override public View makeView() { // 建立要显示文字的TextView TextView text = new TextView(MainActivity.this); text.setTextColor(Color.BLACK); text.setBackgroundColor(Color.WHITE); text.setTextSize(16); text.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return text; } }); // 处理内容 switcher.setText(allTexts[index]); // 设置动画效果, 系统动画功能不完善,一般都满足不了开发的需求, 因此这里完成一个自定义的平移动画 // switcher.setInAnimation(AnimationUtils.loadAnimation(this, // android.R.anim.slide_in_left)); // switcher.setOutAnimation(AnimationUtils.loadAnimation(this, // android.R.anim.slide_out_right)); // 加入触屏监听, 使用该监听的前提是, 组件必须先加入过OnClickListener switcher.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); switcher.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // 只支持单点操作 if (event.getPointerCount() == 1) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // 手指刚按到屏幕上 startX = event.getX(); startTime = System.currentTimeMillis(); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { // 手指在屏幕上滑动 } else if (event.getAction() == MotionEvent.ACTION_UP) { // 手指从屏幕上离开 // 先判断时间是否合法, 要求在1秒内完成滑动的动作 if (System.currentTimeMillis() - startTime < 1000) { // 判断当前的横坐标 if (startX - event.getX() >= 50) { // 下一页 if (index < allTexts.length - 1) { index++; Animation inAnim = new TranslateAnimation( Globals.SCREEN_WIDTH + 10, 0, 0, 0); Animation outAnim = new TranslateAnimation( 0, -Globals.SCREEN_WIDTH - 10, 0, 0); // 设置动画持续时间 inAnim.setDuration(1000); outAnim.setDuration(1000); switcher.setInAnimation(inAnim); switcher.setOutAnimation(outAnim); switcher.setText(allTexts[index]); } } else if (event.getX() - startX >= 50) { // 上一页 if (index > 0) { index--; Animation inAnim = new TranslateAnimation( -Globals.SCREEN_WIDTH - 10, 0, 0, 0); Animation outAnim = new TranslateAnimation( 0, Globals.SCREEN_WIDTH + 10, 0, 0); // 设置动画持续时间 inAnim.setDuration(1000); outAnim.setDuration(1000); switcher.setInAnimation(inAnim); switcher.setOutAnimation(outAnim); switcher.setText(allTexts[index]); } } } } } else { Toast.makeText(MainActivity.this, "只支持单点操作", Toast.LENGTH_SHORT).show(); } return false; } }); } }
如果想改为ImageSwitcher,只需要将设置的内容变为图片,建立的组件变为ImageView即可。
相关文章推荐
- android控件17 TextSwitcher
- Android_TextSwitcher和ImageSwitcher
- Android 上下滚动TextSwitcher实例详解
- AndroidUI组件之TextSwitcher
- ImageSwitcher和TextSwitcher使用笔记
- TextSwitcherProject
- Android中TextSwitcher的功能与用法
- <转>Android 中文 API (17) —— TextSwitcher
- Android学习指南之四十五:用户界面View之ImageSwitcher 和TextSwitcher
- 二、Android应用的界面编程(七)ViewAnimator及其子类[ ViewSwitcher、ImageSwitcher、TextSwitcher、ViewFlipper ]
- Android控件之ImageSwitcher_图片切换和TextSwitcher_文字切换
- Androi UI 详解之ViewAnimator(ViewSwitcher,ImageSwitcher,TextSwitcher,ViewFlipper)
- TextSwitcher实现文字上下翻牌效果
- TextSwitcher文本选择器实现走马灯效果,及Spanny,SpannableStringBuilder和SpannalbeString的应用
- Android7.0中文文档(API)-- TextSwitcher
- TextSwitcher使用示例
- 一个有故事的TextSwitcher
- TextSwitcher,译为文字转换器控件
- TextSwitcher--文本切换器
- Android ImageSwitcher、TextSwitcher以及以比较节省资源的方式显示jpg图片小例子