回答
- 創建一個空的位圖
- 創建一個新的Canvas對象和該位圖傳遞給它
- 呼叫view.draw(Canvas)的傳遞您剛纔創建的畫布對象。 Refer Documentation of method for details.
- 使用Bitmap.compress()將位圖的內容寫入OutputStream文件中。
僞代碼:
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
你好Samuh, 我試過了代碼,它會生成一個jpeg文件,但沒有畫布繪製的形狀或我曾經寫在畫布上。 有任何意見。 感謝, 科坦 – 2011-02-09 09:50:21
感謝您的回答。 – 2011-11-21 04:34:01
- 集描繪緩存啓用任何你想要的
- 從視圖
- 壓縮獲取位圖對象,並保存圖像文件
- 抽獎 個
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class CanvasTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Draw2d d = new Draw2d(this);
setContentView(d);
}
public class Draw2d extends View {
public Draw2d(Context context) {
super(context);
setDrawingCacheEnabled(true);
}
@Override
protected void onDraw(Canvas c) {
Paint paint = new Paint();
paint.setColor(Color.RED);
c.drawCircle(50, 50, 30, paint);
try {
getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));
} catch (Exception e) {
Log.e("Error--------->", e.toString());
}
super.onDraw(c);
}
}
}
不知何故,在onDraw方法中壓縮並保存是個不好的主意。 – 2013-06-23 18:10:28
它給了我空指針異常 – abh22ishek 2016-01-14 12:58:15
畫布JPG:
Canvas canvas = null;
FileOutputStream fos = null;
Bitmap bmpBase = null;
bmpBase = Bitmap.createBitmap(image_width, image_height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmpBase);
// draw what ever you want canvas.draw...
// Save Bitmap to File
try
{
fos = new FileOutputStream(your_path);
bmpBase.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
fos = null;
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (fos != null)
{
try
{
fos.close();
fos = null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
- 1. 畫布上的圖像
- 2. Android - 畫布上的圖像
- 3. 動畫畫布上的圖像列表
- 4. 上傳文件(HTML5畫布圖像→文件)以JavaScript
- 5. 畫布上的布料模擬+圖像
- 6. 圖像不畫布上
- 7. 在畫布上擦圖像
- 8. 帶有圖像的畫布轉換爲jpeg
- 9. EaselJS,從文件創建圖像(畫布)
- 10. 將畫布上的圖像保存到SurfaceView中的文件
- 11. javascript在畫布上畫一個圖像
- 12. 在畫布上畫一串圖像
- 13. Python - 圖像沒有畫在畫布上
- 14. base64圖像從畫布到文件上傳HTML控件
- 15. 畫布圖像
- 16. 如何隱藏和顯示事件畫布上畫的圖像?
- 17. 畫布:drawImage不畫圖像到畫布
- 18. 將圖像上載到畫布上
- 19. 如何在畫布上設置圖像..?然後畫在圖像
- 20. javascript:上傳圖像文件並將其繪製到畫布中
- 21. 在畫布上向量化的圖像
- 22. 從javascript上的圖像創建畫布
- 23. Colourize在畫布上的圖像?
- 24. 覆蓋在畫布上的圖像
- 25. 如何移動畫布上的圖像?
- 26. 畫布上的圖像彼此堆疊
- 27. 畫布上的tkinter圖像消失
- 28. 畫布上的圖像未顯示
- 29. 在HTML5的畫布上繪製圖像
- 30. 如何保存畫布上的圖像
既然你提到的鏈接長久以來是死的,你也許可以添加更多的上下文連接到問題本身? – Flexo 2015-05-22 14:49:39