致:java.lang.IllegalArgumentException:如果x必须< bitmap.width() [英] Caused by: java.lang.IllegalArgumentException: x must be < bitmap.width()

查看:1009
本文介绍了致:java.lang.IllegalArgumentException:如果x必须< bitmap.width()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class SlimmingView extends View implements OnTouchListener {

Paint paint = new Paint();
Paint paint1 = new Paint();
Bitmap bitmap;

Bitmap rotatedBitmap,SlimmingBitmap;


private int mPosX;
private int mPosY;

int screenWidth, screenHeight;

final int bitWidth, bitHeight;


int radius;

boolean isShow=false;

public SlimmingView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    PathEffect effects = new DashPathEffect(new float[] { 8, 8,8, 8 }, 1);
    paint.setPathEffect(effects);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.YELLOW);
    paint.setAntiAlias(true);


    paint1.setPathEffect(effects);
    paint1.setStyle(Paint.Style.STROKE);
    paint1.setStrokeWidth(2);
    paint1.setColor(Color.BLUE);
    paint1.setAntiAlias(true);

    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);



    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    screenHeight = wm.getDefaultDisplay().getHeight();
    screenWidth = wm.getDefaultDisplay().getWidth();

    mPosX = screenWidth / 2;
    mPosY = screenHeight / 2;

    bitWidth = screenWidth / 2 - bitmap.getWidth() / 2;
    bitHeight = screenHeight / 2 - bitmap.getHeight() / 2 - 22;

    radius = 40;

    SlimmingBitmap=Slimming(bitmap,mPosX-20, mPosY-20,mPosX+20, mPosY+20, radius, 0.2f);
}

@Override
public boolean onTouch(View v, MotionEvent event) {



    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        isShow=true;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        break;
    }

    case MotionEvent.ACTION_UP: {

        break;
    }

    case MotionEvent.ACTION_CANCEL: {

        break;
    }

    }

    return true;

}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    canvas.drawBitmap(SlimmingBitmap, 0, 0, null);
    canvas.drawCircle(mPosX-20, mPosY-20, radius, paint);
    canvas.drawCircle(mPosX+20, mPosY+20, radius, paint1);
}

public Bitmap Slimming(Bitmap bmp,int startX,int startY ,int endX,int endY,int radius,float power)
{
    //int height = bmp.getHeight();
//  int width= bmp.getWidth();

//  Bitmap bmpSephia = Bitmap.createBitmap(width, height,bmp.getConfig());
//  Canvas canvas = new Canvas(bmpSephia);

//  canvas.drawBitmap(bmp, 0, 0, null);
    for (int x = endX + radius; x > endX - radius && x > 0; x--)
    {
        for (int y = endY + radius; y > endY - radius && y > 0; y--)
        {
            if (x > bmp.getWidth() || y > bmp.getHeight())
            {
                continue;
            }

            if (Math.pow((x - endX), 2) + Math.pow((y -endY), 2) > Math.pow(radius, 2))
            {
                // not in the cic
                continue;
            }

            double LVF_Modulus = 1 - GetDistXDist(x,y, startX,startY) / (radius * radius);
            LVF_Modulus = LVF_Modulus * LVF_Modulus *  LVF_Modulus;

            LVF_Modulus = LVF_Modulus * power;

            double LVF_XPos = x - LVF_Modulus * (endX - startX);
            double LVF_YPos = y - LVF_Modulus * (endY - startY);

            long LVI_XPos1 = (long) (LVF_XPos);
            long LVI_XPos2 = LVI_XPos1 + 1;
            long LVI_YPos1 = (long)(LVF_YPos);
            long LVI_YPos2 = LVI_YPos1 + 1;

            if (LVI_XPos1 >= bmp.getWidth() ||
                    LVI_YPos1 >= bmp.getHeight() ||
                    LVI_XPos2 >= bmp.getWidth() ||
                    LVI_YPos2 >= bmp.getHeight() ||
                    LVI_XPos1 < 0 ||
                    LVI_YPos1 < 0 ||
                    LVI_XPos2 < 0 ||
                    LVI_YPos2 < 0 )
                {
                    continue;
                }


            int LVI_RValue = 0;
            int LVI_GValue = 0;
            int LVI_BValue = 0;
             double temp1 = 0;
             double temp2 = 0;

            int c = bmp.getPixel((int)LVI_YPos1, (int)LVI_XPos1);
                // a= Color.alpha(c);
                int red = Color.red(c);
                int green = Color.green(c);
                int blue = Color.blue(c);

            int c1 = bmp.getPixel((int)LVI_YPos1, (int)LVI_XPos2);  
            int red1 = Color.red(c1);
            int green1 = Color.green(c1);
            int blue1 = Color.blue(c1);

            int c2 = bmp.getPixel((int)LVI_YPos2, (int)LVI_XPos1);  
            int red2 = Color.red(c2);
            int green2 = Color.green(c2);
            int blue2 = Color.blue(c2);


            int c3 = bmp.getPixel((int)LVI_YPos2, (int)LVI_XPos2);  
            int red3 = Color.red(c3);
            int green3 = Color.green(c3);
            int blue3 = Color.blue(c3);

            temp1 = (red + (LVF_XPos - LVI_XPos1)*(red1 - red));
            temp2 = (red2 + (LVF_XPos - LVI_XPos1)*(red3 - red2));
            LVI_RValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            temp1 = (green + (LVF_XPos - LVI_XPos1)*(green1 - green));
            temp2 = (green2 + (LVF_XPos - LVI_XPos1)*(green3 - green2));
            LVI_GValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            temp1 = (blue + (LVF_XPos - LVI_XPos1)*(blue1 - blue));
            temp2 = (blue2 + (LVF_XPos - LVI_XPos1)*(blue3 - blue2));
            LVI_BValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            if (LVI_RValue> 255) {
                LVI_RValue = 255;
            } else if (LVI_RValue < 0) {
                LVI_RValue = 0;
            }

            if (LVI_GValue > 255) {
                LVI_GValue = 255;
            } else if (LVI_GValue < 0) {
                LVI_GValue = 0;
            }

            if (LVI_BValue > 255) {
                LVI_BValue = 255;
            } else if (LVI_BValue < 0) {
                LVI_BValue = 0;
            }

            bmp.setPixel(y, x, Color.rgb(LVI_RValue, LVI_GValue, LVI_RValue));

        }
    }
    return bmp;
}

