您的位置:首页 > 移动开发 > Android开发

Android TextSwitche 上下滚动的广告条 小喇叭功能

2017-10-24 16:51 274 查看
有些应用里面会有一些上下滚动文字的广告,尤其是关于商城里面的容易出现,具体的效果相比大家也见过


先看图



目录结构



布局文件

activity.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
tools:context="com.example.textswitcher.MainActivity">

<ImageView
android:id="@+id/img_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/notices" />

<TextSwitcher
android:id="@+id/tv_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="12sp" />
</LinearLayout>


动画文件放在anim下

enter_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false"
android:zAdjustment="top"
>
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>


leave_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false"
android:zAdjustment="top"
>
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="-100%p" />
</set>


代码

MainActivity.class

package com.example.textswitcher;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {
private ImageView img_notice;
private AnimationDrawable animationDrawable;
private TextSwitcher tv_notice;
private String[] mAdvertisements ;
private final int HOME_AD_RESULT = 1;
private int mSwitcherCount=0;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
// 广告
case HOME_AD_RESULT:
tv_notice.setText(mAdvertisements[mSwitcherCount % mAdvertisements.length]);
mSwitcherCount++;
mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3000);
break;
}

}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

private void initView() {
img_notice = (ImageView)findViewById(R.id.img_notice);
img_notice.setImageResource(R.drawable.notices);
animationDrawable = (AnimationDrawable)img_notice.getDrawable();
animationDrawable.start();
tv_notice = (TextSwitcher) findViewById(R.id.tv_notice);
tv_notice.setFactory(new ViewSwitcher.ViewFactory() {
// 这里用来创建内部的视图,这里创建TextView,用来显示文字
public View makeView() {
TextView tv = new TextView(getApplicationContext());
// 设置文字的显示单位以及文字的大小
// tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources()
//        .getDimension(R.dimen.font_size));
tv.setTextColor(getResources().getColor(R.color.black));
return tv;
}
});
tv_notice.setInAnimation(getApplicationContext(),
R.anim.enter_bottom);
tv_notice.setOutAnimation(getApplicationContext(), R.anim.leave_top);
mAdvertisements = new String[] { "诺基亚回归手机市场,是在骗粉丝情怀吗?","共享单车寒流:小鹿退出北京 摩拜ofo陷合并绯闻","热点城市新房价格两月停涨 三四线城市或成重点" };
mHandler.sendEmptyMessage(HOME_AD_RESULT);
}
}


源码下载
http://download.csdn.net/download/qq_31939617/10037839
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: