错误:宽度和高度必须> 0改变对比度时的android

时间:2015-01-25 20:06:37

标签: android image imageview processing illegalargumentexception

我的简单图像编辑器出了问题。对比度更改后,我正在尝试设置新的已编辑图像,但会出现异常。

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类(扩展ImageView)的方法,它获取并排除错误。 Debuger显示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);
}

2 个答案:

答案 0 :(得分:0)

当IamgeView没有分配宽度/高度时,通常会导致错误。 如果您要创建新图片,则必须使用 LayoutParams 指定视图的宽度和高度。

像: 的 mImageView.setLayoutParams(new LayoutParams(mwidth. mheight));

答案 1 :(得分:0)

我找到了答案!我看过帖子: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();
        }
    });
}