Guest User

How to add CrossFade effect in Textview, Android

a guest
Feb 26th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. public class MainActivity extends FragmentActivity {
  2.  
  3.     private TextSwitcher mSwitcher, mSwitcher1;
  4.     private int mCounter = 0;
  5.     String textToShow[] = {
  6.             "Main HeadLine", "Your Message", "New In Technology", "New Articles", "Business News", "What IS New"
  7.     };
  8.     String textToShow1[] = {
  9.             "Main HeadLine", "Your Message", "New In Technology", "New Articles", "Business News", "What IS New"
  10.     };
  11.     int messageCount = textToShow.length;
  12.     // to keep current Index of text
  13.     int currentIndex = -1;
  14.  
  15.     Timer timer = new Timer();
  16.  
  17.     public class MyTimerTask extends TimerTask {
  18.  
  19.         @Override
  20.         public void run() {
  21.             runOnUiThread(new Runnable() {
  22.                
  23.                 @Override
  24.                 public void run() {
  25.                     updateTextView();
  26.                 }
  27.             });
  28.         }
  29.     }
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.example_layout);
  35.  
  36.         mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
  37.         mSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher01);
  38.         // BEGIN_INCLUDE(setup)
  39.         // Set the factory used to create TextViews to switch between.
  40.         mSwitcher.setFactory(mFactory);
  41.         mSwitcher1.setFactory(mFactory);
  42.         Animation in = AnimationUtils.loadAnimation(this,
  43.                 android.R.anim.fade_in);
  44.         Animation out = AnimationUtils.loadAnimation(this,
  45.                 android.R.anim.fade_out);
  46.         mSwitcher.setInAnimation(in);
  47.         mSwitcher1.setInAnimation(in);
  48.         mSwitcher.setOutAnimation(out);
  49.         mSwitcher1.setOutAnimation(out);
  50.  
  51.     }
  52.  
  53.     @Override
  54.     protected void onStart() {
  55.         super.onStart();
  56.         timer.schedule(new MyTimerTask(), 0, 1000);
  57.  
  58.     }
  59.  
  60.     private void updateTextView() {
  61.  
  62.         int maxIndex = textToShow.length;
  63.         Random random = new Random();
  64.         int generatedIndex = random.nextInt(maxIndex);
  65.         mSwitcher.setText(textToShow[generatedIndex]);
  66.         mSwitcher1.setText(textToShow1[generatedIndex]);
  67.     }
  68.  
  69.     private ViewFactory mFactory = new ViewFactory() {
  70.  
  71.         @Override
  72.         public View makeView() {
  73.  
  74.             // Create a new TextView
  75.             TextView t = new TextView(MainActivity.this);
  76.             t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
  77.             t.setTextAppearance(MainActivity.this, android.R.style.TextAppearance_Large);
  78.             return t;
  79.         }
  80.     };
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment