在布局上绘制位图的问题 [英] Problems drawing a bitmap on a layout

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

问题描述

我在 onCreate 中以编程方式创建布局:

I create the Layout programatically in onCreate:

new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            myView = new MyView(SketchActivity.this, layout.getWidth(), layout.getHeight());

            layout2.addView(myView);
            layout2.bringToFront();

        }
    }, 50);

我创建(可变?)位图的视图:

The View where i create the (mutable?) bitmap:

public MyView(Context c, int width, int height) {
        super(c);

        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int w = display.getWidth(); // deprecated
        int h = display.getHeight();
        setFocusable(true);
        setBackgroundResource(R.drawable.download);

        // setting paint
        mPaint = new Paint();
        mPaint.setAlpha(0);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mPaint.setAntiAlias(true);

        // getting image from resources
        Resources r = this.getContext().getResources();

        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.smoke);

        // converting image bitmap into mutable bitmap

        bitmap = bm.createBitmap(w, h, Config.ARGB_8888);
        mCanvas = new Canvas();
        mCanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
        mCanvas.drawBitmap(bm, 0, 0, null);

    }

我的 onDraw 函数:

My onDraw function:

 @Override
    protected void onDraw(Canvas canvas) {
        mCanvas.drawCircle(x, y, r, mPaint);

        canvas.drawBitmap(bitmap, 0, 0, null);

        super.onDraw(canvas);

    }

OnTouchEvent:

OnTouchEvent:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int) event.getX();
        y = (int) event.getY();

        r = 20;
        // Atlast invalidate canvas
        invalidate();

        return true;
    }

没有 LogCat 错误问题出在 myView 中.当我创建位图 bitmap = bm.createBitmap(w, h, Config.ARGB_8888);如果我用一个小数字代替 w,h(width and height of screen) 示例:bitmap = bm.createBitmap(20, 20, Config.ARGB_8888);它创造了一个微小的画面.但是如果我把w和h放在一起,那么不是在所有的布局上绘制,它只在一小部分上绘制.(即使我尝试:bitmap = bm.createBitmap(800, 1080, Config.ARGB_8888); 它仍然绘制一小部分,而不是整个屏幕.我该怎么办?

No LogCat Errors The problem is in myView. when i create the bitmap bitmap = bm.createBitmap(w, h, Config.ARGB_8888); If i put instead of w,h(width and height of screen) a small number Example: bitmap = bm.createBitmap(20, 20, Config.ARGB_8888); it creates a tiny picture. But if I put w and h, then instead of drawing on all the layout, it only draws on a small part. (even if i try: bitmap = bm.createBitmap(800, 1080, Config.ARGB_8888); it still draws on a small part, instead of all the screen. What should i do?

推荐答案

这就解决了:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // RECREATE THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

public MyView(Context c, int width, int height) {
        super(c);

        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int w = display.getWidth(); // deprecated
        int h = display.getHeight();
        setFocusable(true);
        setBackgroundResource(R.drawable.download);

        // setting paint
        mPaint = new Paint();
        mPaint.setAlpha(0);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mPaint.setAntiAlias(true);
        mPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));

        // getting image from resources
        Resources r = this.getContext().getResources();

        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.smoke);
        Bitmap bm2 = getResizedBitmap(bm, h, w);

        // converting image bitmap into mutable bitmap

        bitmap = bm2.createBitmap(w, h, Config.ARGB_8888);
        mCanvas = new Canvas();
        mCanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
        mCanvas.drawBitmap(bm2, 0, 0, null);

    }

这篇关于在布局上绘制位图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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