Error: width and height must be > 0 android when changing contrast

Error: width and height must be > 0 android when changing contrast

我的简单图像编辑器有问题。对比度更改后,我试图设置新的、编辑过的图像,但出现异常。

public ContrastBrightnessDialog(Context context, String title, final DrawingView drawView) {
    super(context);
    this.setContentView(R.layout.contrastbrightness_dialog);
    this.setTitle(title);

    txtView = (TextView) findViewById(R.id.textView1);
    contrastValue = (TextView) findViewById(R.id.contrast_value);
    brightnessValue = (TextView) findViewById(R.id.textView1);
    contrastSB = (SeekBar) findViewById(R.id.contrast_seekbar);
    contrastSB.setProgress(0);

    contrastSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    // TODO Auto-generated method stub
                    contrastValue.setText(String.valueOf(progress - 100));
                    contrastInt = Integer.parseInt(String
                            .valueOf(progress - 100));
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                }
            });

    confirmBtn = (Button) findViewById(R.id.confirm_btn);
    confirmBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                    drawView.setDrawingCacheEnabled(true);
                    drawView.measure(
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    drawView.layout(0, 0, drawView.getMeasuredWidth(),
                            drawView.getMeasuredHeight());

                    drawView.buildDrawingCache(true);

                    Bitmap bmin = Bitmap.createBitmap(drawView.getDrawingCache());

                    BitmapFactory.Options options = new BitmapFactory.Options();

                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    Bitmap workingBitmap = Bitmap.createBitmap(doContrast(bmin,
                            contrastInt));

                    Bitmap mutableBitmap = workingBitmap.copy(
                            Bitmap.Config.ARGB_8888, true);
                    options = new BitmapFactory.Options();
                    Drawable d = new BitmapDrawable(drawView.getResources(),
                            mutableBitmap);
                    drawView.setBackground(d);
                    drawView.setDrawingCacheEnabled(false);
            close();
        }
    });
}

public Bitmap doContrast(Bitmap src, int value) {
 //contrast algorithm from https://xjaphx.wordpress.com/2011/06/21/image-processing-contrast-image-on-the-fly/
return someBitmap;}

这里是我的 DrawingView class(扩展 ImageView)中的一个方法,它获取并显示错误。调试器显示 w 和 h 等于 0。

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);//here is an error
    drawCanvas = new Canvas(canvasBitmap);
}

您遇到的错误通常是在 IamgeView 没有分配给它 width/height 时引起的。 如果要创建新图像,则必须使用 LayoutParams.

指定视图的宽度和高度

类似于: mImageView.setLayoutParams(new LayoutParams(mwidth. mheight));

我找到答案了!我读到 post:ShowcaseView - width and height must be > 0

并且有一个工作代码:

confirmBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            drawView.post(new Runnable() {
                @Override
                public void run() {
                    drawView.layout(0, 0, drawView.getMeasuredWidth(),
                            drawView.getMeasuredHeight());

                    drawView.setDrawingCacheEnabled(true);
                    drawView.buildDrawingCache(true);


                    Bitmap bmin = drawView.getDrawingCache();

                    BitmapFactory.Options options = new BitmapFactory.Options();

                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    Bitmap workingBitmap = Bitmap.createBitmap(doContrast(bmin,
                            c, b));

                    Bitmap mutableBitmap = workingBitmap.copy(
                            Bitmap.Config.ARGB_8888, true);

                    options = new BitmapFactory.Options();

                    Drawable d = new BitmapDrawable(drawView.getResources(),
                            mutableBitmap);

                    drawView.setBackground(d);

                    drawView.setDrawingCacheEnabled(false);

                    drawView.invalidate();
                }
            });

            close();
        }
    });
}