上下轮播控件TextSwitcher

见个京东的快报效果吧,实现起来比较简单:需要用到android 原生控件:TextSwitcher

avctivity_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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.textswitcher.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextSwitcher
        android:id="@+id/ts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

slide_bootom_out.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>  


slide_top_in.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>


java 代码:

package com.example.textswitcher;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextSwitcher ts = (TextSwitcher) findViewById(R.id.ts);
        ts.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                final TextView tv = new TextView(MainActivity.this);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                tv.setPadding(30, 30, 30, 30);
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Snackbar.make(v, tv.getText(), Snackbar.LENGTH_SHORT).show();
                    }
                });
                return tv;
            }
        });


        ts.setInAnimation(this, R.anim.slide_top_in);
        ts.setOutAnimation(this, R.anim.slide_bootom_out);

        final String[] titles = {"苹果3 199", "苹果4 299", "苹果4s 399", "苹果5 499", "苹果5s 599", "苹果6 699"};
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ts.setText(String.format("京东快报:%s", titles[new Random().nextInt(titles.length)]));
                handler.postDelayed(this, 1000);
            }
        }, 1000);
    }

}


效果图:

上下轮播控件TextSwitcher_第1张图片


你可能感兴趣的:(上下轮播控件TextSwitcher)