GDG Auckland Message Board › Shake an Elf Application Challenge

Shake an Elf Application Challenge

Simon C
Posted Oct 14, 2010 10:12 AM
user 7614279
Auckland, NZ
Post #: 5
Send an Email Post a Greeting
Hey all,
Seeing as Xmas is coming I thought maybe giving you all a Xmas themed sample application that you can modify yourselves.

All the information you need should be on this site http://blog.serendipi... - including links on installing the development environment and getting the code. All the code is commented and I've licensed it so you can modify it yourselves for your own free apps (no paid apps). - License and helpful links are in the main Activity.

As you will see in the above link, I am currently trying to get hold of a prize for the "winner". - So why not have a go. - I'll give a quick intro in the November meetup But the "winner" be decided at the December meetup (2 Dec 2010).

Good luck to you all!!! - Any Questions can either be posted on this discussion thread or as a comment on our blog.

Simon Collings
Solutions Manager
Serendipity IT Ltd
http://serendipityit....


http://blog.serendipi...

Simon C
Posted Nov 2, 2010 9:50 AM
user 7614279
Auckland, NZ
Post #: 9
Send an Email Post a Greeting
The Rules
The Rules/Guidelines for the 'Shake an Elf' challenge are as follows:
1. Your application must contain an image of an Elf
2. The Elf must do something when someone shakes the phone

How will I do it?
All the information about the application is located here http://blog.serendipi... - including a link to the source code ( http://code.google.co... ).

But I'm not an Android developer - can I still do it?
All you need is a working development environment which can be set up easily if you follow this link : http://developer.andr... . Once you are all up and running (downloading and installing 2 or 3 things) you can just download the code example above and you will have a little app that already runs, then you can just tweek it and explore (that's what this is all about).

Why?
The main aim is to share your learning / experiences at the December Meet-up, but there may be a prize - I'll tell you more on Thursday at the meet-up (then post that info here). Also at the end I'd like to get some feedback on how this 'challenge' worked so I can run more if they are wanted.

-NOTE: To claim any Prize you must present your application at the December 2010 Androids in Auckland Meetup! - But anyone can use this code as training to create an app.
Simon C
Posted Nov 9, 2010 2:56 PM
user 7614279
Auckland, NZ
Post #: 10
Send an Email Post a Greeting
What / How to deliver your Elf Application
The due date is 2nd Dec 2010 7:00PM. You will be required to provide via USB or similar:

  • An Android package file (*.apk)
  • Source code so people can learn from what you have done – any issues please let Simon Collings know beforehand
  • Contact details – so we can credit you when we make a list of the entrants
  • If you have uploaded your application to the market place then a link to that would be good too

(If you will not be at the meetup then email the above list to Simon Collings ( simonc at serendipityit. co. nz ) and he can show your app off for you – but you won’t be in the draw for the prize)


Rules – The full set
1. Your application must contain an image of an Elf
2. The Elf must do something when someone shakes the phone
3. Your application must be able to run on Android version 1.6 (i.e. target 1.6 when you are developing)
4. You can use any development tools or language you like including App Inventor, Eclipse, LUA, Adobe Air - so long as you can fulfil all of the other rules
5. Your application must do no evil!
6. The due date is 2nd Dec 2010 7:00PM
7. You must be there in person to deliver your application, demonstrate your application and receive the prize.


On the night (2 Dec 2010)
You can demo your application including learning's from the development process (time allocation will depend on how many entrants) – just be ready to tell everyone how cool your app is!!!

The winner will be picked and the prize will be given (See below for details of the phone / prize!)


Post event
We would like to upload the entries, including APK's / links to the market, source code and contact details to a website, so other people can use your application to learn from. This is optional.

