9

我正在尝试找到一种方法来设置画布背景,并使用从自定义颜色选择器中拾取的颜色而不删除其上的任何绘图。我正在尝试创建一个可以在画布上绘制并将其保存为 png 的应用程序。但是当我为当前画布设置新背景时,所有绘图都消失了。我正在使用这样的东西:

mCanvas.drawColor(picker.getColor());

有什么想法可以让事情正常进行吗?

4

7 回答 7

8

已经对您的问题给出的答案都指向正确的方向:您确实需要将背景色块和前景图分开在单独的图层中,然后将它们合并,然后将其全部保存在 .png 文件中。这也是 Adob​​e Photoshop 工作流程的设计方式......如果我们考虑一下,它确实是有道理的:以像 MsPaint 这样的软件为例:因为它不使用图层,它必须依赖像填充算法这样的东西来完成(尽管以不完整的方式)远程类似于背景变化的事情......

实现这种事情的一种方法是实例化由 2 个不同位图支持的 2 个 Canvas 对象。第一个 Canvas-Bitmap 对将用于您在前景的绘图,第二个 Canvas-Bitmap 对将用于您的合并图层绘图(即前景绘图 + 背景色块)。然后,当您需要保存第二个位图时,它将被保存到 .png 文件中。这样,我们的第一个 Canvas-Bitmap 对存储您的前景信息,如果需要更改背景颜色,这些信息不会被破坏。每次进行操作时,图层都可以合并到第二个 Canvas-Bitmap 对中,以便始终有一个具有正确内容的位图可以随时保存。

这是我为清除此方法而制作的自定义视图。它实现了一个简单的视图,用于使用手指在触摸屏上绘制一条蓝线,背景颜色根据手指的 XY 位置而变化,以演示背景颜色的变化,而无需完整实现所固有的不必要的代码复杂性带有色轮/菜单/除其他外

package com.epichorns.basicpaint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.view.View;

public class PaintView extends View{

    Bitmap mMergedLayersBitmap=null; //Note: this bitmap here contains the whole of the drawing (background+foreground) to be saved.
    Canvas mMergedLayersCanvas=null;

    Bitmap mBitmap = null; //bitmap onto which we draw our stuff
    Canvas mCanvas = null; //Main canvas. Will be linked to a .bmp file
    int mBackgroundColor = 0xFF000000; //default background color
    Paint mDefaultPaint = new Paint();

    Paint mDrawPaint = new Paint(); //used for painting example foreground stuff... We draw line segments.
    Point mDrawCoor = new Point(); //used to store last location on our PaintView that was finger-touched

    //Constructor: we instantiate 2 Canvas-Bitmap pairs
    public PaintView(Context context, int width, int height) {
        super(context);
        mMergedLayersBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
        mMergedLayersCanvas = new Canvas(mMergedLayersBitmap);

        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    //Change background color
    public void changeColor(int newColor){
        mBackgroundColor = newColor;
        invalidate(); //refresh view: this will indirectly invoke onDraw soon afterwards
    }

    //Called by user of PaintView in order to start a painting "stroke" (finger touching touch-screen): stores the 
    //location of the finger when it first touched the screen
    public void startDraw(int x, int y, int radius, int color){
        mDrawPaint.setColor(color);
        mDrawPaint.setStyle(Style.STROKE);
        mDrawPaint.setStrokeWidth(radius);
        mDrawCoor.x = x;
        mDrawCoor.y = y;        
    }

    //Called by user of PaintView when finger touching touch-screen is moving (must be called after a startDraw, 
    //as the latter initializes a couple of necessary things)
    public void continueDraw(int x, int y){
        mCanvas.drawLine(mDrawCoor.x, mDrawCoor.y, x, y, mDrawPaint);
        mDrawCoor.x = x;
        mDrawCoor.y = y;
        invalidate(); //refresh view: this will indirectly invoke onDraw soon afterwards
    }

    //Merge the foreground Canvas-Bitmap with a solid background color, then stores this in the 2nd Canvas-Bitmap pair.
    private void mergeLayers(){
        mMergedLayersCanvas.drawColor(mBackgroundColor);
        mMergedLayersCanvas.drawBitmap(mBitmap, 0, 0, mDefaultPaint);
    }

    @Override
    public void onDraw(Canvas canvas){
        mergeLayers();
        canvas.drawBitmap(mMergedLayersBitmap, 0, 0, mDefaultPaint);
    }

}

为了测试这个视图,这里有一个使用PaintView该类的测试 Activity。这两个文件在 Android 项目中都是自给自足的,因此您可以在真实设备上轻松测试它:

package com.epichorns.basicpaint;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;


import com.epichorns.basicpaint.PaintView;
public class BasicPaintActivity extends Activity {
    PaintView mPaintView=null;
    LinearLayout mL = null;
    boolean mIsDrawing=false;
    int mBackgroundColor = 0xFF000000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Display display = getWindowManager().getDefaultDisplay();       
        final float dispWidth = (float)display.getWidth();
        final float dispHeight = (float)display.getHeight();

