线未在画布中绘制

时间:2014-11-16 02:05:13

标签: android android-canvas

我想在我拍摄的照片中画线,但画线并没有画出来。 如何使用Canvas是错误的? 我想我可能不太了解如何使用Canvas。 我在哪里修理? 请教我。

PhotoMemoActivity

public class PhotoMemoActivity extends Activity {
    Uri _uri;
    Canvas _canvas;
    DrawCanvas _drawCanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_memo);
        takePicture();
        _drawCanvas = new DrawCanvas(getApplicationContext());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == 0){
            if(resultCode == RESULT_OK){
                ImageView imageView = (ImageView)findViewById(R.id.picture);
                imageView.setImageURI(_uri);
                Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
                Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                _canvas = new Canvas(drawableBitmap);
            }
        }
    }

    public void takePicture(){
        String fileName = System.currentTimeMillis() + ".png";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
        _uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, _uri);
        startActivityForResult(intent, 0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.photo_memo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        float firstX=0, firstY=0, lastX, lastY, x, y;

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                firstX = event.getX();
                firstY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                lastX = event.getX();
                lastY = event.getY();
                _drawCanvas.draw(firstX, firstY, lastX, lastY, _canvas);
                break;
            default:
                break;
        }
        return true;
    }

}

DrawCanvas

public class DrawCanvas extends ImageView {

    public DrawCanvas(Context context){
        super(context);
    }

    public void draw(float firstX, float firstY, float lastX, float lastY, Canvas canvas){
        Paint p = new Paint();
        p.setStyle(Paint.Style.FILL);
        p.setColor(Color.GREEN);
        p.setStrokeWidth(10);
        canvas.drawLine(firstX, firstY, lastX, lastY, p);
        invalidate();
    }
}

1 个答案:

答案 0 :(得分:1)

你几乎就在那里,你只需要结合你的想法。制作一个单独的视图,用于处理绘制拍摄的图片和绘制图片顶部的线条。

所以只需使用你的班级" DrawCanvas"。我查看了View的文档以及创建自定义视图的一般想法,这是您要在此处执行的操作。视图已经有了#34; onDraw"呼叫,所以只需覆盖它并添加自己的触摸处理。

public class DrawCanvas extends ImageView {

private Bitmap curBitmap;
private Paint paint;
private Paint pathPaint;
private Path curPath;

public DrawCanvas(Context context){
    super(context);
}

private void initialize() {

pathPaint = new Paint();
pathPaint.setAntiAlias(true);
pathPaint.setStrokeWidth(2);
pathPaint.setStrokeSize(14);
pathPaint.setColor(Color.BLACK);

curPath = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);

//create the bitmap.
curBitmap = Bitmap.createBitmap(getWidth(), getHeight(), ARGB_8888);


}

public void onDraw(Canvas canvas) {
  //draw bitmap first. 
  canvas.drawBitmap(bitmap, x, y, paint);
  //draw the line
  canvas.drawPath(curPath, pathPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event){


    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            //move to the current path
            curPath.moveTo(event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_MOVE:
            curPath.lineTo(event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_UP:
              //do something when the person lets go. 
        default:
            break;
    }
   // redraw the view. 
    invalidate();
    return true;
}


  public void setBitmapToDraw(Bitmap bitmap) {
    this.curBitmap = bitmap;
    invalidate();
  }

}

然后在你的活动中。只需在视图中设置位图即可。

...other code....
   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == 0){
            if(resultCode == RESULT_OK){
                ImageView imageView = (ImageView)findViewById(R.id.picture);
                imageView.setImageURI(_uri);
                Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
                Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                drawView.setBitmapToDraw(drawableBitmap);
            }
        }
    }

请参阅以下链接:

Android Bitmap

Android Path

Android

Android Canvas