พฤศจิกายน 21, 2018, 01:53:31 pm *
ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน
ส่งอีเมล์ยืนยันการใช้งาน?

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น
   หน้าแรก   ช่วยเหลือ เข้าสู่ระบบ สมัครสมาชิก  
หน้า: [1]   ลงล่าง
  พิมพ์  
ผู้เขียน หัวข้อ: Android 2015: 01-How to draw a bitmap (image) [from Basic to Surface+Thread]  (อ่าน 2324 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
ShadowMan
Administrator
Hero Member
*****
ออฟไลน์ ออฟไลน์

เพศ: ชาย
กระทู้: 8272


ShadowWares


| |
« เมื่อ: พฤศจิกายน 18, 2015, 02:05:09 pm »

Android 2015: 01-How to draw a bitmap (image) [from Basic to Surface+Thread]




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");
    }
}



บันทึกการเข้า

By SDW: Do No Wrong Is Do Nothing
          If you want to increase your success rate, double your failure rate
ShadowMan
Administrator
Hero Member
*****
ออฟไลน์ ออฟไลน์

เพศ: ชาย
กระทู้: 8272


ShadowWares


| |
« ตอบ #1 เมื่อ: พฤศจิกายน 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");
    }
}
บันทึกการเข้า

By SDW: Do No Wrong Is Do Nothing
          If you want to increase your success rate, double your failure rate
ShadowMan
Administrator
Hero Member
*****
ออฟไลน์ ออฟไลน์

เพศ: ชาย
กระทู้: 8272


ShadowWares


| |
« ตอบ #2 เมื่อ: พฤศจิกายน 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");
    }
}
บันทึกการเข้า

By SDW: Do No Wrong Is Do Nothing
          If you want to increase your success rate, double your failure rate
ShadowMan
Administrator
Hero Member
*****
ออฟไลน์ ออฟไลน์

เพศ: ชาย
กระทู้: 8272


ShadowWares


| |
« ตอบ #3 เมื่อ: พฤศจิกายน 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();
    }
}
บันทึกการเข้า

By SDW: Do No Wrong Is Do Nothing
          If you want to increase your success rate, double your failure rate
ShadowMan
Administrator
Hero Member
*****
ออฟไลน์ ออฟไลน์

เพศ: ชาย
กระทู้: 8272


ShadowWares


| |
« ตอบ #4 เมื่อ: พฤศจิกายน 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();
    }
}

บันทึกการเข้า

By SDW: Do No Wrong Is Do Nothing
          If you want to increase your success rate, double your failure rate
many888
Newbie
*
ออฟไลน์ ออฟไลน์

กระทู้: 2


| |
« ตอบ #5 เมื่อ: สิงหาคม 29, 2018, 02:06:00 pm »

ต้องลองทำซะแล้ว



คาสิโนออนไลน์
บันทึกการเข้า
หน้า: [1]   ขึ้นบน
  พิมพ์  
 
กระโดดไป: