温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android基于ViewFilpper如何实现文字LED显示效果

发布时间:2021-06-30 11:47:24 来源:亿速云 阅读:171 作者:小新 栏目:移动开发

小编给大家分享一下Android基于ViewFilpper如何实现文字LED显示效果,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

具体如下:

这里给出来自Android官方API DEMO中动画效果实例。

public class Animation2 extends Activity implements
    AdapterView.OnItemSelectedListener {
  // Spinner数据源
  private String[] mStrings = { "Push up", "Push left", "Cross fade",
      "Hyperspace" };
  // 控件ViewFlipper
  private ViewFlipper mFlipper;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.animation_2);
    // 初始化UI控件
    initViews();
  }
  private void initViews() {
    mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
    mFlipper.startFlipping();
    Spinner s = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, mStrings);
    // 定义Spinner下拉菜单模式
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // 设置数据
    s.setAdapter(adapter);
    // 添加监听
    s.setOnItemSelectedListener(this);
  }
  /**
   * Spinner的item选择监听事件处理
   */
  @Override
  public void onItemSelected(AdapterView<?> parent, View v, int position,
      long id) {
    switch (position) {
    case 0:// 文字从下进入,从上移出,伴随透明度变化
      mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
          R.anim.push_up_in));
      mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
          R.anim.push_up_out));
      break;
    case 1:// 文字从右侧向左进入,从右侧移出,伴随透明度变化
      mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
          R.anim.push_left_in));
      mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
          R.anim.push_left_out));
      break;
    case 2:// 文字透明度改变,从0-1-0
      mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
          android.R.anim.fade_in));
      mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
          android.R.anim.fade_out));
      break;
    default:// 多维空间动画(复合动画效果)
      mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
          R.anim.hyperspace_in));
      mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
          R.anim.hyperspace_out));
      break;
    }
  }
  @Override
  public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub
    // DO NOTHING
  }
}

布局文件,TextView中添加自己想显示的文字

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:padding="10dip" >
  <ViewFlipper
    android:id="@+id/flipper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dip"
    android:flipInterval="2000" >
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:text="@string/animation_2_text_1"
      android:textSize="26sp" />
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:text="@string/animation_2_text_2"
      android:textSize="26sp" />
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:text="@string/animation_2_text_3"
      android:textSize="26sp" />
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:text="@string/animation_2_text_4"
      android:textSize="26sp" />
  </ViewFlipper>
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dip"
    android:text="@string/animation_2_instructions" />
  <Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

动画文件res/anim文件夹下

1. push_up_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="300"<!--动画时长-->
    android:fromYDelta="100%p"<!--Y方向初始位置-->
    android:toYDelta="0" /><!--Y方向动画结束位置-->
  <alpha
    android:duration="300"
    android:fromAlpha="0.0"<!--初始透明度-->
    android:toAlpha="1.0" /><!--动画结束时透明度-->
</set>

2. push_up_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="300"
    android:fromYDelta="0"
    android:toYDelta="-100%p" />
  <alpha
    android:duration="300"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" />
</set>

3. push_left_in.xml

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

4. push_left_out.xml

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

5. fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="@android:integer/config_longAnimTime"
  android:fromAlpha="0.0"
  android:interpolator="@interpolator/decelerate_quad"
  android:toAlpha="1.0" />

6. fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="@android:integer/config_mediumAnimTime"
  android:fromAlpha="1.0"
  android:interpolator="@interpolator/accelerate_quad"<!--设置动画插值器-->
  android:toAlpha="0.0" />

7. hyperspace_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="300"
  android:fromAlpha="0.0"
  android:startOffset="1200"<!--设置启动时间-->
  android:toAlpha="1.0" />

8. hyperspace_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:shareInterpolator="false" >
  <scale
    android:duration="700"
    android:fillAfter="false"<!--动画结束画面是否停留在最后一帧-->
    android:fillEnabled="true"<!--使能填充效果-->
    android:fromXScale="1.0"<!--X方向起始缩放值-->
    android:fromYScale="1.0"<!--Y方向起始缩放值-->
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="50%"<!--动画相对于物件的X、Y坐标的开始位置-->
    android:pivotY="50%"
    android:toXScale="1.4"
    android:toYScale="0.6" />
  <set android:interpolator="@android:anim/accelerate_interpolator" >
    <scale<!--缩放动画-->
      android:duration="400"
      android:fillAfter="true"
      android:fillBefore="false"
      android:fillEnabled="true"
      android:fromXScale="1.4"
      android:fromYScale="0.6"
      android:pivotX="50%"
      android:pivotY="50%"
      android:startOffset="700"
      android:toXScale="0.0"
      android:toYScale="0.0" />
    <rotate<!--旋转动画-->
      android:duration="400"
      android:fillAfter="true"
      android:fillBefore="false"
      android:fillEnabled="true"
      android:fromDegrees="0"
      android:pivotX="50%"
      android:pivotY="50%"
      android:startOffset="700"
      android:toDegrees="-45"
      android:toYScale="0.0" />
  </set>
</set>

看完了这篇文章,相信你对“Android基于ViewFilpper如何实现文字LED显示效果”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI