java.lang.Object
↳ android.view.View
↳ android.view.SurfaceView
を使用しますまた、Surfaceの変更などを取得するために
SurfaceHolder.Callback
を使用します
このインターフェースは
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
- public void surfaceCreated(SurfaceHolder holder)
- public void surfaceDestroyed(SurfaceHolder holder)
というわけで、SurfaceView をいじってみた
サンプルプログラム
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SurfaceTestView(this));
}
class SurfaceTestView extends SurfaceView
implements SurfaceHolder.Callback, Runnable {
private Thread thread;
private BitmapDrawable bitmapDrawable;
public SurfaceTestView(Context context) {
super(context);
// SurfaceHolder の取得
SurfaceHolder holder = getHolder();
// SurfaceHolder に コールバックを設定
holder.addCallback(this);
holder.setFixedSize(getWidth(), getHeight());
// フォーカスをあてる
setFocusable(true);
bitmapDrawable = new BitmapDrawable(
context.getResources(),
BitmapFactory.decodeResource(
context.getResources(),
R.drawable.icon)
);
}
// Surface が変更されたときの動作
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
}
// Surface が作成されたときの動作
public void surfaceCreated(SurfaceHolder holder) {
thread = new Thread(this);
thread.start();
}
// Surface が削除されたときの動作
public void surfaceDestroyed(SurfaceHolder holder) {
thread = null;
}
public void run() {
while (thread != null) {
// 描画の開始
Canvas canvas = getHolder().lockCanvas();
draw(canvas);
// 描画の終了
getHolder().unlockCanvasAndPost(canvas);
}
}
@Override
public void draw(Canvas canvas) {
// 現在の状態を保存
canvas.save();
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(32);
bitmapDrawable.setBounds(0, 0, 96, 96);
bitmapDrawable.draw(canvas);
canvas.drawText("SurfaceViewTest", 0, 128, paint);
paint.setAntiAlias(true);
bitmapDrawable.setBounds(0, 160, 96, 256);
bitmapDrawable.draw(canvas);
canvas.drawText("SurfaceViewTest2", 0, 288, paint);
// 現在の状態の変更
canvas.restore();
}
}
}
プログラムを実行すると...
こんな感じ!(上:アンチエイリアスなし, 下:あり)
アンチエイリアスは時間がかかるが場合によってはかけた方が良さそう
参考サイト
http://developer.android.com/intl/ja/reference/android/view/SurfaceView.html
http://developer.android.com/intl/ja/reference/android/view/SurfaceHolder.Callback.html

0 件のコメント:
コメントを投稿