当前位置:网站首页>Set the transparency of the picture to gradient from left to right

Set the transparency of the picture to gradient from left to right

2022-06-25 12:17:00 User 9854323

One 、 Set the transparency of the picture to gradient from left to right

/**
     *  Set the transparency of the picture to gradient from left to right , Make a smooth transition to the right edge ( Be careful to follow only x Coordinate change )
     *
     * @param num  The scope is 0-100,0 For full transparency ,100 For opacity 
     */
    public static Bitmap getTransAlphaBitmap(Bitmap sourceImg, float num) {
        if (sourceImg == null) {
            return null;
        }

        int width = sourceImg.getWidth();
        int height = sourceImg.getHeight();
        int[] bitArr = new int[width * height];

        sourceImg.getPixels(bitArr, 0, width, 0, 0, width, height);

        float number = num;
        int start = width / DEFAULT_PERCENT;
        float range = width - start;
        try {
            float step = (number * 1.0f) / range;
            for (int j = 0; j < height; j++) {
                number = num;
                for (int i = start + j * width; i < width * (j + 1); i++) {
                    number = number - step;
                    float alpha = number * ALPAH_FULL / NUMBER_FULL;
                    bitArr[i] = ((int) alpha << DEFAULT_BIT) | (bitArr[i] & DEFAULT_ALPHA);
                }
            }
            return Bitmap.createBitmap(bitArr, width, height, Bitmap.Config.ARGB_8888);
        } catch (Throwable e) {
            return sourceImg;
        }
    }

Two 、 Set the transparency of the picture to gradient from top to bottom

/**
     *  Set the transparency of the picture to gradient from top to bottom , Make a smooth transition at the bottom edge ( Be careful to follow only Y Coordinate change )
     *
     * @param sourceImg
     * @return
     */
    public static Bitmap getTransAlphaBitmap(Bitmap sourceImg) {
        int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];

        sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg

                .getWidth(), sourceImg.getHeight());//  Get pictures of ARGB value 

        //number For the range of 0-100,0 For full transparency ,100 For opacity 
        float number = 100;
        // Transparency value 
        float alpha = number * 255 / 100;
        // The range of the image gradient ( Set the gradient from top to bottom for only half of the picture , There is no gradient on it , That is, the half close to the edge )
        float range = sourceImg.getHeight() / 2.0f;
        // Transparency gradient , Every time with Y The amount of coordinate change , Because at the end it will become 0
        float pos = (number * 1.0f) / range;
        // Subscript at the beginning of the loop , When does the setting change 
        int start = sourceImg.getWidth() * (sourceImg.getHeight() - (int) range);

        for (int i = start; i < argb.length; i++) {
            // The same line alpha The value does not change , Because with Y Coordinates change from top to bottom 
            if (i % sourceImg.getWidth() == 0) {
                number = number - pos;
                alpha = number * 255 / 100;
            }
            argb[i] = ((int) alpha << 24) | (argb[i] & 0x00FFFFFF);
        }

        sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg

                .getHeight(), Bitmap.Config.ARGB_8888);
        return sourceImg;
    }
原网站

版权声明
本文为[User 9854323]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251146400408.html