public double GetDistXDist( int Point1X,int Point1Y, int Point2X,int Point2Y)
{
    double dist2 = (Math.pow((Point1X - Point2X), 2) + Math.pow((Point1Y - Point2Y), 2));
    return dist2;
}
 }

在我的code,我想改变形象的区域像素,面积为两个圈公共部分,

In my code,i want to change area pixel of image, the area is common part of two circle,

INT STARTX,诠释startY,诠释endX,INT恩迪是两个圆的中心,但是,当我运行的应用程序, 它给我

int startX,int startY ,int endX,int endY is the centres of two circles,But when i run the app, It give me

Caused by: java.lang.IllegalArgumentException: x must be < bitmap.width() 

我DONOT知道如何midify?

I donot know how to midify?

编辑:

 public class SlimmingView extends View implements OnTouchListener {

Paint paint = new Paint();
Paint paint1 = new Paint();
Bitmap bitmap;

Bitmap rotatedBitmap,SlimmingBitmap;


private int mPosX;
private int mPosY;

int screenWidth, screenHeight;

 int bitWidth, bitHeight;


int radius;

boolean isShow=false;

public SlimmingView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    PathEffect effects = new DashPathEffect(new float[] { 8, 8,8, 8 }, 1);
    paint.setPathEffect(effects);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.YELLOW);
    paint.setAntiAlias(true);


    paint1.setPathEffect(effects);
    paint1.setStyle(Paint.Style.STROKE);
    paint1.setStrokeWidth(2);
    paint1.setColor(Color.BLUE);
    paint1.setAntiAlias(true);
    radius = 20;
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);


}

@Override
public boolean onTouch(View v, MotionEvent event) {



    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        isShow=true;

        SlimmingBitmap=Slimming(bitmap,155, 235,165, 245, radius, 0.2f);
        invalidate();

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        break;
    }

    case MotionEvent.ACTION_UP: {

        break;
    }

    case MotionEvent.ACTION_CANCEL: {

        break;
    }

    }

    return true;

}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    if(isShow)
       {canvas.drawBitmap(SlimmingBitmap, 0, 0, null);}
    canvas.drawCircle(155, 235, radius, paint);
    canvas.drawCircle(165, 245, radius, paint1);
}

