A new feature called DayDream is introduced in Android. The Daydream is basically an interactive screensaver. When an Android device is left idle while charging or when is in a dock mode then screen-saver automatically get activated only if you have your device's Daydreaming option ON.
How to enable Daydreaming in your android device:-
Settings > Display > DayDream option and then select the daydream you want to display.
You can create your own Screen saver , like if you want to display your name or want to animate your image then the best option is to develop your own Daydreaming application according to your desire or requirements.
A basic example is given here just to lay your foundation and provide you the basic idea for how to create customised Daydream.
Step(1): Manifest File
First Step is to add a service in the manifest file
<service android:name=".MyDreamService" android:exported="true" android:label="DreamingDemo">
<intent-filter>
<action android:name="android.service.dreams.DreamService"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
Step(2): Code Implementation
Second Step is to implement the code to create the Screen saver
(a). Creating the Service by extending "DreamService" class:
package com.example.mydaydreamdemo;
import android.service.dreams.DreamService;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MyDreamService extends DreamService {
@Override
public void onDreamingStarted() {
super.onDreamingStarted();
/for animating the widgets placed on it.
final Bouncer bouncer = new Bouncer(this);
bouncer.setLayoutParams(new
ViewGroup.LayoutParams(100, 100));
bouncer.setSpeed(300); //sets the speed of your widgets on the screensaver
// Add some views that will be bounced around.
for (int i=0; i<5; i++) {
final FrameLayout.LayoutParams lp
= new FrameLayout.LayoutParams(100, 100);
final ImageView image = new ImageView(this);
image.setImageResource(R.drawable.bubbles);
image.setBackgroundColor(000000);
bouncer.addView(image, lp);
}
setContentView(bouncer);
}
}
Creating the bouncer class to animate the views
(b). Next part of this is to create the bouncer class that will implement the TimeListener Interface of TimeAnimator class
package com.example.mydaydreamdemo;
import android.animation.TimeAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
public class Bouncer extends FrameLayout implements TimeAnimator.TimeListener {
private float myMaxSpeed;
private final TimeAnimator mAnimator;
private int myWidth, myHeight;
public Bouncer(Context context) {
this(context, null);
}
public Bouncer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Bouncer(Context context, AttributeSet attrs, int flags) {
super(context, attrs, flags);
mAnimator = new TimeAnimator();
mAnimator.setTimeListener(this);
}
/**
so that as soon as we are on the screen , bouncing of view starts
*/
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mAnimator.start();
}
/**
* animations Stops with this
*/
@Override
public void onDetachedFromWindow() {
mAnimator.cancel();
super.onDetachedFromWindow();
}
/**
* toplace the view on the screen
*/
@Override
public void addView(View v, ViewGroup.LayoutParams lp) {
super.addView(v, lp);
setupView(v);
}
/**
* Reposit all its child when the size of container change .
*/
@Override
protected void onSizeChanged (int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(width, height, oldWidth, oldHeight);
myWidth = width;
myHeight = height;
for (int i=0; i<getChildCount(); i++) {
setupView(getChildAt(i));
}
}
/**
* view setup for bouncing: random placement, random velocity.
*/
private void setupView(View v) {
final PointF p = new PointF();
final float a = (float) (Math.random()*360);
p.x = myMaxSpeed * (float)(Math.cos(a));
p.y = myMaxSpeed * (float)(Math.sin(a));
v.setTag(p);
v.setX((float) (Math.random() * (myWidth - v.getWidth())));
v.setY((float) (Math.random() * (myHeight - v.getHeight())));
}
/**
* Every TimeAnimator frame, nudge each bouncing view along.
*/
public void onTimeUpdate(TimeAnimator animation, long elapsed, long dt_ms) {
final float dt = dt_ms / 1000f; // seconds
for (int i=0; i<getChildCount(); i++) {
final View view = getChildAt(i);
final PointF v = (PointF) view.getTag();
// step view for velocity * time
view.setX(view.getX() + v.x * dt);
view.setY(view.getY() + v.y * dt);
// handle reflections
final float l = view.getX();
final float t = view.getY();
final float r = l + view.getWidth();
final float b = t + view.getHeight();
boolean flipX = false, flipY = false;
if (r > myWidth) {
view.setX(view.getX() - 2 * (r - myWidth));
flipX = true;
} else if (l < 0) {
view.setX(-l);
flipX = true;
}
if (b > myHeight) {
view.setY(view.getY() - 2 * (b - myHeight));
flipY = true;
} else if (t < 0) {
view.setY(-t);
flipY = true;
}
if (flipX) v.x *= -1;
if (flipY) v.y *= -1;
}
}
public void setSpeed(float s) {
myMaxSpeed = s;
}
}
0 Comment(s)