更改位图背景图像/颜色,或使其透明?

时间:2011-06-19 00:54:31

标签: java android

我一直在关注这个绘图教程,我已经做了很多并且修改了很多。

唯一的,我无法弄清楚如何更改Bitmap / canvas的颜色或图像。它是黑色的,我希望它是白色的,设置为名为bg.png的图像,或透明的。任何帮助赞赏。这是我的java类/位图。

import android.content.Context;
import android.graphics.*;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
    private Boolean _run;
    protected DrawThread thread;
    private Bitmap mBitmap;
    public boolean isDrawing = true;
    public DrawingPath previewPath;


private CommandManager commandManager;

public DrawingSurface(Context context, AttributeSet attrs) {
    super(context, attrs);

    getHolder().addCallback(this);


    commandManager = new CommandManager();
    thread = new DrawThread(getHolder());
}

private Handler previewDoneHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        isDrawing = false;
    }
};

class DrawThread extends  Thread{
    private SurfaceHolder mSurfaceHolder;


    public DrawThread(SurfaceHolder surfaceHolder){
        mSurfaceHolder = surfaceHolder;

    }

    public void setRunning(boolean run) {
        _run = run;
    }


    @Override
    public void run() {
        Canvas canvas = null;
        while (_run){
            if(isDrawing == true){
                try{
                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }
                    final Canvas c = new Canvas (mBitmap);

                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                    canvas.drawColor(0, PorterDuff.Mode.CLEAR);


                    commandManager.executeAll(c,previewDoneHandler);
                    previewPath.draw(c);

                    canvas.drawBitmap (mBitmap, 0,  0,null);
                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }


            }

        }

    }
}


public void addDrawingPath (DrawingPath drawingPath){
    commandManager.addCommand(drawingPath);
}

public boolean hasMoreRedo(){
    return commandManager.hasMoreRedo();
}

public void redo(){
    isDrawing = true;
    commandManager.redo();


}

public void undo(){
    isDrawing = true;
    commandManager.undo();
}

public boolean hasMoreUndo(){
    return commandManager.hasMoreUndo();
}

public Bitmap getBitmap(){
    return mBitmap;
}


public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height) {
    // TODO Auto-generated method stub
    mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
}


public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    thread.setRunning(true);
    thread.start();
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    boolean retry = true;
    thread.setRunning(false);
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // we will try it again and again...
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

此链接可帮助它显示如何使用绿色屏幕或蓝屏,这是如何使图像背景在Java中透明:

  

-howto-chromakey