回答
- 創建一個空的位圖
- 創建一個新的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. Android - 畫布上的圖像
- 2. 動畫畫布上的圖像列表
- 3. 畫布上的布料模擬+圖像
- 4. 在畫布上擦圖像
- 5. 帶有圖像的畫布轉換爲jpeg
- 6. 畫布圖像
- 7. 如何變暗畫布或畫布上繪製的圖像?
- 8. Colourize在畫布上的圖像?
- 9. 畫布上的圖像圓角
- 10. 畫布上的圖像彼此堆疊
- 11. globalAlpha在畫布上的一個圖像
- 12. 如何保存圖像jpeg文件?
- 13. 微軟Office文件圖像(JPEG/PNG)
- 14. HTML畫布圖像
- 15. 畫布圖像(securityError)
- 16. 在觸摸事件上重繪畫布上的圖像?
- 17. 本地圖像在鉻在畫布上
- 18. 在畫布上重繪圖像
- 19. 調整圖像大小在畫布上
- 20. 在畫布上隨機顯示圖像
- 21. 分層圖像加上畫布
- 22. JS圖像未顯示在畫布上
- 23. 繪製大型圖像到畫布上
- 24. 在圖像上覆蓋HTML5畫布
- 25. 將圖像放到畫布上
- 26. 圖像不載入畫布上?
- 27. 在畫布上繪製對象/圖像
- 28. 圖像不顯示在畫布上(Java)
- 29. 在畫布上拖動圖像
- 30. 在畫布上繪製背景圖像
既然你提到的鏈接長久以來是死的,你也許可以添加更多的上下文連接到問題本身? – Flexo 2015-05-22 14:49:39