ShadowWares Forum

Programming (Applications) => Android Programming => ข้อความที่เริ่มโดย: ShadowMan ที่ พฤศจิกายน 18, 2015, 02:05:09 pm



หัวข้อ: Android 2015: 01-How to draw a bitmap (image) [from Basic to Surface+Thread]
เริ่มหัวข้อโดย: ShadowMan ที่ พฤศจิกายน 18, 2015, 02:05:09 pm
Android 2015: 01-How to draw a bitmap (image) [from Basic to Surface+Thread]


(http://upic.me/i/ik/ww511.jpg)

Code: (java)
package com.control.insight.renderdemo1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GameView(this));
    }
}


Code: (java)
package com.control.insight.renderdemo1;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.View;

/**
 * Created by Santi on 11/18/2015.
 */
public class GameView extends View {
    private Bitmap bmp;
    public GameView(Context context) {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        Log.i("SANTI", "GameView() is called");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int cw = canvas.getWidth();
        int ch = canvas.getHeight();
        int bw = bmp.getWidth();
        int bh = bmp.getHeight();
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, (cw - bw) / 2, (ch - bh) / 2, null);

        Log.i("SANTI", "onDraw() is called");
    }
}


(http://upic.me/i/w2/vzxj2.jpg)


หัวข้อ: Re: Android 2015: 01-How to draw a bitmap (image)
เริ่มหัวข้อโดย: ShadowMan ที่ พฤศจิกายน 18, 2015, 02:20:47 pm
Using the SurfaceView

Code: (java)
package com.control.insight.renderdemo1;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Santi on 11/18/2015.
 */
public class GameView extends SurfaceView {
    private Bitmap bmp;
    private SurfaceHolder holder;
    public GameView(Context context) {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        Log.i("SANTI", "GameView() is called");
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Log.i("SANTI", "surfaceCreated() is called");
                Canvas canvas = holder.lockCanvas();
                onDraw(canvas);
                holder.unlockCanvasAndPost(canvas);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Log.i("SANTI", "surfaceChanged() is called");
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Log.i("SANTI", "surfaceDestroyed() is called");
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int cw = canvas.getWidth();
        int ch = canvas.getHeight();
        int bw = bmp.getWidth();
        int bh = bmp.getHeight();
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, (cw - bw) / 2, (ch - bh) / 2, null);
        Log.i("SANTI", "onDraw() is called");
    }
}


หัวข้อ: Re: Android 2015: 01-How to draw a bitmap (image)
เริ่มหัวข้อโดย: ShadowMan ที่ พฤศจิกายน 18, 2015, 02:25:51 pm
Using the "implements SurfaceHolder.Callback"

Code: (java)
package com.control.insight.renderdemo1;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Santi on 11/18/2015.
 */
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
    private Bitmap bmp;
    private SurfaceHolder holder;

    public GameView(Context context) {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        Log.i("SANTI", "GameView() is called");
        holder = getHolder();
        holder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i("SANTI", "surfaceCreated() is called");
        Canvas canvas = holder.lockCanvas();
        onDraw(canvas);
        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.i("SANTI", "surfaceChanged() is called");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i("SANTI", "surfaceDestroyed() is called");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int cw = canvas.getWidth();
        int ch = canvas.getHeight();
        int bw = bmp.getWidth();
        int bh = bmp.getHeight();
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, (cw - bw) / 2, (ch - bh) / 2, null);
        Log.i("SANTI", "onDraw() is called");
    }
}


หัวข้อ: Re: Android 2015: 01-How to draw a bitmap (image)
เริ่มหัวข้อโดย: ShadowMan ที่ พฤศจิกายน 18, 2015, 03:05:51 pm
Using Thread w/ implements Runnable

Code: (java)
package com.control.insight.renderdemo1;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Santi on 11/18/2015.
 */
public class GameView extends SurfaceView implements Runnable {
    private Bitmap bmp;
    private SurfaceHolder holder;
    private Thread thread = null;
    private boolean isRunning = false;
    private int frameCount = 0;
    private int touchCounter = 0;

