【发布时间】:2014-10-16 07:03:47
【问题描述】:
我使用来自 android 的 API 示例来绘制响应触摸事件的视图。基本上它的作用是每次我触摸屏幕时,它都会在我所拥有的上面画一个圆圈(所以画一个透明背景的圆圈,看起来好像内容被保留了)
现在的问题是当我尝试将其保存为图像时。我尝试了JPG和PNG,我得到了黑色图片。我猜它不知何故让所有透明的东西都变黑了。
有什么方法可以将此位图保存为图像(最好是 JPG)?当你看图像本身时,它根本没有透明度。
谢谢
根据要求添加代码
我按如下方式初始化视图
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
int curW = mBitmap != null ? mBitmap.getWidth() : 0;
int curH = mBitmap != null ? mBitmap.getHeight() : 0;
if (curW >= w && curH >= h) {
return;
}
if (curW < w) curW = w;
if (curH < h) curH = h;
Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888);
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (mBitmap != null) {
newCanvas.drawBitmap(mBitmap, 0, 0, null);
}
mBitmap = newBitmap;
mCanvas = newCanvas;
}
@Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
我将视图绘制如下
void paintmycolor(float x, float y, float major, float minor){
mPaint.setColor(myDrawColor);
mPaint.setAlpha(Math.min((int) (pressure * 128), 255));
canvas.save(Canvas.MATRIX_SAVE_FLAG);
RectF mReusableOvalRect = new RectF();
canvas.rotate((float) (orientation * 180 / Math.PI), x, y);
mReusableOvalRect.left = x - minor / 2;
mReusableOvalRect.right = x + minor / 2;
mReusableOvalRect.top = y - major / 2;
mReusableOvalRect.bottom = y + major / 2;
canvas.drawOval(mReusableOvalRect, paint);
canvas.restore();
}
我保存如下 公共文件 saveBitmap() 抛出 IOException {
File path=Environment.getExternalStoragePublicDirectory(android.os.Environment.DCIM);
File myDirectory = new File(path,mContext.getString(R.string.app_name));
if(!myDirectory.exists()){
myDirectory.mkdirs();
}
File output;
do {
int rand = new Random().nextInt();
output = new File(myDirectory,rand+".jpg");
}while(output.exists());
BufferedOutputStream ous = null;
try {
ous=new BufferedOutputStream(new FileOutputStream(output));
mBitmap.compress(CompressFormat.JPEG, 100, ous);
ous.flush();
ous.close();
return output;
} finally {
if (ous!=null) {
try {
ous.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("closing", e.getMessage());
}
}
}
}
【问题讨论】:
-
您是否正在绘制您的圈子用户视图...??请分享您的绘制圈子以更好地理解
-
显示一些代码。一些陷阱:1)JPG,不支持透明度,所以你会得到一个黑色的背景。如果你的圈子是黑色的,那么你将无法找到它。 2)您使用的是 SurfaceView 吗?或者你正在使用画布? SurfaceView 实际上是在画布中绘制了一个黑色图像,如果您使用画布捕获位图,SurfaceView 图层尚未渲染,因此您的位图将是黑色的。
-
我添加了相关的代码。这是我正在扩展的正常视图