Android仿淘宝头条基于TextView实现上下滚动通知效果

所属分类: 软件编程 / Android 阅读数: 30
收藏 0 赞 0 分享

最近有个项目需要实现通知栏的上下滚动效果,仿淘宝头条的那种。

我从网上看了一些代码,把完整的效果做了出来。如图所示:

具体代码片段如下:

1.在res文件夹下新建anmin文件夹,在这个文件夹里创建两个文件

(1).anim_marquee_in.xml进入时动画

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

(2).anim_marquee_out.xml退出时动画

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

2.activity_main.xml

<RelativeLayout 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" 
 tools:context="com.spore.marqueeview.MainActivity" > 
 <ViewFlipper 
  android:id="@+id/marquee_view" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:autoStart="true" 
  android:background="#fff" 
  android:flipInterval="2500" 
  android:inAnimation="@anim/anim_marquee_in" 
  android:outAnimation="@anim/anim_marquee_out" > 
 </ViewFlipper> 
</RelativeLayout> 

3.noticelayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:orientation="horizontal" > 
 <ImageView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:padding="5dp" 
  android:src="@drawable/icon_home_notice" /> 
 <TextView 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:gravity="center_vertical" 
  android:paddingLeft="10dp" 
  android:singleLine="true" 
  android:text="[2017-02-28 08:00]通知:上午九点整开会!" 
  android:textSize="18sp" /> 
</LinearLayout> 

4.MainActivity.java

package com.iponkan.textviewupdown; 
import com.example.textviewupdown.R; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ViewFlipper; 
public class MainActivity extends Activity { 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  // 为ViewFlipper添加广告条 
  ViewFlipper vf = (ViewFlipper) findViewById(R.id.marquee_view); 
  vf.addView(View.inflate(this, R.layout.noticelayout, null)); 
  vf.addView(View.inflate(this, R.layout.noticelayout, null)); 
  vf.addView(View.inflate(this, R.layout.noticelayout, null)); 
 } 
 @Override 
 public boolean onCreateOptionsMenu(Menu menu) { 
  // Inflate the menu; this adds items to the action bar if it is present. 
  getMenuInflater().inflate(R.menu.main, menu); 
  return true; 
 } 
} 

以上所述是小编给大家介绍的Android仿淘宝头条基于TextView实现上下滚动通知效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多