        mPaintView = new PaintView(this, display.getWidth(), display.getHeight());    
        mPaintView.changeColor(mBackgroundColor);
        mPaintView.setOnTouchListener(new View.OnTouchListener(){

            public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction()==MotionEvent.ACTION_DOWN){
                    mPaintView.startDraw((int)event.getX(), (int)event.getY(), 6, 0x806060FF);              
                    mIsDrawing=true;
                    return true;
                }
                else if(event.getAction()==MotionEvent.ACTION_UP){
                    mIsDrawing=false;
                    return true;
                }
                else if(event.getAction()==MotionEvent.ACTION_MOVE){
                    if(mIsDrawing){

                        //To demonstrate background change, change background color depending on X-Y position
                        int r = (int)(255f*event.getX()/dispWidth);
                        int g = (int)(255f*event.getY()/dispHeight);
                        mBackgroundColor = Color.argb(0xFF, r,g, 0x00);
                        Log.d("DEBUG1", "Color channels: (r, g) = ("+String.valueOf(r)+", "+String.valueOf(g)+")");
                        mPaintView.changeColor(mBackgroundColor);

                        //now, draw stuff where finger was dragging...
                        mPaintView.continueDraw((int)event.getX(), (int)event.getY());
                        return true;
                    }
                    else{
                        return false;
                    }

                }

                return false;
            }

        });

        setContentView(mPaintView);
    }




}
于 2012-04-29T08:39:41.943 回答
5

当您绘制颜色时,它会在您的图纸上绘制。您需要绘制颜色,然后再次绘制其他所有内容。

于 2012-04-22T14:47:47.133 回答
1

看看你是否想在画布中进行更改,那么你必须调用 invalidate 以将这些更改应用到你的屏幕上。如果你调用 invalidate 那么你的onDraw()方法将调用。

如果您只想从颜色选择器更改画布的背景颜色,则将颜色值保存在变量中并在保存变量后立即调用无效。现在您将调用。onDraw()现在通过调用更改画布背景并绘制您想要的所有其他内容setBackgroundColor(color variable)onDraw()

于 2012-05-02T06:50:54.803 回答
1

使用 canvas.drawARGB(a,r,g,b) 它将适用于明确

于 2012-05-02T10:49:10.393 回答
0

@安卓机器人

这两行代码对我来说就像魅力一样。当用户单击任何颜色(例如: Red )时,将该颜色设置为 mPaint

      mPaint.setColor(Color.RED);

当你想改变画布颜色时

    dv.setBackgroundColor(mPaint.getColor());

其中 dv 是扩展视图(自定义视图)的类的对象。试试看,如果您遇到任何问题,请告诉我。

于 2015-01-23T21:31:46.957 回答
0

只要您的背景是并且将是另一种颜色,您就可以执行以下操作:

for (x...)
  for (y...)
    if (bitmap.getPixel(x,y) == oldBackgroundColor)
      bitmap.setPixel(x,y,newBackgroundColor)

或者,您可以在离屏位图上绘制内容,然后将背景和离屏绘制到实际位图上。这样,您可以更改将在接下来的两步绘图发生时使用的背景颜色。

于 2012-04-26T06:25:23.253 回答
0

也许这是一个老问题,但我想为这个解决方案做出贡献,如果你从源中获取位图,然后用画布做一个可绘制的,也许这可以适合你:

@Override
public Bitmap transform(final Bitmap source) {
    //Background for transparent images
    Bitmap backg = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    backg.eraseColor(Color.WHITE); // Any color you want...
    Paint back = new Paint();
    BitmapShader backshader = new BitmapShader(backg,
    BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    back.setShader(backshader);
    back.setAntiAlias(true);

    // Image for the draw
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP,
            Shader.TileMode.CLAMP));
    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
    Canvas canvas = new Canvas(output);

    // IMPORTANT THING
    canvas.drawRoundRect(new RectF(margin, margin, source.getWidth()
            - margin, source.getHeight() - margin), radius, radius, back); // Draw the background first...
    canvas.drawRoundRect(new RectF(margin, margin, source.getWidth()
            - margin, source.getHeight() - margin), radius, radius, paint); // And then Draw the image, so it draws on top of the background

    if (source != output) {
        source.recycle();
    }

    // This is for if i want to put a border in the drawable, its optional
    Paint paint1 = new Paint();      
    paint1.setColor(Color.parseColor("#CC6C7B8B"));
    paint1.setStyle(Style.STROKE);
    paint1.setAntiAlias(true);
    paint1.setStrokeWidth(2);
    canvas.drawRoundRect(new RectF(margin, margin, source.getWidth()
            - margin, source.getHeight() - margin), radius, radius, paint1);

    // and then, return the final drawable...
    return output;
}

希望能帮助到你...

于 2015-08-06T14:30:11.207 回答