2022年7月24日

Android Generating grid transparent background (生成透明網格背景)



  public  static  void draw_alpha_background(Canvas canvas,int x,int y,int width,int height){
        Paint paint=new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);

        int color1= Color.WHITE;
        int color2=Color.GRAY;
        int temp;
        int right=x+width;
        int bottom=y+height;
        int index;
        for(int i=x;i<right;i+=8) {

            index=0;
            for(int j=y;j<bottom;j+=8) {

                if(index%2==0)paint.setColor(color1);
                else paint.setColor(color2);

                canvas.drawRect( i,  j , i + 8, j+8, paint);
                index++;
            }


            temp=color1;
            color1=color2;
            color2=temp;
        }

    }


How to use:

        imageView=findViewById(R.id.imageview);
        Bitmap grid_white_black_bg=Bitmap.createBitmap(200,200, Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(grid_white_black_bg);
        draw_alpha_background(canvas,0,0,200,200);
        imageView.setImageBitmap(grid_white_black_bg);


沒有留言:

張貼留言