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;
}
}
在我的代码中,我想改变图像的区域像素,该区域是两个圆圈的共同部分,
int startX,int startY ,int endX,int endY
是两个圈子的中心,但是当我运行应用程序时,
它给了我
Caused by: java.lang.IllegalArgumentException: x must be < 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;
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;
}
}
答案 0 :(得分:0)
在onMeasure()(或onLayout()之后,您的自定义视图将不会“知道”其大小,并且也可能在onSizeChanged()中更改。您需要在这些内部获取View维度,而不是在构造函数中。
答案 1 :(得分:0)
修改方法确定
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;
}