public Bitmap Slimming(Bitmap bmp,int startX,int startY ,int endX,int endY,int radius,float power)
{
    //int height = bmp.getHeight();
//  int width= bmp.getWidth();

//  Bitmap bmpSephia = Bitmap.createBitmap(width, height,bmp.getConfig());
//  Canvas canvas = new Canvas(bmpSephia);

//  canvas.drawBitmap(bmp, 0, 0, null);
    for (int x = endX + radius; x > endX - radius && x > 0; x--)
    {
        for (int y = endY + radius; y > endY - radius && y > 0; y--)
        {
            if (x > bmp.getWidth() || y > bmp.getHeight())
            {
                continue;
            }

            if (Math.pow((x - endX), 2) + Math.pow((y -endY), 2) > Math.pow(radius, 2))
            {
                // not in the cic
                continue;
            }

            double LVF_Modulus = 1 - GetDistXDist(x,y, startX,startY) / (radius * radius);
            LVF_Modulus = LVF_Modulus * LVF_Modulus *  LVF_Modulus;

            LVF_Modulus = LVF_Modulus * power;

            double LVF_XPos = x - LVF_Modulus * (endX - startX);
            double LVF_YPos = y - LVF_Modulus * (endY - startY);

            long LVI_XPos1 = (long) (LVF_XPos);
            long LVI_XPos2 = LVI_XPos1 + 1;
            long LVI_YPos1 = (long)(LVF_YPos);
            long LVI_YPos2 = LVI_YPos1 + 1;

            if (LVI_XPos1 >= bmp.getWidth() ||
                    LVI_YPos1 >= bmp.getHeight() ||
                    LVI_XPos2 >= bmp.getWidth() ||
                    LVI_YPos2 >= bmp.getHeight() ||
                    LVI_XPos1 < 0 ||
                    LVI_YPos1 < 0 ||
                    LVI_XPos2 < 0 ||
                    LVI_YPos2 < 0 )
                {
                    continue;
                }


            int LVI_RValue = 0;
            int LVI_GValue = 0;
            int LVI_BValue = 0;
             double temp1 = 0;
             double temp2 = 0;

            int c = bmp.getPixel((int)LVI_YPos1, (int)LVI_XPos1);
                // a= Color.alpha(c);
                int red = Color.red(c);
                int green = Color.green(c);
                int blue = Color.blue(c);

            int c1 = bmp.getPixel((int)LVI_YPos1, (int)LVI_XPos2);  
            int red1 = Color.red(c1);
            int green1 = Color.green(c1);
            int blue1 = Color.blue(c1);

            int c2 = bmp.getPixel((int)LVI_YPos2, (int)LVI_XPos1);  
            int red2 = Color.red(c2);
            int green2 = Color.green(c2);
            int blue2 = Color.blue(c2);


            int c3 = bmp.getPixel((int)LVI_YPos2, (int)LVI_XPos2);  
            int red3 = Color.red(c3);
            int green3 = Color.green(c3);
            int blue3 = Color.blue(c3);

            temp1 = (red + (LVF_XPos - LVI_XPos1)*(red1 - red));
            temp2 = (red2 + (LVF_XPos - LVI_XPos1)*(red3 - red2));
            LVI_RValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            temp1 = (green + (LVF_XPos - LVI_XPos1)*(green1 - green));
            temp2 = (green2 + (LVF_XPos - LVI_XPos1)*(green3 - green2));
            LVI_GValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            temp1 = (blue + (LVF_XPos - LVI_XPos1)*(blue1 - blue));
            temp2 = (blue2 + (LVF_XPos - LVI_XPos1)*(blue3 - blue2));
            LVI_BValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            if (LVI_RValue> 255) {
                LVI_RValue = 255;
            } else if (LVI_RValue < 0) {
                LVI_RValue = 0;
            }

            if (LVI_GValue > 255) {
                LVI_GValue = 255;
            } else if (LVI_GValue < 0) {
                LVI_GValue = 0;
            }

            if (LVI_BValue > 255) {
                LVI_BValue = 255;
            } else if (LVI_BValue < 0) {
                LVI_BValue = 0;
            }

            bmp.setPixel(y, x, Color.rgb(LVI_RValue, LVI_GValue, LVI_RValue));

        }
    }
    return bmp;
}

public double GetDistXDist( int Point1X,int Point1Y, int Point2X,int Point2Y)
{
    double dist2 = (Math.pow((Point1X - Point2X), 2) + Math.pow((Point1Y - Point2Y), 2));
    return dist2;
}

}

推荐答案

修改的方法是确定

