1. 程式人生 > >Android 畫畫板(OnTouchListener 應用)

Android 畫畫板(OnTouchListener 應用)

【1】佈局:

 

【2】 程式碼實現:

流程:

A,因為原圖無法被修改所以我們需要建立一個副本,把副本新增到Imageview 上

B,給ImageView 設定setOnTouchListener監聽  按下,擡起,移動。

C,設定畫筆顏色,字型

D,儲存在記憶體卡里。

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // [1]找到iv 用來展示我們畫的內容

        iv = (ImageView) findViewById(R.id.iv);



        // [2]先獲取bg.png的原圖

        srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);



        copyBimap = Bitmap.createBitmap(srcBitmap.getWidth(),

                srcBitmap.getHeight(), srcBitmap.getConfig());

        // [3.1]建立畫筆類

        paint = new Paint();

        // [3.2]建立一個畫布類 相當於把白紙鋪到了畫布上

        canvas = new Canvas(copyBimap);

        // [3.3]開始作畫 當32行程式碼執行完畢後 白紙上就有內容了

        canvas.drawBitmap(srcBitmap, new Matrix(), paint);

        // [3.4]畫一條線 線由2個點確定一條線

        // canvas.drawLine(20, 30, 40, 70, paint);

        // [4]把質的內容展示到iv上

        iv.setImageBitmap(copyBimap);

        // [5]給iv設定觸控事件

        iv.setOnTouchListener(new OnTouchListener() {

            float startX = 0;

            float startY = 0;



            @Override

            public boolean onTouch(View v, MotionEvent event) {

                // [6]具體判斷一下觸控事件的型別

                int action = event.getAction();

                switch (action) {

                case MotionEvent.ACTION_DOWN: // 按下

                    // [7]獲取手指按下的座標

                    startX = event.getX();

                    startY = event.getY();

                    System.out.println("按下:" + startX + "~~~~" + startY);

                    break;

                case MotionEvent.ACTION_MOVE: // 移動

                    // [8]獲取移動後的座標

                    float stopX = event.getX();

                    float stopY = event.getY();

                    // [9]不停的畫線

                    canvas.drawLine(startX, startY, stopX, stopY, paint);

                    System.out.println("移動:" + stopX + "~~~~" + stopY);

                    // [10]更新iv

                    iv.setImageBitmap(copyBimap);

                    // [11]更新一下開始的座標 startX startY

                    startX = stopX;

                    startY = stopY;



                    break;



                case MotionEvent.ACTION_UP: // 擡起



                    break;

                }



                // 如果返回值是true 當前監聽器物件會消費掉

                return true;

            }

        });



    }



    // 點選按鈕讓畫筆變紅色

    public void click1(View v) {

        paint.setColor(Color.RED);

    }



    // 點選按鈕讓畫筆加粗

    public void click2(View v) {

        paint.setStrokeWidth(19);

    }

    

    //點選按鈕儲存 大作

    public void click3(View v) {

        try {

            File file = new File(Environment.getExternalStorageDirectory().getPath(),"dazuo.png");

            FileOutputStream fos = new FileOutputStream(file);

            //參1:儲存圖片的格式   引數2:quality 質量

        copyBimap.compress(CompressFormat.PNG, 100, fos);

            fos.close();

            Toast.makeText(getApplicationContext(), "sucess", 1).show();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }



}