2015-05-01 58 views

回答

0

您可以在Android的佈局保存爲形象的比方:

RelativeLayout view; 
    view.setDrawingCacheEnabled(true); 
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
    view.buildDrawingCache(true); 
    Bitmap b = Bitmap.createBitmap(view.getDrawingCache()); 
    view.setDrawingCacheEnabled(false); // clear draFwing cache 
    bmImage.setImageBitmap(b); 
    saveImage(); 

    public void saveImage() { 
      BitmapDrawable drawable = (BitmapDrawable) bmImage.getDrawable(); 
      Bitmap bitmap = drawable.getBitmap(); 
      File sdCardDirectory = Environment.getExternalStorageDirectory(); 
      File image = new File(sdCardDirectory, "my_card.png"); 
      boolean success = false; 
      FileOutputStream outStream; 
      try { 
       outStream = new FileOutputStream(image); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
       /* 100 to keep full quality of the image */ 
       outStream.flush(); 
       outStream.close(); 
       success = true; 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      if (success) { 
       Toast.makeText(getApplicationContext(), "Image saved with success", 
         Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(getApplicationContext(), 
         "Error during image saving", Toast.LENGTH_LONG).show(); 
      } 
     } 
相關問題