android   Android developers web site provide you with 2 predefined animation techniques which you can use in your applications – Tween animation and frame animation. They are super easy to implement and work quite nice.
If you are using Android 3.0 or above, you should check out Property Animation technicqe.
But what if you need to create your own customized animations and you are using older versions? Don’t worry, that is easy to do as well, here is how you do it.


To create a customized animation you need to follow these 3 steps.
For the explanation, we will create a simple animation that changes the background of a button gradually from black to red.

Step 1 – Extend the Animation class and set the properties
Create a class which extends Android Animation class.
This class will hold the logic of your animation.
See my example:

public class BGColorAnimation extends Animation {

	private View view;
	private int currentRedColor;

	//The steps to skip between colors
	private static int STEP_SIZE=30;
	private static int ANIMATION_DURATION=50;

	public BGColorAnimation(View view) {
		this.view=view;
		setDuration(ANIMATION_DURATION);
		setRepeatCount(255/STEP_SIZE);
		setFillAfter(true);
		setInterpolator(new AccelerateInterpolator());

		setAnimationListener(new MyAnimationListener());
	}
}
  • As you see, there is not much in this class since my animation is not that complicated.
  • Notice that I have made all the necessary animation parameters initialization from inside the constructor, but you can defiantly initialize them from outside the class.
  • There are 2 important parameters which determines the behavior of the animation:
    1. RepeatCount – the number of steps this animation has.
    2. Duration – the sleep time between 2 steps.
  • On each step, the animation listener will be triggered.

Step 2 – Implement AnimationListener interface
Create a class which implements Animation.AnimationListener.
As mentioned, it is triggered on each animation step.

class MyNumbersAnimationListener implements AnimationListener{
	    	private int index;

	    	class MyAnimationListener implements AnimationListener{

		@Override
		public void onAnimationEnd(Animation animation) {

		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// Change color of the view
			view.setBackgroundColor(
				Color.rgb(currentRedColor+=STEP_SIZE, 0, 0));
		}

		@Override
		public void onAnimationStart(Animation animation) {
			view.setBackgroundColor(Color.BLACK);
			currentRedColor=0;
		}

	}



In fact, I see no reason why not doing both steps in the same class:

public class BGColorAnimation extends Animation implements
		Animation.AnimationListener {

	private View view;
	private int currentRedColor;

	// The steps to skip between colors
	private static int STEP_SIZE= 30;
	private static int ANIMATION_DURATION = 50;

	public BGColorAnimation(View view) {
		this.view = view;
		setDuration(ANIMATION_DURATION);
		setRepeatCount(255 / STEP_SIZE);
		setFillAfter(true);
		setInterpolator(new AccelerateInterpolator());

		setAnimationListener(this);
	}

	@Override
	public void onAnimationEnd(Animation animation) {

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		view.setBackgroundColor(
			Color.rgb(currentRedColor += STEP_SIZE, 0, 0));
	}

	@Override
	public void onAnimationStart(Animation animation) {
		view.setBackgroundColor(Color.BLACK);
		currentRedColor = 0;
	}

}

Step 3 – Start the animation from a view
Animations are triggered from a view.
Example:

Button button = (Button)findViewById(R.id.b_colors);
button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					button.startAnimation(new BGColorAnimation(button));
				}
			});

zip Download demo project

Tagged with:
 

Well it’s a well known fact that the java native function System.arraycopy() is a useful way to copy one array to another since it is native, but is that also the case for Android? And if so, how much more useful is it?

To answer these questions I have made a simple test and ran it as a java program on my PC and than as an Android activity.

Here is the test for the PC:

 private static final int SIZE_OF_ARRAY = 10000000;
	private static long time;

	public static void main(String[] args) {

		        Integer [] sourceArray = new Integer[SIZE_OF_ARRAY];
		        Integer [] destinationArray = new Integer[SIZE_OF_ARRAY];
		        fillArray(sourceArray);

		        startBenchmark();
		        naiveCopy(sourceArray,destinationArray);
		        stopBenchmark();

		        startBenchmark();
		        System.arraycopy(sourceArray, 0, destinationArray, 0,
						  sourceArray.length);
		        stopBenchmark();
		    }

			private static void naiveCopy(Integer [] src, Integer [] dst) {
				for (int i = 0; i < src.length; i++) {
					dst[i]=src[i];
				}

			}

			private static void fillArray(Integer [] src) {
				for (int i = 0; i < src.length; i++) {
					src[i]=i;
				}

			}

			private static void startBenchmark() {
				time = System.currentTimeMillis();
			}

			private static void stopBenchmark() {
				time = System.currentTimeMillis() - time;
				System.out.println( "time="+time);

			}

