2016-07-13 3 views
2

어떻게 안드로이드 투명 그라디언트 배경의 선형 레이아웃을 제공 할 수 있습니까? 레이아웃에는 배경이 흐리게 표시되어 있지만 레이아웃의 맨 위에 약간 흐리게 표시해야합니다. 그런 다음 그 아래에있는 불투명으로 이동하십시오.그라디언트 투명도가 적용된 Android 선형 레이아웃

레이아웃이 시차 스크롤 뷰에 중첩되어 레이아웃이 헤더 이미지의 맨 위로 슬라이드 될 수 있습니다.

나는 흐린 당김과 같이 생성 :

final View content = img_header; 
    if (content.getWidth() > 0) { 
     Bitmap image = BlurBuilder.blur(content); 
     BitmapDrawable background = new BitmapDrawable(image); 

     ll_parent_layout.setBackground(background); 
    } else { 
     content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       Bitmap image = BlurBuilder.blur(content); 
       BitmapDrawable background = new BitmapDrawable(image); 
       ll_parent_layout.setBackground(background); 
      } 
     }); 
    } 

흐림 빌더는 다음과 같습니다

public class BlurBuilder { 
private static final float BITMAP_SCALE = 1f; 
private static final float BLUR_RADIUS = 25f; 

public static Bitmap blur(View v) { 
    return blur(v.getContext(), getScreenshot(v)); 
} 

public static Bitmap blur(Context ctx, Bitmap image) { 
    int width = Math.round(image.getWidth() * BITMAP_SCALE); 
    int height = Math.round(image.getHeight() * BITMAP_SCALE); 

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); 
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); 
    /// compress bitmap here 

    RenderScript rs = RenderScript.create(ctx); 
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); 
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); 
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); 
    theIntrinsic.setRadius(BLUR_RADIUS); 
    theIntrinsic.setInput(tmpIn); 
    theIntrinsic.forEach(tmpOut); 
    tmpOut.copyTo(outputBitmap); 

    return outputBitmap; 
} 

public static Bitmap blur(Context ctx, Bitmap image, float scale, float radius) { 
    int width = Math.round(image.getWidth() * scale); 
    int height = Math.round(image.getHeight() * scale); 

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); 
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); 

    RenderScript rs = RenderScript.create(ctx); 
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); 
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); 
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); 
    theIntrinsic.setRadius(radius); 
    theIntrinsic.setInput(tmpIn); 
    theIntrinsic.forEach(tmpOut); 
    tmpOut.copyTo(outputBitmap); 

    return outputBitmap; 
} 


private static Bitmap getScreenshot(View v) { 
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b); 
    v.draw(c); 
    return b; 
} 
} 
+0

드로어 블 코드를 게시 할 수 있습니까? –

+0

요청하신대로 @KaranMer 게시물을 편집했습니다. – MichaelStoddart

+0

당신은 이것을 위해 드로어 블 리소스를 사용하지 않으십니까? –

답변

1

흐림 이미지를 생성하기 위해 Bitmap을 조작하고 있으므로 LinearGradient 쉐이더를 사용하여 this answer처럼 투명도를 추가 할 수 있습니다.

public Bitmap addGradient(Bitmap src) { 
    int w = src.getWidth(); 
    int h = src.getHeight(); 
    Bitmap overlay = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(overlay); 

    canvas.drawBitmap(src, 0, 0, null); 

    Paint paint = new Paint(); 
    LinearGradient shader = new LinearGradient(0, 0, 0, h, 0xFFFFFFFF, 0x00FFFFFF, Shader.TileMode.REPEAT); 
    paint.setShader(shader); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
    canvas.drawRect(0, h - h, w, h, paint); 

    return overlay; 
} 
+0

매력처럼 일해 왔으며, 적어도 일주일 동안이 솔루션을 찾고있었습니다! – MichaelStoddart

+0

Im이 솔루션에 문제가 발생했습니다. 배경이 흐릿한 이미지가 검은 색 인 경우 그라데이션이 시작되는 선을 볼 수 있습니다. 이전에이 문제가 있었습니까? – MichaelStoddart

0

당신은 다음과 레이아웃에 그라데이션을 설정 당김 XML을 사용할 수 있습니다.

아래 파일의 코드를 mybg.xml이라고하고 drawable 폴더에 넣습니다. 이

mycolor1 -> #000000aa //this will give you transparent colored background 
mycolor2 -> #ff00aa00 //this will be opaque like background 

처음 두 문자가 여기에 색상의 알파 레벨을 설정 같은 색상

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
> 
    <gradient 
     android:startColor="@color/mycolor1" 
     android:endColor="@color/mycolor2" 
     android:angle="270" /> 
</shape> 

사용 8 문자의 해시 코드는 그래서 당신의 색이 될 수 있습니다.

0

는이 방법으로 수행 할 수 있습니다

그냥 투명 배경 그라데이션의 일부만을 할 수 있습니다 (> = 0 = 1 <) 비트 맵을 변경할 수 있는지, 도 사용 fadingPortion 값을을

public static Bitmap makeBitmapTransparent(Bitmap mutableBitmap, float fadingPortion) { 
    final Shader gradientShader = new LinearGradient(
      mutableBitmap.getWidth()/2, 0, 
      mutableBitmap.getWidth()/2, mutableBitmap.getHeight() * fadingPortion, 
      0x00000000, 0xFF000000, 
      Shader.TileMode.CLAMP); 

    Paint paint = new Paint(); 
    paint.setShader(gradientShader); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 

    Canvas canvas = new Canvas(mutableBitmap); 
    canvas.drawRect(new Rect(0, 0, mutableBitmap.getWidth(), (int) (mutableBitmap.getHeight() * FADING_PORTION)), paint); 
    canvas.drawBitmap(mutableBitmap, 0, 0, null); 

    return mutableBitmap; 
} 
+0

페이딩 부분으로 무엇을 전달합니까? – MichaelStoddart

+0

0 ~ 1의 범위의 부동 소수점 숫자 (예 : 0)는 상위 분기 만 기울임을 투명하게 나타냅니다. – Sharpe