Android Open Source - Android-Sessions Rainbow Text View






From Project

Back to project page Android-Sessions.

License

The source code is released under:

MIT License

If you think the Android project Android-Sessions listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.ac.icappsoc.appsoctwo.rainbowtv;
/*  w w  w  .java 2  s  .  c o m*/
import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

/** An excited TextView. */
public class RainbowTextView extends TextView {
  
  // One way to generate random numbers in Java - we'll use this for random color generation
  private Random random = new Random();
  
  // Provide the appropriate constructor
  public RainbowTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // I'm red when I begin, no matter what you tell me to do!
    setTextColor(Color.RED);
  }
  
  // Called whenever the system is ready to draw us!
  @Override
  public void onDraw(Canvas canvas){
    // Do the default drawing
    super.onDraw(canvas);
    
    // Notify LogCat, in case anyone's interested in seeing how often this is called
    Log.d("RainbowTextView", "onDraw()");
    
    // Change the text color to something random!
    setTextColor(Color.argb(255, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
    
    // Tell Android system to draw us again, as soon as possible!
    // This will result in onDraw() being called again.. and we get (random) animation!
    invalidate();
  }
  
  /** Called when any sort of touch event takes place on our custom view! */
  @Override
  public boolean onTouchEvent(MotionEvent event){
    // Figure out what sort of touch event this was..
    switch(event.getActionMasked()){
    case MotionEvent.ACTION_DOWN:
      // Finger put down on View, i.e. touch just started
      setText("I was touched!");
      return true;
    case MotionEvent.ACTION_UP:
      // Finger lifted from View, i.e. touch just ended
      setText("Hello World!");
      return true;
    case MotionEvent.ACTION_MOVE:
      // Finger moved on View, i.e. drag
      setTextColor(Color.argb(255, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
      return true;
    }
    
    // If we didn't find anything we want, just use the default behavior
    return super.onTouchEvent(event);
  }

}




Java Source Code List

uk.ac.icappsoc.appsocone.MainActivity.java
uk.ac.icappsoc.appsocthree.MainActivity.java
uk.ac.icappsoc.appsocthree.SensorListActivity.java
uk.ac.icappsoc.appsocthree.accel.BouncyBallView.java
uk.ac.icappsoc.appsocthree.accel.Gravity2DActivity.java
uk.ac.icappsoc.appsocthree.accel.ShakeActivity.java
uk.ac.icappsoc.appsocthree.compass.CompassActivity.java
uk.ac.icappsoc.appsocthree.compass.CompassView.java
uk.ac.icappsoc.appsocthree.light.LightClockActivity.java
uk.ac.icappsoc.appsocthree.proximity.ProximityActivity.java
uk.ac.icappsoc.appsoctwo.MainActivity.java
uk.ac.icappsoc.appsoctwo.circleimage.CircleImageActivity.java
uk.ac.icappsoc.appsoctwo.circleimage.CircleImageView.java
uk.ac.icappsoc.appsoctwo.circleimage.SessionTitleActivity.java
uk.ac.icappsoc.appsoctwo.rainbowtv.RainbowTextViewActivity.java
uk.ac.icappsoc.appsoctwo.rainbowtv.RainbowTextView.java
uk.ac.icappsoc.appsoctwo.split.SplitActivity.java
uk.ac.icappsoc.appsoctwo.split.SplitGameView.java