Android 中textSwitcher与imageSwitcher的使用
来源:互联网 发布:收买男生室友 知乎 编辑:程序博客网 时间:2024/04/28 16:10
TextSwitcher的使用,它和ImageSwitcher非常类似,都是继承ViewSwitcher,只是TextSwitcher用于切换文本,我们可以设置进入和退出的动画,使其设置文字和图片更加平滑,增加用户体验。
这里我们写了一个按钮来切换文字和图片的例子:
效果图如下:
xml布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.textswitcherdemo.MainActivity"> <TextSwitcher android:id="@+id/textSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="match_parent" android:layout_height="200dp" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="改变文字" /></LinearLayout>
activity中:
package com.example.administrator.textswitcherdemo;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Gravity;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn; private TextSwitcher textSwitcher; private int i = 0; private String[] strings = {"社会王", "我的加特林呢", "哒哒哒哒哒哒", "冒蓝光的"}; private int[] ints = {R.drawable.iv1, R.drawable.iv2, R.drawable.iv3, R.drawable.iv4}; private ImageSwitcher imageSwitcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 设置转换时的淡入和淡出动画效果(可选) Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_in); Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher); imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); textSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { TextView textView = new TextView(MainActivity.this); //设置文字居中显示 textView.setGravity(Gravity.CENTER); //设置字体大小 textView.setTextSize(36); //设置字体的颜色 textView.setTextColor(Color.GREEN); return textView; } }); textSwitcher.setInAnimation(inAnimation); textSwitcher.setOutAnimation(outAnimation); imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(R.drawable.iv2); return imageView; } }); imageSwitcher.setInAnimation(inAnimation); imageSwitcher.setOutAnimation(outAnimation); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn: if (i < 4) { textSwitcher.setText(strings[i]); imageSwitcher.setImageResource(ints[i]); } i++; break; } }}
当然我么也可以使用imageSwitcher进行图片的左右切换,来看效果
/** * 一个左右滑动浏览图片的Demo * */public class ImageSwicherDemoActivity extends Activity implements ViewFactory, OnTouchListener { private ImageSwitcher imageSwicher; // 图片数组 private int[] arrayPictures = { R.drawable.iv1, R.drawable.iv2, R.drawable.iv3, R.drawable.iv4 }; // 要显示的图片在图片数组中的Index private int pictureIndex; // 左右滑动时手指按下的X坐标 private float touchDownX; // 左右滑动时手指松开的X坐标 private float touchUpX; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_swicher); imageSwicher = (ImageSwitcher) findViewById(R.id.imageSwitcher); // 为ImageSwicher设置Factory,用来为ImageSwicher制造ImageView imageSwicher.setFactory(this); // 设置ImageSwitcher左右滑动事件 imageSwicher.setOnTouchListener(this); } @Override public View makeView() { ImageView imageView = new ImageView(this); imageView.setImageResource(arrayPictures[pictureIndex]); return imageView; } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // 取得左右滑动时手指按下的X坐标 touchDownX = event.getX(); return true; } 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]); } return true; } return false; }}
效果图如下:
demo下载地址
http://download.csdn.net/detail/afanbaby/9916546
本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!
阅读全文
1 0
- Android 中textSwitcher与imageSwitcher的使用
- Android之TextSwitcher----ImageSwitcher
- Android ViewSwitcher、TextSwitcher、ImageSwitcher
- Android使用TextSwitcher和ImageSwitcher实现平滑过渡
- Android中TextSwitcher的功能与用法
- Android----ViewFlipper,ViewSwitcher(ImageSwitcher,TextSwitcher)
- ImageSwitcher和TextSwitcher使用笔记
- Android中Gallery和ImageSwitcher的使用
- Android中Gallery和ImageSwitcher的使用
- android viewanimator viewfilter viewswitcher textswitcher imageswitcher
- 使用TextSwitcher和ImageSwitcher实现平滑过渡
- 使用TextSwitcher和ImageSwitcher实现平滑过渡
- 使用TextSwitcher 和ImageSwitcher实现平滑过渡
- android ImageSwitcher的使用
- android ImageSwitcher的使用
- Android零基础入门第55节:ImageSwitcher和TextSwitcher使用
- Android ImageSwitcher、TextSwitcher以及以比较节省资源的方式显示jpg图片小例子
- Android仿淘宝商品自动滚动 ImageSwitcher和TextSwitcher的用法
- JDBC基本知识
- maven profile多环境配置
- Guava学习笔记一:Google Guava 类库简介
- python-单例模式
- Laravel中间件
- Android 中textSwitcher与imageSwitcher的使用
- select服务器
- 错误: HTTPS请求报错Error Code=-999 "cancelled"
- SCP 命令详解
- 英语音标学习
- 文章标题
- JSP九大内置对象
- 【NYOJ-96】 n-1位数
- Maven实战(三)--Pom.xml详解