美文网首页
Android开发实现图片渐变融入背景效果

Android开发实现图片渐变融入背景效果

作者: 隔壁家de老王 | 来源:发表于2019-09-29 16:07 被阅读0次

    效果图:


    15689482562051.png

    主要通过修改图片的alpha通道值实现,离图片中心点越近alpha值越大,最大255,距离中心点越远alpha值越小最小0

     public Bitmap changeAlpha(Bitmap bitmap){
            int w = bitmap.getWidth();
            int h = bitmap .getHeight();
            Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
    
            int ratio = w >  h ?  h * 32768 / w : w * 32768 /  h;
            int r,g,b,a,color;
    
            int[] oldPx = new int[w * h];
            int[] newPx = new int[w * h];
    
            float Size =0.5f;
            int cx = w >> 1;
            int cy = h >> 1;
            int max = cx * cx + cy * cy;
            int min = (int)(max * (2 - Size));
            int diff = max - min;
            bitmap.getPixels(oldPx, 0, w, 0, 0, w, h);
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
    
                    color = oldPx[x * h + y];
                    r = Color.red(color);
                    g = Color.green(color);
                    b = Color.blue(color);
                    a = Color.alpha(color);
    
                    int dx = cx - x;
                    int dy = cy - y;
                    if (w > h){
                        dx = (dx * ratio) >> 15;
                    }
                    else{
                        dy = (dy * ratio) >> 15;
                    }
    
                    int distSq = dx * dx + dy * dy;
                    float v =  ((float)distSq / diff) * 255;
                    a = (int)(a+(v));
                    a = (a > 255 ? 255 : (a < 0 ? 0 : a));
                    newPx[x * h + y] = Color.argb(a,r, g, b);
                }
            }
            result.setPixels(newPx, 0, w, 0, 0, w, h);
            return result;
        }
    

    相关文章

      网友评论

          本文标题:Android开发实现图片渐变融入背景效果

          本文链接:https://www.haomeiwen.com/subject/vszductx.html