Simon C
Posted Nov 17, 2010 6:29 PM
user 7614279
Auckland, NZ
Post #: 11
Send an Email Post a Greeting
Hey all,
Great news, Telecom have given us a "Motorola Flipout" ( http://bit.ly/bE6V3H... ) for the winner of the "Shake an Elf Application Challenge". The Flipout phone is pictured below:

Features
Android 2.1
3.1MP Camera
WiFi b/g
Flip out keyboard

Get going on your Elf apps!

Just a reminder, the rules etc are posted above - the apps don't have to be brilliant, it's about the experience, growing the number of Android developers in Auckland (& winning the Telecom Android phone)!

Someone will go home with this phone at the end of the meet-up!


Simon C
Posted Dec 3, 2010 12:42 PM
user 7614279
Auckland, NZ
Post #: 12
Send an Email Post a Greeting
For everyones information Tong won the phone with an app that told a story as it went - including waking the elf via shaking the phone and some xmas music to set the theme! nice work!

Honorable mention goes to John who used the Carona system to create his app and added a physics engine to detect when crates hit the elf!!

Great work!!! And a special thanks to Telecom for giving us a new phone as a prize.

See you all in 2011!!

Simon C
Tong
Posted Dec 12, 2010 1:59 PM
user 10593306
Auckland, NZ
Post #: 1
Send an Email Post a Greeting
//Here attached the code. The app is available on Android market. It is called Free Christmas Elf.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.elfshake);

mgr=(SensorManager)this.getSystemService(Context.SENSOR_SERVICE);
if(mgr==null){
throw new UnsupportedOperationException("Sensors not supported");
}

boolean supported = mgr.registerListener(listener,mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
if (!supported) {
mgr.unregisterListener(listener);
throw new UnsupportedOperationException("Accelerometer not supported");
}

treeView = (ImageView) findViewById(R.id.christmastree);
elfImageView = (ImageView) findViewById(R.id.christmastree);

christmas = (TextView) findViewById(R.id.txtChristmas);
christmas.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);

mSwitcher = (ImageSwitcher) findViewById(R.id.switcherelf);

mSwitcher.setFactory(this);


mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));

mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
mSwitcher.setImageResource(mImageIds[0]);

christmas.setText(R.string.Wake);
christmas.bringToFront();
christmas.setVisibility(1);

fadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
fadeIn.setAnimationListener( myFadeInAnimationListener );
fadeOut = AnimationUtils.loadAnimation(this, R.anim.fadeout);
fadeOut.setAnimationListener( myFadeOutAnimationListener );

launchInAnimation();

threshold = Elf_SHAKE_THRESHOLD*Elf_SHAKE_THRESHOLD*SensorManager.GRAVITY_EARTH*SensorManager.GRAVITY_EARTH;

try {
mp = MediaPlayer.create(getBaseContext(),R.raw.snore);
mp.setLooping(true);
mp.start();
} catch (Exception ex) {
mp.release();
mp = null;
}
try {
wishMerryXmas = MediaPlayer.create(getBaseContext(),R.raw.wewishyouamerrychristmas);
wishMerryXmas.start();
} catch (Exception ex) {
wishMerryXmas.release();
wishMerryXmas = null;
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
isTouching = true;
long now=System.currentTimeMillis();
long timegap;
timegap = now-lastShakeTimestamp;
if (timegap > Elf_SHAKE_GAP || FirstShakeFlag == true) {
elfAction(false, isTouching);
}
return true;
}

private SensorEventListener listener=new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {

if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
long now=System.currentTimeMillis();
long timegap;
timegap = now-lastShakeTimestamp;
if (timegap> Elf_SHAKE_GAP || FirstShakeFlag == true) {
double netForce=e.values[0]*e.values[0];
netForce+=e.values[1]*e.values[1];
netForce+=e.values[2]*e.values[2];
if (netForce>threshold) {
isShaking = true;
elfAction(isShaking, false);
}
}
}

}

public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};

private Runnable runnable = new Runnable() {
public void run() {
handler.postDelayed(this, 1000);
myslideshow();
}
};

private void myslideshow()
{
if (PicPosition <mImageIds.length){
FirstShakeFlag = false;
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mSwitcher.setImageResource(mImageIds[PicPosition]);
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
if (PicPosition==1){
christmas.setText("");
} else if (PicPosition==4 ){
try {
if(mp.isPlaying()==true){
mp.stop();
}
mp = MediaPlayer.create(getBaseContext(),R.raw.yawn);
mp.setLooping(false);
mp.start();
} catch (Exception ex) {
mp.release();
mp = null;
}

}else if (PicPosition==5 ){
christmas.setText(R.string.Christmas);
try{
if(mp.isPlaying()==true){
mp.stop();
}
}catch(Exception ex) {
}

}else if (PicPosition==7 ){

try {
mp = MediaPlayer.create(getBaseContext(),R.raw.babygigl);
mp.setLooping(false);
mp.start();
} catch (Exception ex) {
mp.release();
}
}
PicPosition++;


} else if (PicPosition ==mImageIds.length){
try{
if(mp.isPlaying()==true){
mp.stop();
}
}catch(Exception ex) {
}
christmas.setText(R.string.Shake);
SecondShakeFlag=true;
}
}


private Runnable runnable2 = new Runnable() {
public void run() {

handler.postDelayed(this,3000);
runsecondHalf();
}
};

