Android:使用 Canvas 加载 PNG 图像

Android: Load PNG image with Canvas


我正在为我的 Android 应用程序实现图形签名捕获功能。 我在网上找到了一个 完美运行 的函数,它已经在我的代码中了。用户绘制签名,图片正确保存!
现在我想添加这段代码:当调用 onCreate 函数时,我想检查签名图像是否已经存在,如果存在,则加载它。通过此实现,用户可以检查自己的签名。
那可能吗? 非常感谢您的建议!提前致谢!

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.firma_allievo);
    tempDir =  Environment.getExternalStorageDirectory().toString() + "/temp/"+corsok+"/";
    ContextWrapper cw = new ContextWrapper(getApplicationContext());

    current = tempDir + allievo + ".png"; // THIS IS THE PATH OF THE SIGNATURE IMAGE FILE

    mypath= new File(current);
    mContent = (LinearLayout) findViewById(R.id.linearLayout);

    mSignature = new signature(this, null);
    mSignature.setBackgroundColor(Color.WHITE);
    mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

    mClear = (Button)findViewById(R.id.clear);
    mGetSign = (Button)findViewById(R.id.getsign);
    mGetSign.setEnabled(true);
    mCancel = (Button)findViewById(R.id.cancel);
    mView = mContent;

    mClear.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mSignature.clear();
        }
    });

    mGetSign.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            boolean error = captureSignature();
            if(!error){
                mView.setDrawingCacheEnabled(true);
                mSignature.save(mView);
                Bundle b = new Bundle();
                b.putString("status", "done");
                Intent intent = new Intent();
                intent.putExtras(b);
                setResult(RESULT_OK,intent);
                finish();
             }
        }
    });
}

它应该像这样做一样简单:

ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);

如果你想在Canvas上绘制图像,那么你可以使用Bitmap创建一个BitmapDrawable,然后在canvas上绘制一个Drawable是微不足道的。

Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
Drawable d = new BitmapDrawable(bMap);
d.setBounds(left, top, right, bottom);  
d.draw(canvas);

下面是signature

public class signature extends View
    {
        private static final float STROKE_WIDTH = 5f;
        private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
        private Paint paint = new Paint();
        private Path path = new Path();

    private float lastTouchX;
    private float lastTouchY;
    private final RectF dirtyRect = new RectF();

    public signature(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
    }

    public void save(View v)
    {
        if(mBitmap == null)
        {
            mBitmap =  Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);;
        }
        Canvas canvas = new Canvas(mBitmap);
        try
        {
            FileOutputStream mFileOutStream = new FileOutputStream(mypath);

            v.draw(canvas);
            mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
            mFileOutStream.flush();
            mFileOutStream.close();
            String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null);
                //In case you want to delete the file
                //boolean deleted = mypath.delete();
                //Log.v("log_tag","deleted: " + mypath.toString() + deleted);
                //If you want to convert the image to string use base64 converter

        }
        catch(Exception e)
        {
            Log.v("log_tag", e.toString());
        }
    }

    public void clear()
    {
        path.reset();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        float eventX = event.getX();
        float eventY = event.getY();
        mGetSign.setEnabled(true);
        acquisisciFirma = "SI";

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                lastTouchX = eventX;
                lastTouchY = eventY;
                return true;

            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_UP:

                resetDirtyRect(eventX, eventY);
                int historySize = event.getHistorySize();
                for (int i = 0; i < historySize; i++)
                {
                    float historicalX = event.getHistoricalX(i);
                    float historicalY = event.getHistoricalY(i);
                    expandDirtyRect(historicalX, historicalY);
                    path.lineTo(historicalX, historicalY);
                }
                path.lineTo(eventX, eventY);
                break;

            default:
                debug("Ignored touch event: " + event.toString());
                return false;
        }

        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
                (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
    }

    private void debug(String string){
    }

    private void expandDirtyRect(float historicalX, float historicalY)
    {
        if (historicalX < dirtyRect.left)
        {
            dirtyRect.left = historicalX;
        }
        else if (historicalX > dirtyRect.right)
        {
            dirtyRect.right = historicalX;
        }

        if (historicalY < dirtyRect.top)
        {
            dirtyRect.top = historicalY;
        }
        else if (historicalY > dirtyRect.bottom)
        {
            dirtyRect.bottom = historicalY;
        }
    }

    private void resetDirtyRect(float eventX, float eventY)
    {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
    }
}

我自己解决了!加载的位图在 Canvas 组件中设置为背景。这是代码:

File file = new File(current);
if(file.exists()) {
    Bitmap bMap = BitmapFactory.decodeFile(current);
    Drawable d = new BitmapDrawable(bMap);
    mSignature.setBackground(d);
}

注意:最低 API 16 级!