Android仿淘寶頭條基于TextView實現(xiàn)上下滾動通知效果
最近有個項目需要實現(xiàn)通知欄的上下滾動效果,仿淘寶頭條的那種。
我從網(wǎng)上看了一些代碼,把完整的效果做了出來。如圖所示:

具體代碼片段如下:
1.在res文件夾下新建anmin文件夾,在這個文件夾里創(chuàng)建兩個文件
(1).anim_marquee_in.xml進(jìn)入時動畫
<?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實現(xiàn)上下滾動通知效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android自定義ViewGroup實現(xiàn)堆疊頭像的點贊Layout
這篇文章主要介紹了 Android自定義ViewGroup實現(xiàn)堆疊頭像的點贊Layout,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android編程實現(xiàn)類似天氣預(yù)報圖文字幕垂直滾動效果的方法
這篇文章主要介紹了Android編程實現(xiàn)類似天氣預(yù)報圖文字幕垂直滾動效果的方法,涉及Android基于布局及事件響應(yīng)實現(xiàn)圖文滾動效果的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
android教程使用webview訪問https的url處理sslerror示例
這篇文章主要介紹了android教程使用webview訪問https的url處理sslerror示例,大家參考使用吧2014-01-01
Android 實現(xiàn)調(diào)用系統(tǒng)照相機(jī)拍照和錄像的功能
這篇文章主要介紹了Android 實現(xiàn)調(diào)用系統(tǒng)照相機(jī)拍照和錄像的功能的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android 通過自定義view實現(xiàn)水波紋效果案例詳解
這篇文章主要介紹了Android 通過自定義view實現(xiàn)水波紋效果案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