public Bitmap Slimming(Bitmap bmp,int startX,int startY ,int endX,int endY,int radius,float power)
{
    //int height = bmp.getHeight();
//  int width= bmp.getWidth();

//  Bitmap bmpSephia = Bitmap.createBitmap(width, height,bmp.getConfig());
//  Canvas canvas = new Canvas(bmpSephia);

//  canvas.drawBitmap(bmp, 0, 0, null);
    for (int x = endX + radius; x > endX - radius && x > 0; x--)
    {
        for (int y = endY + radius; y > endY - radius && y > 0; y--)
        {
            if (x > bmp.getWidth() || y > bmp.getHeight())
            {
                continue;
            }

            if (Math.pow((x - endX), 2) + Math.pow((y -endY), 2) > Math.pow(radius, 2))
            {
                // not in the cic
                continue;
            }

            double LVF_Modulus = 1 - GetDistXDist(x,y, startX,startY) / (radius * radius);
            LVF_Modulus = LVF_Modulus * LVF_Modulus *  LVF_Modulus;

            LVF_Modulus = LVF_Modulus * power;

            double LVF_XPos = x - LVF_Modulus * (endX - startX);
            double LVF_YPos = y - LVF_Modulus * (endY - startY);

            int LVI_XPos1 = (int)(LVF_XPos);
            int LVI_XPos2 = LVI_XPos1 + 1;
            int LVI_YPos1 = (int)(LVF_YPos);
            int LVI_YPos2 = LVI_YPos1 + 1;

            if (LVI_XPos1 >= bmp.getWidth() ||
                    LVI_YPos1 >= bmp.getHeight() ||
                    LVI_XPos2 >= bmp.getWidth() ||
                    LVI_YPos2 >= bmp.getHeight() ||
                    LVI_XPos1 < 0 ||
                    LVI_YPos1 < 0 ||
                    LVI_XPos2 < 0 ||
                    LVI_YPos2 < 0 )
                {
                    continue;
                }


            int LVI_RValue = 0;
            int LVI_GValue = 0;
            int LVI_BValue = 0;
             double temp1 = 0;
             double temp2 = 0;

            int c = bmp.getPixel((int)LVI_XPos1, (int)LVI_YPos1);
                // a= Color.alpha(c);
                int red = Color.red(c);
                int green = Color.green(c);
                int blue = Color.blue(c);

            int c1 = bmp.getPixel((int)LVI_XPos1, (int)LVI_YPos2);  
            int red1 = Color.red(c1);
            int green1 = Color.green(c1);
            int blue1 = Color.blue(c1);

            int c2 = bmp.getPixel((int)LVI_XPos2, (int)LVI_YPos1);  
            int red2 = Color.red(c2);
            int green2 = Color.green(c2);
            int blue2 = Color.blue(c2);


            int c3 = bmp.getPixel((int)LVI_XPos2, (int)LVI_YPos2);  
            int red3 = Color.red(c3);
            int green3 = Color.green(c3);
            int blue3 = Color.blue(c3);

            temp1 = (red + (LVF_XPos - LVI_XPos1)*(red1 - red));
            temp2 = (red2 + (LVF_XPos - LVI_XPos1)*(red3 - red2));
            LVI_RValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            temp1 = (green + (LVF_XPos - LVI_XPos1)*(green1 - green));
            temp2 = (green2 + (LVF_XPos - LVI_XPos1)*(green3 - green2));
            LVI_GValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            temp1 = (blue + (LVF_XPos - LVI_XPos1)*(blue1 - blue));
            temp2 = (blue2 + (LVF_XPos - LVI_XPos1)*(blue3 - blue2));
            LVI_BValue = (int)(temp1 + (LVF_YPos - LVI_YPos1)*(temp2 - temp1));

            if (LVI_RValue> 255) {
                LVI_RValue = 255;
            } else if (LVI_RValue < 0) {
                LVI_RValue = 0;
            }

            if (LVI_GValue > 255) {
                LVI_GValue = 255;
            } else if (LVI_GValue < 0) {
                LVI_GValue = 0;
            }

            if (LVI_BValue > 255) {
                LVI_BValue = 255;
            } else if (LVI_BValue < 0) {
                LVI_BValue = 0;
            }

            bmp.setPixel(y, x, Color.rgb(LVI_RValue, LVI_GValue, LVI_RValue));

        }
    }
    return bmp;
}

这篇关于致:java.lang.IllegalArgumentException:如果x必须&LT; bitmap.width()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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