2012-05-30 55 views
0

我一直在開發繪圖應用程序。我有以下的主要活動代碼:我的自定義視圖如何爲繪圖製作黑色背景和紅色?

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    Display display = getWindow().getWindowManager().getDefaultDisplay(); 
    mMainView = new MyView(this, display.getWidth(), display.getHeight()); 
    setContentView(mMainView); 

    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(0xFFFF0000); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(12); 

    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f); 
    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); 
} 

和代碼繪製:

public class MyView extends View { 

    private static final float TOUCH_TOLERANCE = 4; 
    private static final float MINP = 0.25f; 
    private static final float MAXP = 0.75f; 

    private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Path mPath; 
    private Paint mBitmapPaint; 

    private float mX, mY; 

    public MyView(Context context, int width, int height) { 
     super(context); 

     this.setDrawingCacheEnabled(true); 
     mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     canvas.drawColor(0xFF000000); 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 
    } 

    public void clearView() { 
     mBitmap.eraseColor(Color.TRANSPARENT); 
    } 

    public Bitmap getState() { 
     return mBitmap; 
    } 

    private void touchStart(float x, float y) { 
     mPath.reset(); 
     mPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
    } 

    private void touchMove(float x, float y) { 
     float dx = Math.abs(x - mX); 
     float dy = Math.abs(y - mY); 
     if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
      mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
      mX = x; 
      mY = y; 
     } 
    } 

    private void touchUp() { 
     mPath.lineTo(mX, mY); 
     // commit the path to our offscreen 
     mCanvas.drawPath(mPath, mPaint); 
     // kill this so we don't double draw 
     mPath.reset(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      touchStart(x, y); 
      invalidate(); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      touchMove(x, y); 
      invalidate(); 
      break; 
     case MotionEvent.ACTION_UP: 
      touchUp(); 
      invalidate(); 
      break; 
     } 
     return true; 
    } 
} 

如此,因爲如果我嘗試使用位圖壓縮,然後一切都是爲了挽救導致位爲JPEG文件好,但如果我嘗試保存爲PNG文件,我將獲得具有透明背景(白色)和紅色圖片的文件。我需要我可以使用帶有黑色背景和紅色圖片的JPEG/PNG保存。保存代碼很好,不用擔心。謝謝。

回答

0

可以對位圖繪製一個黑色的長方形,就像這樣:

Bitmap myBitmap = Bitmap.createBitmap(width, height, ...); 
Canvas mCanvas = new Canvas(myBitmap); 

// create a black rectangle 
final int color = 0xffffffff; 
final Paint p1 = new Paint(); 
final Rect rect = new Rect(0, 0, width, height); 
final RectF rectF = new RectF(rect); 

// draw it to the canvas 
mCanvas.drawARGB(0, 0, 0, 0); 
p1.setColor(color); 
mCanvas.drawRect(rectF, p1); 

這應該強制黑色的背景,你可以在畫布上疊加更多的位圖,最後使用MYBITMAP導出爲PNG。 我認爲你現在擁有的黑色背景是jpeg的默認設置,因爲它沒有透明度。

+0

對不起,但我需要「畫到畫布」?我需要繪製位圖,因爲我需要在繪製後保存... – user1166635

+0

添加了上面的解釋。 – Steelight