Java tutorial
//package com.java2s; import android.os.AsyncTask; import android.os.Build; import android.view.View; import java.util.Date; public class Main { private static int mColor; private static void smoothToOrigin(final View view) { Date firstDate = new Date(); final long firstTime = firstDate.getTime(); executeAsyncTask(new AsyncTask<Void, Integer, Void>() { int n = 1, t = 4000; boolean increaseN; @Override protected Void doInBackground(Void... params) { while (!isCancelled()) { Date currentDate = new Date(); long diffTime = currentDate.getTime() - firstTime; double y = getCosY(diffTime); int alpha = (int) (y * 255); int resultColor = setAlphaComponent(mColor, alpha); if (alpha < 0.038 * 255) { publishProgress(0); this.cancel(true); return null; } publishProgress(resultColor, alpha); try { Thread.sleep(38); } catch (InterruptedException e) { e.printStackTrace(); } } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); view.setBackgroundColor(values[0]); } }); } @SafeVarargs private static <Params, Progress, Result> void executeAsyncTask(AsyncTask<Params, Progress, Result> task, Params... params) { if (Build.VERSION.SDK_INT >= 11) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); } else { task.execute(params); } } private static double getCosY(long diffTime) { return 0.5f * Math.cos(3 * diffTime / 1000.0f) + 0.5; } public static int setAlphaComponent(int color, int alpha) { if (alpha < 0 || alpha > 255) { throw new IllegalArgumentException("alpha must be between 0 and 255."); } return (color & 0x00ffffff) | (alpha << 24); } }