使用ViewFlipper实现广告信息栏的上下翻滚效果
2024-08-29 20:55:42
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.View;
import android.widget.ViewFlipper; public class MainActivity extends AppCompatActivity {
ViewFlipper flipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewFlipper vf = (ViewFlipper) findViewById(R.id.vf);
vf.addView(View.inflate(this, R.layout.guanggao, null));
vf.addView(View.inflate(this, R.layout.guanggao2, null));
vf.addView(View.inflate(this, R.layout.guanggao3, null));
vf.startFlipping();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.loaderman.viewflipperdemo.MainActivity"> <ViewFlipper
android:id="@+id/vf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoStart="true"
android:flipInterval="3000"
android:inAnimation="@anim/anim_marque_in"
android:outAnimation="@anim/anim_marque_out"
/>
</RelativeLayout>
guanggao.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="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="热议"
android:textColor="#f00"
android:textSize="18sp"/> <TextView
android:textSize="18sp" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="小米手机降价了"/>
</LinearLayout>
</LinearLayout>
guanggao2.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="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#00f"
android:text="爆料"/> <TextView
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小米手机降价了"/>
</LinearLayout>
</LinearLayout>
guanggao3.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="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#0f0"
android:text="最新"/> <TextView
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果手机发布了"/>
</LinearLayout>
</LinearLayout>
在res/anim下创建anim_marque_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500">
<!--anim_marque_in-->
<translate
android:fromYDelta="100%p"
android:toYDelta="0"/>
<alpha
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
在res/anim下创建anim_marque_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500">
<!--anim_marque_out-->
<translate
android:fromYDelta="0%p"
android:toYDelta="-100%p"/>
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
效果图:
最新文章
- 简单几句话总结Unicode,UTF-8和UTF-16
- ASP.NET——生成验证码
- LPTHW 笨方法学习python 16章
- 【云计算】mesos生态系统
- Java 调用 Javascript 函数的范例
- 安装Golang 1.6及开发环境
- 最大连续子数和问题-homework-03
- 2015年最棒的10个 JavaScript 框架
- fstream,ifstream,ofstream 详解与用法
- win7下不能收到窗口hook消息的问题
- 经验分享:Xcode 创建.a和framework静态库
- 一道C语言安全编码题目
- Java - 路线图
- ITU-T Technical Paper: NP, QoS 和 QoE的框架以及它们的区别
- sql server创建登录出发器后导致登录失败--解决方案
- 今天圆满了----tensorflow安装日志
- linux系统执行mysql脚本:Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39;
- 二、消息队列之如何在C#中使用RabbitMQ
- 浅析Java源码之Math.random()
- Nuts &; Bolts Problem