It's a bit hard to explain my problem so i'll show it with few pictures. I made this bitmap :
and I wanted to draw a circle of light inside it, using this code:
Rect bounds1 = new Rect(x, y, x+bit.getWidth(), y+bit.getHeight());
for (int i = bounds1.left; i < bounds1.right; i++) {
for (int j = bounds1.top; j < bounds1.bottom; j++) {
int result=(int) Math.sqrt((x2-i)*(x2-i)+(y2-j)*(y2-j));
if(result<=120){
int pixel = (bit.getPixel(i-x,j-y));
int a,r, g, b;
int different=result/10;
//change the value of each component
a=Color.alpha(pixel);
r = Color.red(pixel) + rn;
g = Color.green(pixel) + gn;
b = Color.blue(pixel) + bn;
//r,g,b values which are out of the range 0 to 255 should set to 0 or 255
if (r >= 256) {
r = 255;
} else if (r < 0) {
r = 0;
}
if (g >= 256) {
g = 255;
} else if (g < 0) {
g = 0;
}
if (b >= 256) {
b = 255;
} else if (b < 0) {
b = 0;
}
bit.setPixel(i-x, j-y, Color.argb(a,r, g, b));
}
}
I managed to create this:
Now, I'm trying to make the light ball be smoother and not so rough so it wont look like a circle, anyone has any idea how can I do that? I tried many things but nothing worked out.
whole code of drawing:
public class CreatorView extends View
{
Context c;
boolean quickfix=false;
boolean bin_isEmpty=true;
boolean iftrue=false;
boolean stopnow=false;
boolean buttonpressed=false;
String arrowcheck="";
int screenw;
int screenh;
int pixelx;
int pixely;
int smallpixelx;
int smallpixely;
Point arrowsp;
Paint paintblock;
int currentColor=0;
int x2=120;
int y2=120;
int rn=60;
int gn=45;
int bn=30;
Bitmap tileBitmap;
Bitmap lightBitmap;
RectF lightRect;
Paint paint0 = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap mountaintop;
Bitmap mountainbottom;
Bitmap grass;
Bitmap dirt;
Bitmap dirt2;
Bitmap dirt3;
Bitmap stonegrass;
Bitmap stone;
Bitmap stone2;
Bitmap stone3;
Bitmap stone4;
Bitmap cloud;
Bitmap bin_Empty;
Bitmap bin_Full;
Bitmap arrowno;
Bitmap arrown;
Bitmap arrowl;
Bitmap arrowr;
Bitmap arrowu;
Bitmap arrowd;
Bitmap arrowul;
Bitmap arrowur;
Bitmap save;
Bitmap fliph;
Bitmap flipv;
Bitmap Rotatef;
Bitmap Rotateb;
Bitmap arrowdl;
Bitmap arrowdr;
Bitmap grassSide;
Bitmap grassTop;
Bitmap orange;
Bitmap blue;
Bitmap smallpixel;
Bitmap PixelDetect;
Bitmap colorpick;
Bitmap brush;
Bitmap brushcolor;
Bitmap torch;
Map.Entry<CreatorBlock,Point>currentBlock;
Map.Entry<CreatorBlock,Point>lastBlock;
Map<CreatorBlock, Point> grassblock = new HashMap<CreatorBlock, Point>();
Map<CreatorBlock, Point> stonegrassblock = new HashMap<CreatorBlock, Point>();
Map<CreatorBlock, Point> dirtblock = new HashMap<CreatorBlock, Point>();
Map<CreatorBlock, Point> stoneblock = new HashMap<CreatorBlock, Point>();
Map<CreatorBlock, Point> grassSideBlock = new HashMap<CreatorBlock, Point>();
Map<CreatorBlock, Point> grassTopBlock = new HashMap<CreatorBlock, Point>();
Map<CreatorBlock, Point> orangeBlock = new HashMap<CreatorBlock, Point>();
Map<CreatorBlock, Point> blueBlock = new HashMap<CreatorBlock, Point>();
Map<BackBlock, Point> mountaintopBack = new HashMap<BackBlock, Point>();
Map<BackBlock, Point> mountainbottomBack = new HashMap<BackBlock, Point>();
Map<BackBlock, Point> torchBack = new HashMap<BackBlock, Point>();
Map<CreatorBlock, Point> levelMap = new HashMap<CreatorBlock, Point>();
List<MenuButton>menuButtonList=new ArrayList<MenuButton>();
public CreatorView(Context c) {
super(c);
this.c=c;
WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
this.screenw= display.getWidth();
this.screenh=display.getHeight();
this.PixelDetect = BitmapFactory.decodeResource( getResources(), R.drawable.custom_pixel);
this.smallpixel = Bitmap.createScaledBitmap(PixelDetect, 3, 3, false);
this.grass=BitmapFactory.decodeResource(getResources(), R.drawable.block_grass);
this.grassSide=BitmapFactory.decodeResource(getResources(), R.drawable.block_grassside);
this.grassTop=BitmapFactory.decodeResource(getResources(), R.drawable.block_grasstop);
this.orange=BitmapFactory.decodeResource(getResources(), R.drawable.block_cube1);
this.blue=BitmapFactory.decodeResource(getResources(), R.drawable.block_bluecube);
this.dirt=BitmapFactory.decodeResource(getResources(), R.drawable.block_dirt);
this.dirt2=BitmapFactory.decodeResource(getResources(), R.drawable.block_dirt2);
this.dirt3=BitmapFactory.decodeResource(getResources(), R.drawable.block_dirt3);
this.stonegrass=BitmapFactory.decodeResource(getResources(), R.drawable.block_stonegrass);
this.stone=BitmapFactory.decodeResource(getResources(), R.drawable.block_stone);
this.stone2=BitmapFactory.decodeResource(getResources(), R.drawable.block_stone2);
this.stone3=BitmapFactory.decodeResource(getResources(), R.drawable.block_stone3);
this.stone4=BitmapFactory.decodeResource(getResources(), R.drawable.block_stone4);
this.mountaintop=BitmapFactory.decodeResource(getResources(), R.drawable.back_mountaintop);
this.mountainbottom=BitmapFactory.decodeResource(getResources(), R.drawable.back_mountainbottom);
this.arrowno=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_noclick);
this.arrown=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_normal);
this.arrowl=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_left);
this.arrowr=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_right);
this.arrowu=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_up);
this.arrowd=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_down);
this.arrowul=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_upperleft);
this.arrowur=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_upperright);
this.arrowdl=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_downleft);
this.arrowdr=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_downright);
this.arrowno=Bitmap.createScaledBitmap(arrowno, arrowno.getWidth()*3, arrowno.getHeight()*3, false);
this.save=BitmapFactory.decodeResource(getResources(), R.drawable.button_save);
this.brush=BitmapFactory.decodeResource(getResources(), R.drawable.menu_brush);
this.brushcolor=BitmapFactory.decodeResource(getResources(), R.drawable.menu_brush_color);
this.colorpick=BitmapFactory.decodeResource(getResources(), R.drawable.menu_colorpicker);
this.torch=BitmapFactory.decodeResource(getResources(), R.drawable.item_torch);
this.save=BitmapFactory.decodeResource(getResources(), R.drawable.button_save);
this.fliph=BitmapFactory.decodeResource(getResources(), R.drawable.menu_fliph);
this.flipv=BitmapFactory.decodeResource(getResources(), R.drawable.menu_flipv);
this.Rotatef=BitmapFactory.decodeResource(getResources(), R.drawable.menu_rotatef);
this.Rotateb=BitmapFactory.decodeResource(getResources(), R.drawable.menu_rotateb);
this.bin_Empty=BitmapFactory.decodeResource(getResources(), R.drawable.bin_empty);
this.bin_Full=BitmapFactory.decodeResource(getResources(), R.drawable.bin_full);
this.bin_Empty=Bitmap.createScaledBitmap(bin_Empty, bin_Empty.getWidth()*3, bin_Empty.getHeight()*3, false);
this.bin_Full=Bitmap.createScaledBitmap(bin_Full, bin_Full.getWidth()*3, bin_Full.getHeight()*3, false);
this.brush=Bitmap.createScaledBitmap(brush, brush.getWidth()*3, brush.getHeight()*3, false);
this.brushcolor=Bitmap.createScaledBitmap(brushcolor, brushcolor.getWidth()*3, brushcolor.getHeight()*3, false);
this.colorpick=Bitmap.createScaledBitmap(colorpick, colorpick.getWidth()*3, colorpick.getHeight()*3, false);
this.fliph=Bitmap.createScaledBitmap(fliph, fliph.getWidth()*3, fliph.getHeight()*3, false);
this.flipv=Bitmap.createScaledBitmap(flipv, flipv.getWidth()*3, flipv.getHeight()*3, false);
this.Rotateb=Bitmap.createScaledBitmap(Rotateb, Rotateb.getWidth()*3, Rotateb.getHeight()*3, false);
this.Rotatef=Bitmap.createScaledBitmap(Rotatef, Rotatef.getWidth()*3, Rotatef.getHeight()*3, false);
this.arrown=Bitmap.createScaledBitmap(arrown, arrown.getWidth()*3, arrown.getHeight()*3, false);
this.arrowl=Bitmap.createScaledBitmap(arrowl, arrowl.getWidth()*3, arrowl.getHeight()*3, false);
this.arrowr=Bitmap.createScaledBitmap(arrowr, arrowr.getWidth()*3, arrowr.getHeight()*3, false);
this.arrowu=Bitmap.createScaledBitmap(arrowu, arrowu.getWidth()*3, arrowu.getHeight()*3, false);
this.arrowd=Bitmap.createScaledBitmap(arrowd, arrowd.getWidth()*3, arrowd.getHeight()*3, false);
this.arrowul=Bitmap.createScaledBitmap(arrowul, arrowul.getWidth()*3, arrowul.getHeight()*3, false);
this.arrowur=Bitmap.createScaledBitmap(arrowur, arrowur.getWidth()*3, arrowur.getHeight()*3, false);
this.arrowdl=Bitmap.createScaledBitmap(arrowdl, arrowdl.getWidth()*3, arrowdl.getHeight()*3, false);
this.arrowdr=Bitmap.createScaledBitmap(arrowdr, arrowdr.getWidth()*3, arrowdr.getHeight()*3, false);
Menu_Add(arrowno,0,true,"arrows");
Menu_Add(bin_Empty,1,false,"bin");
Menu_Add(brush,2,false,"brush");
Menu_Add(brushcolor,2,false,"brushcolor");
Menu_Add(colorpick,3,false,"colorpick");
Menu_Add(Rotateb,4,false,"rotateb");
Menu_Add(Rotatef,5,false,"rotatef");
Menu_Add(fliph,6,false,"fliph");
Menu_Add(flipv,7,false,"flipv");
Menu_Add(grassTop,1,true,"grasstop");
Menu_Add(grassSide,2,true,"grassside");
Menu_Add(grass,3,true,"grass");
Menu_Add(dirt,4,true,"dirt");
Menu_Add(stonegrass,5,true,"stonegrass");
Menu_Add(stone,6,true,"stone");
Menu_Add(orange,7,true,"orange");
Menu_Add(blue,8,true,"blue");
Menu_Add(mountaintop,9,true,"mountaintop");
Menu_Add(mountainbottom,10,true,"mountainbottom");
Menu_Add(torch,11,true,"torch");
arrowsp=new Point();
arrowsp.x=0;
arrowsp.y=0;
tileBitmap = grass;
}
private void Menu_Add(Bitmap b,int order,boolean vertical,String name)
{
Point p=new Point();
if(order==0){
p.x=0;
p.y=0;
MenuButton m=new MenuButton(order,b , vertical, p,name);
menuButtonList.add(m);
}
else{
for (MenuButton m : menuButtonList) {
if((m.isVertical()==vertical||order==1)&&m.getOrder()+1==order ){
if(vertical){
p.x=0;
p.y=m.getP().y+m.getBit().getHeight()+2;
}
else{
p.x=m.getP().x+m.getBit().getWidth()+2;
p.y=0;
}
MenuButton m2=new MenuButton(order,b , vertical, p,name);
menuButtonList.add(m2);
return;
}
}
}
}
public void drawLight(){
lightBitmap = Bitmap.createBitmap(1500, 15000, Bitmap.Config.ARGB_8888);
Canvas cc = new Canvas(lightBitmap);
Paint pp = new Paint(Paint.ANTI_ALIAS_FLAG);
pp.setMaskFilter(new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL));
for (Map.Entry<BackBlock, Point> entry : torchBack.entrySet()) {
cc.drawCircle(entry.getValue().x+6, entry.getValue().y+55, 25, pp);
}
lightRect = new RectF(-256, -256, lightBitmap.getWidth(), lightBitmap.getHeight());
lightRect.offset(200, 200);
float[] array = {
1.2f, 0, 0, 0, 1,
0,1.2f, 0, 0, 1,
0, 0, 1.2f, 0, 1,
0, 0, 0, 1, 0,
};
ColorMatrix ccc=new ColorMatrix(array);
ccc.setScale(2, 1.5f, 1f, 1f);
ColorMatrixColorFilter c4=new ColorMatrixColorFilter(ccc);
paint0.setColorFilter(c4);
paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawLight();
Paint paintAlpha = new Paint();
paintAlpha.setAlpha(200);
canvas.drawARGB(255, 86, 194, 243);
drawTiles(canvas, paint0);
drawTiles(canvas, null);
for(MenuButton m : menuButtonList){
switch(m.getName()){
case "bin":
if(bin_isEmpty){
canvas.drawBitmap(bin_Empty, m.getP().x, m.getP().y,paintAlpha);
}
else{
canvas.drawBitmap(bin_Full, m.getP().x, m.getP().y,paintAlpha);
}
break;
case "brushcolor":
canvas.drawBitmap(m.getBit(), m.getP().x,m.getP().y, changeBitmapColor(m.getBit(), currentColor));
break;
case "arrows":
canvas.drawBitmap(m.getBit(),m.getP().x,m.getP().y,paintAlpha);
switch (arrowcheck) {
case "normal":
canvas.drawBitmap(arrown, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "left":
canvas.drawBitmap(arrowl, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "right":
canvas.drawBitmap(arrowr, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "down":
canvas.drawBitmap(arrowd, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "up":
canvas.drawBitmap(arrowu, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "upleft":
canvas.drawBitmap(arrowul, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "upright":
canvas.drawBitmap(arrowur, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "downleft":
canvas.drawBitmap(arrowdl, arrowsp.x, arrowsp.y,paintAlpha);
break;
case "downright":
canvas.drawBitmap(arrowdr, arrowsp.x, arrowsp.y,paintAlpha);
break;
}
break;
default:
canvas.drawBitmap(m.getBit(),m.getP().x,m.getP().y,paintAlpha);
break;
}
Paint paintRec = new Paint();
paintRec.setColor(Color.RED);
paintRec.setStyle(Style.STROKE);
paintRec.setStrokeWidth(4);
paintRec.setStrokeCap(Cap.ROUND);
paintRec.setStrokeJoin(Join.ROUND);
paintRec.setPathEffect(new DashPathEffect(new float[] {30,40}, 0));
paintRec.setAlpha(5);
if(currentBlock!=null)
canvas.drawRect(currentBlock.getValue().x,currentBlock.getValue().y,currentBlock.getValue().x+currentBlock.getKey().getBit().getWidth(),currentBlock.getValue().y+currentBlock.getKey().getBit().getHeight(),paintRec);
}
canvas.saveLayer(lightRect, null, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
canvas.clipRect(lightRect);
drawTiles(canvas, paint0);
canvas.drawBitmap(lightBitmap, null, lightRect, paint1);
canvas.restore();
}
void drawTiles(Canvas canvas, Paint p) {
for (Map.Entry<BackBlock, Point> entry : mountainbottomBack.entrySet()) {
//Bitmap lighten=Bitmap.createScaledBitmap(entry.getKey().getBit(), entry.getKey().getBit().getWidth(), entry.getKey().getBit().getHeight(), false);
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<BackBlock, Point> entry : mountaintopBack.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : grassblock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : stonegrassblock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : dirtblock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : stoneblock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : grassSideBlock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : grassTopBlock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : orangeBlock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<CreatorBlock, Point> entry : blueBlock.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
for (Map.Entry<BackBlock, Point> entry : torchBack.entrySet()) {
canvas.drawBitmap(entry.getKey().getBit(), entry.getValue().x,entry.getValue().y, p);
}
}