android+ canvas繪圖後儲存圖片

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

canvas繪圖後儲存圖片,分兩種情況,一種:自己new一個canvas對象。 第二種:在onDraw函數作為參數引入Canvas

 

第一種情況:

  建一個空的bitmap對象,然後在初始化畫布時,把它作為參數帶入,之後canvas所做的繪圖操作,都是在該bitmap上,只要儲存該bitmap對象即可。如下代碼:畫一個藍色的圓環:

            //這邊直接用canvas畫,然後儲存            Bitmap bitmap = Bitmap.createBitmap(700,700, Bitmap.Config.ARGB_8888);            Canvas canvas = new Canvas(bitmap);            canvas.drawColor(Color.WHITE);            Paint paint = new Paint();            paint.setAntiAlias(true);//設定消除鋸齒            paint.setStrokeWidth(7);//設定筆觸寬度            paint.setStyle(Paint.Style.STROKE);            //畫奧運五環            paint.setColor(Color.BLUE);            canvas.drawCircle(150,150,100,paint); //繪製藍色圓環            canvas.save(Canvas.ALL_SAVE_FLAG); //儲存            canvas.restore(); // 儲存            File sdRoot = Environment.getExternalStorageDirectory();            String save_path = sdRoot.getAbsolutePath() + "/DCIM/100MEDIA/114.jpg";            try {                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(save_path)));            } catch (FileNotFoundException e) {                e.printStackTrace();            }

 

 

第二種情況:

  重點就是setDrawingCacheEnabled(true); 和 getDrawingCache()這兩個函數

        public MyView(Context context) {            super(context);            //這函數是要開啟圖形緩衝,這樣才能getDrawingCache            setDrawingCacheEnabled(true);        }

 

        protected void onDraw(Canvas canvas){            canvas.drawColor(Color.WHITE);            Paint paint = new Paint();            paint.setAntiAlias(true);//設定消除鋸齒            paint.setStrokeWidth(7);//設定筆觸寬度            paint.setStyle(Paint.Style.STROKE);            //畫一個環            paint.setColor(Color.BLUE);            canvas.drawCircle(150,150,100,paint); //繪製藍色圓環            canvas.save(Canvas.ALL_SAVE_FLAG); //儲存            canvas.restore(); // 儲存            //將canvas裡的draw儲存下來            File sdRoot = Environment.getExternalStorageDirectory();            String save_path = sdRoot.getAbsolutePath() + "/DCIM/100MEDIA/113.jpg";            try {                getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(save_path)));            } catch (Exception e) {                Log.e("Error--------->", e.toString());            }            super.onDraw(canvas);        }    

 

android+ canvas繪圖後儲存圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.