如何在画布上绘制并转换为位图? [英] How to draw on canvas and convert to bitmap?

查看:81
本文介绍了如何在画布上绘制并转换为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在画布上绘制一些线条和形状,然后在ImageView上将其转换为位图.我在一个自定义类中扩展视图",并在"OnDraw方法上绘制线条.这是我的代码(该类仅绘制简单的线条):

I'm tring to draw some line and shapes on canvas and then convert it to bitmap on ImageView. I'm usin a custom class that extands "View" and on "OnDraw method i'm drawing the lines. here is my code(this class only draw simple lines) :

public class finalDraw extends View {
        public finalDraw(Context context) {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setColor(Color.BLUE);
            for (int i = 0; i <100; i++) {
                 canvas.drawLine(xStart * i + 50 , yStart , stopX + 30 , stopY,paint); 
            }

            invalidate();
        }
    }

如何获取绘图结果并将其显示在ImageView上? 谢谢!

How can i get the drawing result and show it on ImageView? Thanks!

推荐答案

找到这篇文章可能会有所帮助:

Found this article may help: http://www.informit.com/articles/article.aspx?p=2143148&seqNum=2

draw.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Bitmap imageBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(imageBitmap);
        float scale = getResources().getDisplayMetrics().density;
        Paint p = new Paint();
        p.setColor(Color.BLUE);
        p.setTextSize(24*scale);
        canvas.drawText("Hello", imageView.getWidth()/2, imageView.getHeight()/2, p);
        imageView.setImageBitmap(imageBitmap);
    }
});

这篇关于如何在画布上绘制并转换为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