    public GameView(Context context) {
        super(context);
        Log.i("SANTI", "GameView() is called");
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        holder = getHolder();

        isRunning = true;
        thread = new Thread(this);
        thread.start();
    }

    public void draw(Canvas canvas) {
        super.draw(canvas);
        //Log.i("SANTI", "draw() is called");
        int cw = canvas.getWidth();
        int ch = canvas.getHeight();
        int bw = bmp.getWidth();
        int bh = bmp.getHeight();
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, (cw - bw) / 2, (ch - bh) / 2, null);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(60);
        canvas.drawText(String.format("%d %d", frameCount, touchCounter), 200, 100, paint);
        frameCount++;
    }

    @Override
    public void run() {
        Log.i("SANTI", "run() is called");
        while(isRunning) {
            if(!holder.getSurface().isValid()) {
                continue;
            }
            Canvas canvas = holder.lockCanvas();
            draw(canvas);
            holder.unlockCanvasAndPost(canvas);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("SANTI", "onTouchEvent() is called");
        touchCounter++;
        return super.onTouchEvent(event);
    }

    // Will be used later
    public void pause() {
        Log.i("SANTI", "pause() is called");
        isRunning = false;
        while (true) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
        thread = null;
    }

    // Will be used later
    public void resume(){
        Log.i("SANTI", "resume() is called");
        isRunning = true;
        thread = new Thread(this);
        thread.start();
    }
}


หัวข้อ: Re: Android 2015: 01-How to draw a bitmap (image)
เริ่มหัวข้อโดย: ShadowMan ที่ พฤศจิกายน 18, 2015, 03:27:36 pm
Full test

Code: (java)
package com.control.insight.renderdemo1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
    private GameView render = null;

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

        //--Remove title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //--Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                   WindowManager.LayoutParams.FLAG_FULLSCREEN);

        startRender();
        setContentView(render);
        Log.i("SANTI", "Main:onCreate() is called");
    }

    private void startRender() {
        if(render == null) {
            render = new GameView(this);
            render.start();
        }
    }

    private void stopRender() {
        if(render != null) {
            render.pause();
            render = null;
        }

    }
    @Override
    protected void onStart() {
        super.onStart();
        startRender();
        Log.i("SANTI", "Main:onStart() is called");
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopRender();
        Log.i("SANTI", "Main:onStop() is called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopRender();
        Log.i("SANTI", "Main:onPause() is called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        startRender();
        Log.i("SANTI", "Main:onResume() is called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopRender();
        Log.i("SANTI", "Main:onDestroy() is called");
    }
}



Code: (java)
package com.control.insight.renderdemo1;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Santi on 11/18/2015.
 */
public class GameView extends SurfaceView implements Runnable {
    private Bitmap bmp;
    private SurfaceHolder holder;
    private Thread thread = null;
    private boolean isRunning = false;
    private int frameCount = 0;
    private int touchCounter = 0;

    public GameView(Context context) {
        super(context);
        Log.i("SANTI", "GameView() is called");
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        holder = getHolder();
    }

    public void draw(Canvas canvas) {
        super.draw(canvas);
        int cw = canvas.getWidth();
        int ch = canvas.getHeight();
        int bw = bmp.getWidth();
        int bh = bmp.getHeight();
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, (cw - bw) / 2, (ch - bh) / 2, null);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(60);
        canvas.drawText(String.format("%d %d", frameCount, touchCounter), 200, 100, paint);
        frameCount++;
    }

    @Override
    public void run() {
        Log.i("SANTI", "run() is called");
        while(isRunning) {
            if(!holder.getSurface().isValid()) {
                continue;
            }
            Canvas canvas = holder.lockCanvas();
            draw(canvas);
            holder.unlockCanvasAndPost(canvas);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("SANTI", "onTouchEvent() is called");
        touchCounter++;
        return super.onTouchEvent(event);
    }

    public void pause() {
        Log.i("SANTI", "pause() is called");
        isRunning = false;
        while (true) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
        thread = null;
        frameCount = 0;
        touchCounter = 0;
    }

    public void resume(){
        Log.i("SANTI", "resume() is called");
        isRunning = true;
        thread = new Thread(this);
        thread.start();
    }

    public void start() {
        resume();
    }
}