温馨提示×

温馨提示×

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

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

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

发布时间:2020-09-26 06:54:11 来源:脚本之家 阅读:229 作者:艾妮旭 栏目:移动开发

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

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

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

具体代码片段如下:

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实现上下滚动通知效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

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

AI