private void runsecondHalf(){
if (SecondHalfFlag==1 ){
PicPosition++;
christmas.setText(R.string.Christmas);
SecondHalfFlag=2;
mSwitcher.setVisibility(View.GONE);

elfImageView.setBackgroundResource(R.drawable.elf9);
Animation elfImageAnimation = AnimationUtils.loadAnimation(this, R.anim.elf_rotate_anim);
elfImageView.startAnimation(elfImageAnimation);
}else if (SecondHalfFlag ==2){

elfImageView.setBackgroundResource(R.drawable.elf7);
BounceInterpolator bi = new BounceInterpolator();
Animation elfImageAnimation = AnimationUtils.loadAnimation(this, R.anim.elf_fall_anim);
elfImageAnimation.setInterpolator(bi);
elfImageView.startAnimation(elfImageAnimation);
SecondHalfFlag=3;
}
else if (SecondHalfFlag ==3){
christmas.setText(R.string.Light);
elfImageView.setBackgroundResource(R.drawable.tree);
Animation elfImageAnimation = AnimationUtils.loadAnimation(this, R.anim.elf_rise_anim);
elfImageAnimation.setDuration((long) (Math.random() * 3000));
elfImageView.startAnimation(elfImageAnimation);
ThirdHalfFlag =1;
ThirdShakeFlag = true;
SecondShakeFlag = false;
SecondHalfFlag=4;
}
}

private void ThirdPart(){
elfImageView.setBackgroundResource(R.drawable.treeflashing);
AnimationDrawable treeAnimation = (AnimationDrawable)elfImageView.getBackground() ;
treeAnimation.start();
christmas.setText(R.string.Touch);
ThirdHalfFlag++;
TheEndFlag=1;
//FourthShakeFlag = true;
}


private void TheEnd(){
if(TheEndFlag==1){
setContentView(R.layout.end);
}
}
...
Tong
Posted Dec 12, 2010 2:03 PM
user 10593306
Auckland, NZ
Post #: 2
Send an Email Post a Greeting
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return i;
}

private void elfAction(boolean Shaking, boolean Touching) {
boolean currentShaking, currentTouching;
currentShaking = Shaking;
currentTouching = Touching;
if (currentShaking == true || currentTouching == true) {
long curTime = System.currentTimeMillis();
long diffTime = (curTime - lastShakeTimestamp);
if (FirstShakeFlag == true ){
// Log.i("diffTime", "diffTime: "+diffTime);
lastShakeTimestamp = curTime+4000;
runnable.run();
} else if (SecondShakeFlag ==true ){
lastShakeTimestamp = curTime;
SecondShakeFlag= false;
runnable2.run();
} else if (ThirdShakeFlag ==true && diffTime>3000 ){
ThirdShakeFlag=false;
lastShakeTimestamp = curTime;
ThirdPart();
} else if (TheEndFlag >0 && diffTime>2000 ){
lastShakeTimestamp = curTime;
TheEnd();
}
}
}

@Override
protected void onPause() {
super.onPause();
try{
if(mp!=null){
if(mp.isPlaying()==true){
mp.stop();
}
// mp=null;
mp.release();
}


if( wishMerryXmas!= null){
if( wishMerryXmas.isPlaying()==true){
wishMerryXmas.stop();
}
wishMerryXmas.release();
// wishMerryXmas=null;
}

}catch (Exception e){

}
handler.removeCallbacks(runnable);
handler.removeCallbacks(runnable2);
// finish();
}


@Override
protected void onResume() {
super.onResume();
try{
if (mgr != null) {
mgr.registerListener(listener, mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
}catch (Exception e){

}

}

@Override
protected void onStop() {
super.onStop();

try{
if (mgr != null) {
mgr.unregisterListener(listener);
}
}catch (Exception e){

}
finish();
}



public void launchInAnimation() {
christmas.startAnimation(fadeIn);
}

public void launchOutAnimation() {
christmas.startAnimation(fadeOut);
}

private class LocalFadeInAnimationListener implements AnimationListener {
public void onAnimationEnd(Animation animation) {
christmas.post(mLaunchFadeOutAnimation);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
};

private class LocalFadeOutAnimationListener implements AnimationListener {
public void onAnimationEnd(Animation animation) {
christmas.post(mLaunchFadeInAnimation);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
};

private LocalFadeInAnimationListener myFadeInAnimationListener = new LocalFadeInAnimationListener();
private LocalFadeOutAnimationListener myFadeOutAnimationListener = new LocalFadeOutAnimationListener();

private Runnable mLaunchFadeOutAnimation = new Runnable() {
public void run() {
launchOutAnimation();
}
};

private Runnable mLaunchFadeInAnimation = new Runnable() {
public void run() {
launchInAnimation();
}
};


@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
Powered by mvnForum

Offer a perk for our members and get exposure.

Offer a perk →
People in this
Meetup are also in:

Log in

Not registered with us yet?

or
Log in using Facebook

Sign up

Meetup members, Log in

or
Sign up using Facebook
By clicking the "Sign up using Facebook" or "Sign up" buttons above, you agree to Meetup's Terms of Service