Here are the results while running it from my PC (java 7, 8GB memory, CPU intel i5):
Naive algorithm – 14 ms
System.arraycopy(); – 6 ms.

Arraycopy does the task in less than half of the time.

Now to use it on Android – here is the code:

public class ArrayCopyTestActivity extends Activity {
    private static final int SIZE_OF_ARRAY = 1000000;
	private long time;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Integer [] sourceArray = new Integer[SIZE_OF_ARRAY];
        Integer [] dst = new Integer[SIZE_OF_ARRAY];
        fillArray(sourceArray);

        startBenchmark();
        naiveCopy(sourceArray,dst);
        stopBenchmark();

        startBenchmark();
        System.arraycopy(sourceArray, 0, dst, 0, sourceArray.length);
        stopBenchmark();
    }

	private void naiveCopy(Integer [] src, Integer [] dst) {
		for (int i = 0; i < src.length; i++) {
			dst[i]=src[i];
		}

	}

	private void fillArray(Integer [] src) {
		for (int i = 0; i < src.length; i++) {
			src[i]=i;
		}

	}

	private void startBenchmark() {
		time = System.currentTimeMillis();
	}

	private void stopBenchmark() {
		time = System.currentTimeMillis() - time;
		Log.d("array copy test", "time="+time);

	}
}

* Notice I have reduced the size of the Array from 10 million to 1 million, this is due to restrictions on memory for applications in Android.



The results from running it on my device (nexus 1):
Naive algorithm – 182 ms
System.arraycopy(); – 12 ms.

This means that the fact that System.arraycopy() is better than the regular copy is even more true for Android.
In short than, always use System.arraycopy() especially on Android.

Test for PC
Test for Android

Tagged with:
 
android   Linkify is a class that lets you create links from a TextView or a Spannable.
You can create links not just to web pages, but also to locations on the map, emails and even phone numbers.

*Note: the examples bellow may not always work in the emulator.


Web address:

TextView myWebSite = new TextView(this);
myWebSite .setText("http://http://www.dzone.com/");
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);




Phone number:

TextView myPhone = (TextView) findViewById(R.id.my_web_site);
myPhone .setText(“5552323233”);
Linkify.addLinks(myPhone  , Linkify.PHONE_NUMBERS);





Map address:

TextView myLocation = new TextView(this);
myLocation.setText("436 Mayfield Ave, Stanford, CA");
Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES);
mainLayout.addView(myLocation);




Email address:

TextView myEmail= (TextView) findViewById(R.id.my_web_site);
myEmail.setText(“aviyehuda@gmail.com”);
Linkify.addLinks(myEmail  , Linkify.EMAIL_ADDRESSES);





Auto detect:
Use Linkify.ALL to automatically detect the link type.

Linkify.addLinks(myTextView , Linkify.ALL);


More than one option:
You can choose more than a single option for the link type.

Linkify.addLinks(myTextView, Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);


Using pattern:
You can use a regular expression for detecting text parts and transform only them to links instead of the whole text.

 TextView myCustomLink = new TextView(this);
Pattern pattern = Pattern.compile("[a-zA-Z]+&");
myCustomLink.setText("press Linkify& or on Android& to search it on google");
Linkify.addLinks(myCustomLink,pattern, "http://www.google.ie/search?q=");
mainLayout.addView(myCustomLink);




MatchFilter:
MatchFilter is used for more complicated filters.

MatchFilter myMatchFilter = new MatchFilter() {
	@Override
	public boolean acceptMatch(CharSequence cs, int start, int end) {
		return start > 48;
	}
};

TextView myCustomLink2 = new TextView(this);
Pattern pattern2 = Pattern.compile("[a-zA-Z]+");
myCustomLink2.setText("press one of these words to search it on google: Android Linkify dzone");
Linkify.addLinks(myCustomLink2,pattern2, "http://www.google.ie/search?q=", myMatchFilter, null);
mainLayout.addView(myCustomLink2);




TransformFilter:
So fat the text we have filtered stayed the same. But sometimes you need the text to be different than the text which is appended to the link.

  TransformFilter myTransformFilter = new TransformFilter() {
	@Override
	public String transformUrl(Matcher match, String url) {
		return url.substring(1); //remove the $ sign
	}
};

TextView myCustomLink3 = new TextView(this);
Pattern pattern3 = Pattern.compile("\\$[a-zA-Z]+");
myCustomLink3.setText("press $Linkify or on $Android to search it on google");
Linkify.addLinks(myCustomLink3,pattern3, "http://www.google.ie/search?q=", null, myTransformFilter);
mainLayout.addView(myCustomLink3);






Download code example

Tagged with: