目前日期文章:201201 (2)

瀏覽方式: 標題列表 簡短摘要

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

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

台南小新 發表在 痞客邦 PIXNET 留言(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;
    }
}

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()