android.support.v13.dreams.BasicDream
android-support-v13.jarの中身を見てたらそういうのがあるっていうのを発見。で色々調べてみたら、なにやらスクリーンセーバーのような物を作れるAPIな模様。という事でやってみた
AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="sample.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 追加 -->
<activity android:name=".SampleDream" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.DREAM" />"
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package sample.test;
import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setComponent(
new ComponentName("sample.test", "sample.test.SampleDream")
);
startActivity(intent);
finish();
}
}
SampleDream.java
package sample.test;
import android.support.v13.dreams.BasicDream;
public class SampleDream extends BasicDream {
@Override
public void onStart() {
super.onStart();
setContentView(new SampleView(this));
}
}
SampleView.java
package sample.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SampleView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "SampleView";
private static final int[] colors = new int[]{
Color.RED,
Color.WHITE,
Color.BLUE,
Color.GREEN,
Color.YELLOW
};
private int width;
private int height;
private SurfaceHolder holder;
public SampleView(Context ctx) {
super(ctx);
holder = getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder2) {
Log.v(TAG, "surfaceCreated");
new Thread() {
@Override
public void run() {
Paint paint = new Paint();
paint.setAntiAlias(true);
Canvas c = null;
while (true) {
c = holder.lockCanvas();
if (c == null) {
break;
}
float x = (float)(Math.random() * width);
float y = (float)(Math.random() * height);
paint.setColor(getColor());
c.drawCircle(x, y, 5f, paint);
holder.unlockCanvasAndPost(c);
}
}
}.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.v(TAG, "surfaceChanged");
this.width = width;
this.height = height;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.v(TAG, "surfaceDestroyed");
}
private int getColor() {
int color = (int)(Math.random() * colors.length);
return colors[color];
}
}
ランダムな位置に円を描くだけのSurfaceView
でコンパイルして起動してみると、アプリ起動後SurfaceViewが表示されて円がレンダリングされていく。で画面タッチ(もしくはクリック)すると画面が消える
まぁそういう事が出来る模様
追記
Androidのソースのpackages/app/Launcher2の中にRocketLauncherっていうBasicDreamがあるのでそれを参考にするのが良いかと思われる