我正在开发图像编辑器,我正在使用自定义类来绘制位图,这是我的代码..
private void settingBitmapToDraw() {
// TODO Auto-generated method stub
resultBitmap=Bitmap.createScaledBitmap(resultBitmap, WIDTH, HEIGHT, true);
Matrix matrix=new Matrix();
matrix.setRotate(TO_DEGREE);
tempBitmap=Bitmap.createBitmap(resultBitmap, 0, 0, WIDTH, HEIGHT,
matrix, true);
bitmap=Bitmap.createBitmap(WIDTH, HEIGHT, tempBitmap.getConfig());
canvas=new Canvas(bitmap);
invalidate();
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(tempBitmap, xAxis, yAxis, paint);
if(overlayBitmap!=null)
{
canvas.drawBitmap(overlayBitmap, xAxis+50, yAxis+50, paint);
}
}
public void overlayImage() {
// TODO Auto-generated method stub
ImageProcessing.SAVE_STATUS=false;
overlayBitmap=BitmapFactory.decodeResource(getResources(),
R.drawable.add_image);
invalidate();
}
位图在画布上绘制,但在保存时会另存为黑色图像。这是我的保存位图代码...
try {
FileOutputStream fos=new FileOutputStream(file);
updatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
SAVE_STATUS=true;
saveDialog.dismiss();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
更新的位图由“位图”初始化
public Bitmap getUpdatedImage() {
// TODO Auto-generated method stub
//width=2048 height=1232
Bitmap updatedBitmap=Bitmap.createScaledBitmap(bitmap,
orgWidth, orgHeight, true);
return updatedBitmap;
}
///////////// 我的 ImageProcessing.java 类有代码...
if(!SAVE_STATUS)
{
updatedBitmap=ip_DrawingClass.getUpdatedImage();
if(updatedBitmap!=null)
{
file=new File(file_root);
saveDialog.show();
}
else
{
Toast.makeText(ImageProcessing.this,
"file can't saved...", Toast.LENGTH_SHORT).show();
}
}
并且 saveDialog 具有保存按钮,其中包含用于保存上面编写的图像的代码。
////////////////////////
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float xAxis,yAxis;
switch(event.getAction()&MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
MODE="DRAG";
startX=event.getX();
startY=event.getY();
if(ImageProcessing.drawLineStatus)
path.moveTo(startX, startY);
break;
case MotionEvent.ACTION_POINTER_DOWN:
MODE="ZOOM";
oldDist=this.findDistanceXY(event);
break;
case MotionEvent.ACTION_MOVE:
if(MODE=="ZOOM")
{
newDist=findDistanceXY(event);
if(newDist>oldDist)
{
this.applyZooming("plus");
}
else
{
this.applyZooming("minus");
}
oldDist=newDist;
}
else if(MODE=="DRAG")
{
xAxis=event.getX();
yAxis=event.getY();
if(!lock_status)
{
this.translateImage(xAxis,yAxis,startX,startY);
}
else
{
this.translateTextOnBitmap
(xAxis,yAxis,startX,startY);
}
if(ImageProcessing.drawLineStatus)
{
path.lineTo(xAxis, yAxis);
this.drawLinesOnBitmap();
}
else
draw_line=false;
}
break;
case MotionEvent.ACTION_POINTER_UP:
MODE="DRAG";
this.setZoomBoxXY();
break;
case MotionEvent.ACTION_UP:
this.set_XY_Axis();
break;
}
return true;
}
图片翻译在这里..
public void translateImage(float x, float y, float startX, float startY) {
// TODO Auto-generated method stub
if((startX>=imageAtX&&startX<=(imageAtX+imageW))&&
(startY>=imageAtY&&startY<=(imageAtY+imageH)))
{
if(imagePortionSelected||crop_status)
{
if((startX>=selectorAtX&&startX<=(selectorAtX+ZOOM))
&&(startY>=selectorAtY&&startY<=(selectorAtY+ZOOM)))
{
left=(int)(x-(startX-selectorAtX));
top=(int)(y-(startY-selectorAtY));
right=left+ZOOM;
bottom=top+ZOOM;
checkForValidityOfPara();
}
rect.set(left, top, right, bottom);
}
else
{
xAxis=x-(startX-imageAtX);
yAxis=y-(startY-imageAtY);
}
}
invalidate();
}