见个京东的快报效果吧,实现起来比较简单:需要用到android 原生控件:TextSwitcher
avctivity_main.xml:
slide_bootom_out.xml
slide_top_in.xml:
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);
}
}
效果图: