今回からSurfaceViewに入ります。
また画像表示してみます。
Activityのプログラムです。
次にSurfaceViewのプログラム
コードが長いですね。
これで最低限の画像表示ができるところです。
親クラスはSurfaceViewです。
インターフェースに SurfaceHolder.Callback, Runnableを指定します。
コンストラクタで重要なのは
this.thread = new Thread(this);
super.getHolder().addCallback(this);
この2行です。
が、やってることは見たままです。
次が
public void surfaceCreated(SurfaceHolder p1)
{
this.holder = p1;
this.thread.start();
}
holderとやらをクラス変数に保持して、
threadを開始します。
最後に表示処理です。(runの中)
canvas = this.holder.lockCanvas();
canvasを取得しています。
paint = new Paint();
canvas.drawColor(this.backColor);
毎ループ毎に画面をリセットします。
canvas.drawBitmap(bmp, 100, 200, paint);
Viewと同じ画像描画です。
this.holder.unlockCanvasAndPost(canvas);
画面に表示します。
解説に出てこなかったものはいったんオマジナイです。




また画像表示してみます。
Activityのプログラムです。
public class MainActivity extends Activity
{
private SurfaceViewSample surfaceView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.surfaceView = new SurfaceViewSample(this);
setContentView(surfaceView);
}
}
次にSurfaceViewのプログラム
public class SurfaceViewSample extends SurfaceView implements SurfaceHolder.Callback, Runnable
{
private Bitmap bmp = null;
protected Thread thread;
protected SurfaceHolder holder;
protected long SLEEP_TIME;
protected int backColor;
private boolean stopFlg = false;
public void surfaceCreated(SurfaceHolder p1)
{
this.holder = p1;
this.thread.start();
}
public void surfaceChanged(SurfaceHolder p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
public void surfaceDestroyed(SurfaceHolder p1)
{
// TODO: Implement this method
}
public void run()
{
Canvas canvas = null;
Paint paint = null;
while(!this.stopFlg){
canvas = this.holder.lockCanvas();
paint = new Paint();
canvas.drawColor(this.backColor);
canvas.drawBitmap(bmp, 100, 200, paint);
this.holder.unlockCanvasAndPost(canvas);
try{
this.thread.sleep(SLEEP_TIME);
}catch(Exception e){}
}
}
public SurfaceViewSample(Context context){
super(context);
this.thread = new Thread(this);
super.getHolder().addCallback(this);
this.SLEEP_TIME = 1000;
this.backColor = Color.BLACK;
this.bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}
public void stopLoop(){
this.stopFlg = true;
}
}
コードが長いですね。
これで最低限の画像表示ができるところです。
親クラスはSurfaceViewです。
インターフェースに SurfaceHolder.Callback, Runnableを指定します。
コンストラクタで重要なのは
this.thread = new Thread(this);
super.getHolder().addCallback(this);
この2行です。
が、やってることは見たままです。
次が
public void surfaceCreated(SurfaceHolder p1)
{
this.holder = p1;
this.thread.start();
}
holderとやらをクラス変数に保持して、
threadを開始します。
最後に表示処理です。(runの中)
canvas = this.holder.lockCanvas();
canvasを取得しています。
paint = new Paint();
canvas.drawColor(this.backColor);
毎ループ毎に画面をリセットします。
canvas.drawBitmap(bmp, 100, 200, paint);
Viewと同じ画像描画です。
this.holder.unlockCanvasAndPost(canvas);
画面に表示します。
解説に出てこなかったものはいったんオマジナイです。
