今天在看 android 的書,剛好有範例的 source code,想說直接加入 eclipse,就直接把整個範例目錄 copy 到 workspace,按了 F5 重新更新,卻不見出現在 Package Explorer。

拜請 google 大神,查了許久也沒有結果,只好自己用 eclipse 把功能試出來囉。

Posted by 台南小新 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;

@SuppressWarnings("deprecation")
public class WhiteBoardActivity extends Activity {
    private AbsoluteLayout layout;
    private Bitmap bmp;
    private Canvas canvas;
    private Paint brush;
    private View canvasView;
    private float oldX=0, oldY=0;
    private boolean paintStart = false;
    
    /** Called when the activity is first created. */
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // fullscreen
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // paint brush
        brush = new Paint();
        brush.setColor(Color.BLACK);

        // bitmap
        bmp = Bitmap.createBitmap(
                this.getWindowManager().getDefaultDisplay().getWidth(),
                this.getWindowManager().getDefaultDisplay().getHeight(),
                Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bmp);
        canvas.drawColor(Color.WHITE);

        // bitmap view
        canvasView = new View(this) {
            @Override
            public void onDraw(Canvas canvas){
                canvas.drawBitmap(bmp, 0, 0, null);
            }
        };
       // clear button
        Button btnClear = new Button(this);
        btnClear.setText("Clear");
        btnClear.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                canvas.drawColor(Color.WHITE);
                canvasView.invalidate();
            }
        });

        // color button
        final Button btnBlack = new Button(this);
        btnBlack.setText("Black");
        btnBlack.setTextColor(Color.BLUE);
        final Button btnBlue = new Button(this);
        btnBlue.setText("Blue");
        btnBlue.setTextColor(Color.BLACK);
        final Button btnRed = new Button(this);
        btnRed.setText("Red");
        btnRed.setTextColor(Color.BLACK);

        btnBlack.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                btnBlack.setTextColor(Color.BLUE);
                btnBlue.setTextColor(Color.BLACK);
                btnRed.setTextColor(Color.BLACK);
                brush.setColor(Color.BLACK);
            }
        });
        btnBlue.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                btnBlack.setTextColor(Color.BLACK);
                btnBlue.setTextColor(Color.BLUE);
                btnRed.setTextColor(Color.BLACK);
                brush.setColor(Color.BLUE);
            }
        });
        btnRed.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                btnBlack.setTextColor(Color.BLACK);
                btnBlue.setTextColor(Color.BLACK);
                btnRed.setTextColor(Color.BLUE);
                brush.setColor(Color.RED);
            }
        });
       // layout create
        layout = new AbsoluteLayout(this);
        layout.addView(canvasView,
                new AbsoluteLayout.LayoutParams(
                        this.getWindowManager().getDefaultDisplay().getWidth(),
                        this.getWindowManager().getDefaultDisplay().getHeight(),
                        0, 0));
        layout.addView(btnClear,
                new AbsoluteLayout.LayoutParams(
                        100, 50, 0, 0));
        layout.addView(btnBlack,
                new AbsoluteLayout.LayoutParams(
                        100, 50, 0, 50));
        layout.addView(btnBlue,
                new AbsoluteLayout.LayoutParams(
                        100, 50, 0, 100));
        layout.addView(btnRed,
                new AbsoluteLayout.LayoutParams(
                        100, 50, 0, 150));
        setContentView(layout);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            paintStart = true;
            oldX = event.getX();
            oldY = event.getY();
            canvas.drawPoint(oldX, oldY, brush);
            canvasView.invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            if (paintStart) {
                float newX = event.getX();
                float newY = event.getY();
                canvas.drawLine(oldX, oldY, newX, newY, brush);
                oldX = newX;
                oldY = newY;
                canvasView.invalidate();
            }
            break;
        case MotionEvent.ACTION_UP:
            paintStart = false;
            break;
        }
        return true;
    }
}

Posted by 台南小新 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

很多遊戲的 APPS 都可以全螢幕下操作,去除了 apps title 及 statusbar,下面就介紹如何實現 fullscreen apps。

// 去除 apps title
requestWindowFeature(Window.FEATURE_NO_TITLE);

// full screen
getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

切記 requestWindowFeature 必須在 setContentView 之前執行,否則會造成程式當掉。

Posted by 台南小新 at 痞客邦 PIXNET 留言(1) 引用(0) 人氣()

使用 SurfaceView + MediaPlayer 有幾項要注意的重點,不然是無法順利播放影片。

  • MediaPlayer.setDisplay(SurfaceHolder) 必須在 surfaceCreated 之後才能執行,不然只會聽到聲音看不到影像。
  • SurfaceHolder.setType() 必須設定成 SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS,否則播放會出錯。
  • MediaPlayer.setDisplay() 不一定要在 MediaPlayer.start() 前面,隨時都可以變更。
  • MediaPlayer.setDataSource() 可以輸入網址或是檔案路徑。
  • 如果要重覆撥放,需要設定 OnCompletionListener,在 onCompletion 放一個 mediaPlayer.start() 即可。
  • 如果撥完要撥另一個檔案,則需要先下 reset() 進入 IDLE stage,才能再用 setDataSource(),不然會出現 error stage 的錯誤訊息。
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class VideoTestActivity extends Activity {

    MediaPlayer mediaPlayer;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surfaceView = new SurfaceView(this);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        setContentView(surfaceView);

        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource("/mnt/sdcard/video/video2.3gp");
            mediaPlayer.prepare();
        } catch (Exception e) {
        }
        mediaPlayer.start();

        mediaPlayer.setOnCompletionListener(
            new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer arg0) {
                    try {
                        mp.reset();
                        mediaPlayer.setDataSource("/mnt/sdcard/video/video1.3gp");
                        mediaPlayer.prepare();
                    } catch (Exception e) {
                    }
                    // repeat play 只要下 start()
                    mediaPlayer.start();
                }
            });

        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
                Log.e("video","surfaceChanged");
            }
            @Override
            public void surfaceCreated(SurfaceHolder arg0) {
                Log.e("video","surfaceCreated");
                mediaPlayer.setDisplay(surfaceHolder);
            }
            @Override
            public void surfaceDestroyed(SurfaceHolder arg0) {
                Log.e("video","surfaceDestroyed");
                if (mediaPlayer != null) mediaPlayer.release();
            }
        });
    }
}

Posted by 台南小新 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

一般要取得 android 營幕的解析度大小,可以藉由 Activity.getWindowManager().getDefaultDisplay() 先取得 Display 物件,再利用 Display 來取得 height 及 width。但是這個尺吋不是 layout 真正的尺寸,android API 也說了,因為還有一些裝飾(如 status bar),所以 layout 的尺寸會小一點。

那要如何取得 layout 真正的尺寸大小呢?由於 layout 也是繼承於 View,所以可以藉由 getHeigth 及 getWidth 來取得,但是一開始初始化時,所取得的值卻為 0。原因就出在 layout 一開始並沒有計算尺寸大小,而是在後面重新安排元件位置時才會計算。但是在那一個過程才能取得 layout 實際大小呢?

Posted by 台南小新 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()