从其他活动中读取位图->空图像

问题描述 投票:0回答:1

背景:为了提供一些背景信息,⇒该应用程序应该简单地向用户显示一个区域(SignatureActivity / SignatureCanvasView)以放置一个签名。我找到了一个片段,可以很好地插入。

问题:

尝试在MainActivity中检索位图以在ImageView中显示它会显示一个空的Image。同时编写检索到的位图会创建一个png文件,由于它甚至没有背景色,因此几乎是空的。

这就是在MainActivity中完成的过程:

    private void insertSignature(){
        ivSign.setImageBitmap(signature);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == SIGNATURE_REQUEST){
            if(resultCode == RESULT_OK){
                signature = BitmapFactory.decodeByteArray(data.getByteArrayExtra("SignatureBitmap"), 0, data.getByteArrayExtra("SignatureBitmap").length);
                //signature = (Bitmap) getIntent().getParcelableExtra("SignatureBitmap");
                if(signature != null){
                    insertSignature();
                    Log.e("ActivityResult: ", "SignatureActivity finished with RESULT_OK");
                }
            }else if(resultCode == RESULT_CANCELED){
                Log.e("ActivityResult: ", "SignatureActivity was cancelled");
            }else{
                Log.e("ActivityResult", "Unknown activity result!");
            }
        }

    }

这是视图(SignatureCanvasView)的代码,它维护签名功能:

public class SignatureCanvasView extends View {
    public int width;
    public int height;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    Context context;
    private Paint mPaint;
    private float mX, mY;
    private static final float TOLERANCE = 5;

    public SignatureCanvasView(Context c, AttributeSet attrs) {
        super(c, attrs);
        context = c;

        // we set a new Path
        mPath = new Path();

        // and we set a new Paint with the desired attributes
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(2f);
    }

    public byte[] getBitmap() {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();

        return byteArray;
    }


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

        // your Canvas will draw onto the defined Bitmap
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    // override onDraw
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // draw the mPath with the mPaint on the canvas when onDraw
        canvas.drawPath(mPath, mPaint);
    }

    // when ACTION_DOWN start touch according to the x,y values
    private void startTouch(float x, float y) {
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    // when ACTION_MOVE move touch according to the x,y values
    private void moveTouch(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOLERANCE || dy >= TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    public void clearCanvas() {
        mPath.reset();
        invalidate();
    }

    // when ACTION_UP stop touch
    private void upTouch() {
        mPath.lineTo(mX, mY);
    }

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

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startTouch(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                moveTouch(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                upTouch();
                invalidate();
                break;
        }
        return true;
    }
}

在完成之前,先在SignatureActivity中调用SignatureCanvasView的getBitmap()方法,将其作为额外内容放置:

public class SignatureActivity extends AppCompatActivity {
    //...
    public void Save(){
        this.getIntent().putExtra("SignatureBitmap",scw.getBitmap());
        setResult(RESULT_OK,this.getIntent());
        finish();
    }
    //...
}

我感谢解决此问题的任何提示和建议。

[背景:要提供一些背景信息⇒该应用程序应仅向用户显示一个区域(SignatureActivity / SignatureCanvasView)以放置签名。我找到了一个片段,可以很好地绘制...

android canvas bitmap
1个回答
0
投票

您正在将Path绘制到附有canvasSignatureCanvasView上,而不是绘制在附有Bitmap mBitmap的画布上。

© www.soinside.com 2019 - 2024. All rights reserved.