Skip to content

Instantly share code, notes, and snippets.

/**
* Shifts all tiles to the left, merging consecutive tiles as necessary.
*/
public void moveLeft() {
if (mIsGameOver) {
return;
}
final int oldScore = mCurrentScore;
boolean moved = false;
@alexjlockwood
alexjlockwood / gist:febcf171605d83e64207
Created July 14, 2014 01:58
Adjacent Fibonacci #s
public boolean canMerge(int t1, int t2) {
final int lowFib = Math.min(t1, t2);
final int highFib = Math.max(t1, t2);
int a = 1, b = 1;
while (b < highFib) {
int temp = b;
b = a + b;
a = temp;
}
@alexjlockwood
alexjlockwood / ActivityTransitionCoordinator.java
Last active August 29, 2015 14:08
ActivityTransitionCoordinator.java
package android.app;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.os.ResultReceiver;
public class TextSizeTransition extends Transition {
private static final String PROPNAME_TEXT_SIZE = "propname_text_size";
private static final String[] TRANSITION_PROPERTIES = { PROPNAME_TEXT_SIZE };
private static final Property<TextView, Float> TEXT_SIZE =
new Property<TextView, Float>(Float.class, "textSize") {
@Override
public Float get(TextView textView) {
return textView.getTextSize();
public class TransitionActivity extends Activity implements View.OnClickListener {
private static final Transition TRANSITION = new Fade();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRootView = findViewById(R.id.root_view);
mRootView.setOnClickListener(this);
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@alexjlockwood
alexjlockwood / SingletonDatabase.java
Created May 23, 2012 01:18
Singleton Database #1
public class MainApplication extends Application {
/**
* see NotePad tutorial for an example implementation of DataDbAdapter
*/
private static DataDbAdapter mDbHelper;
/**
* Called when the application is starting, before any other
* application objects have been created. Implementations
@alexjlockwood
alexjlockwood / SingletonDatabase.java
Created May 23, 2012 01:07
Singleton Database #2
/**
* create custom DatabaseHelper class that extends SQLiteOpenHelper
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper mInstance = null;
private static final String DATABASE_NAME = "databaseName";
private static final String DATABASE_TABLE = "tableName";
private static final int DATABASE_VERSION = 1;
@alexjlockwood
alexjlockwood / MyFragment.java
Created May 24, 2012 19:20
newInstance() fragment instantiation #2
public class MyFragment {
/**
* Make the default constructor private to prevent direct
* instantiation. Now all instances of this class must be
* instantiated with a call to newInstance().
*/
private MyFragment() { }
/**
@alexjlockwood
alexjlockwood / Singleton.java
Created May 24, 2012 19:26
newInstance fragment instantiation #1
public static class Singleton {
private static final Singleton instance = null;
/**
* Make the class private to prevent direct instantiation.
* this forces clients to call newInstance(), which will
* ensure the class' Singleton property.
*/
private Singleton() { }