java - 如何从View的onDraw()方法中提取Bitmap?

标签 java android android-canvas android-custom-view android-bitmap

如何从 CustomView 的 onDraw() 例程中提取 Bitmap 对象?

这是我的代码:

public class DrawView extends View {
    private Paint paint = new Paint();
    private Point point;
    private LinkedList<Point> listaPontos;
    private static Context context;

    class Point {

        public Point(float x, float y) {
            this.x = x;
            this.y = y;
        }

        float x = 0;
        float y = 0;
    }

    public DrawView(Context context) {
        super(context);
        this.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        this.context = context;
        paint.setColor(Color.YELLOW);
        this.listaPontos = new LinkedList<Point>();
    }

    @Override
    public void onDraw(Canvas canvas) {

        if(listaPontos.size() != 0){
            for(Point point : listaPontos){
                canvas.drawCircle(point.x,  point.y, 25, paint);    
            }
        }
        calculateAmount(canvas);        
    }

    private void calculateAmount(Canvas canvas) {
        LinkedList<Integer> colors = new LinkedList<Integer>();
        for(int i = 0 ; i != canvas.getWidth(); i++)
        {
            for(int j = 0; j != canvas.getHeight(); j++){

                int color = BITMAP.getPixel(i,j);  //How can I get the bitmap generated on onDraw ?

                colors.add(color);
            }
        }

        int yellow = 0;
        int white = 0;

        for(Integer cor : colors) {

            if(cor == Color.WHITE) {
                white++;
            }
            if(cor == Color.YELLOW) {
                yellow++;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    listaPontos.add(new Point(event.getX(), event.getY()));
                    break;
        }
        invalidate();
        return true;
    }
}

提前非常感谢;)

编辑:位图的作用是计算每个像素颜色,如何将背景图像添加到我的 DrawView 中?我测试了 this.setBackgroundResource(R.drawable.a);在构造函数中但没有工作,再次感谢;)

最佳答案

无法从 Canvas 中提取位图。至少不能直接提取。

但是,可以使用Canvas在位图上绘图,然后使用位图

Bitmap mDrawBitmap;
Canvas mBitmapCanvas;
Paint drawPaint = new Paint();

@Override
public void onDraw(Canvas canvas) {

    drawPaint.setColor(Color.RED);

    if (mDrawBitmap == null) {
        mDrawBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        mBitmapCanvas = new Canvas(mDrawBitmap);
    }

    // clear previously drawn stuff
    mBitmapCanvas.drawColor(Color.WHITE);

    // draw on the btimapCanvas
    mBitmapCanvas.drawStuff(...);
    //... and more

    // after drawing with the bitmapcanvas,
    //all drawn information is stored in the Bitmap    


    // draw everything to the screen
    canvas.drawBitmap(mDrawBitmap, 0, 0, drawPaint);
}

onDraw()方法执行完毕后,所有绘制的信息都会被绘制到屏幕上(通过调用canvas.drawBitmap(...),并且也会被绘制到屏幕上)存储在您的 Bitmap 对象中(因为所有绘制操作都在使用 Bitmap 创建的 Canvas 上完成)。

关于java - 如何从View的onDraw()方法中提取Bitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21914978/

相关文章:

java - 使用 HashMap 和数组组织锻炼信息

java - 用%20替换空格时长度的值

android - 自定义偏好 Android Kotlin

android - 提交到 Rails 时 JSON 导致实体无法处理(错误 422)

java - 如何测试 jdbc 中的更新方法

java - GWT 将大量数据从服务器发送到客户端的最佳实践

android - 我的RecyclerView没有在通知方法上更新

android - 如何在android中裁剪imageview的顶部、左侧、右侧和底部

Android 在 SurfaceView 上相对于用户平滑旋转

android - Canvas 的性能问题