Android:使用画布在imageview上绘制

时间:2015-03-17 22:35:05

标签: java android button geometry ondraw

现在我有一个显示图像的活动。我希望能够在图像上画一个圆圈。 '绘制'按钮应该在图像上绘制一个圆圈,但由于某种原因,它会覆盖原始位图而不是在其上绘制。

如何让按钮在原始位图/ imageview上放置一个圆圈而不是制作一个全新的?

public class ImageDisplayActivity extends FragmentActivity {

    public static final String KEY_PATH = "img.jpg";
    private ZoomInZoomOut touch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_display);
        Intent intent = getIntent();
        String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
        try {
            java.io.FileInputStream in = this.openFileInput(path);
            Bitmap bitmap = BitmapFactory.decodeStream(in);
            touch = (ZoomInZoomOut) findViewById(R.id.IMAGEID);
            touch = arrangeImageView(touch);
            touch.setImageBitmap(bitmap);
            in.close();
            Button draw = (Button) findViewById(R.id.draw);
            draw.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Bitmap imageBitmap = Bitmap.createBitmap(200,
                            200, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(imageBitmap);
                    Paint p = new Paint();
                    p.setAntiAlias(true);
                    p.setColor(Color.BLUE);
                    p.setStyle(Paint.Style.STROKE);
                    canvas.drawCircle(100, 100, 5, p);
                    touch.setImageBitmap(imageBitmap);
                }
            });
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ZoomInZoomOut arrangeImageView(ZoomInZoomOut img) {
        try {
            img.setRotation(90);
            img.setScaleX(1.8f);
            img.setScaleY(1.8f);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return img;
    }
}

1 个答案:

答案 0 :(得分:0)

SetImageBitmap用新的位图替换当前位图。它并没有在它上面绘制。最简单的解决方法是从图像视图中获取位图,将其绘制到imageBitmap,然后将圆绘制到imageBitmap。