Du3
Du3

Reputation: 1422

Setting background image faded in background

I am trying to do a wiki like activity where there is a faded background image thats downloaded from a repo and fades in to the background behind a linear layout. There are three problems I am having.

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView 
        android:id="@+id/bg_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cropToPadding="true"
        android:scaleType="centerCrop" />
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
        android:textColor="#FFFFFF" >
        <LinearLayout>Several layouts within here....</LinearLayout>
    </LinearLayout>
</FrameLayout>

1) I can get the image to show, but some some elements within the LinearLayout are not transparent until I open another app, then go back to my app. I think there might be some weird view caching built into android thats causing things like linearlayouts to have a solid background.

2) I cannot get the image to be 'faded' in the background. I almost want to just darken the image so the text on top of it stands out more. Would this be opacity on the image?

3) The animation to fade in doesn't work. I am using an AsyncTask to call back to a listener to set the background. Here is the code I use in the activity, InfoActivity:

bg =(ImageView)findViewById(R.id.bg_image);
public void onBgImageResponse(Bitmap background) {
    if(background != null) {
        bg.setAlpha(50);
        bg.setImageBitmap(background);
        Animation myFadeInAnimation = AnimationUtils.loadAnimation(InfoActivity.this, R.anim.fadein_bg);
        bg.setBackgroundDrawable(new BitmapDrawable(this.getResources(), background));
        bg.startAnimation(myFadeInAnimation);
    }
}

In fadein_bg.xml I have:

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha android:fromAlpha="0.0" 
        android:toAlpha=".5" 
        android:interpolator="@android:anim/accelerate_interpolator"  
        android:duration="2000" android:repeatCount="infinite"/> 
</set>

Any help is greatly appreciated. I will put a bounty out if needed.

Upvotes: 2

Views: 3587

Answers (1)

Nick
Nick

Reputation: 6385

  1. This most likely isnt due to caching. I would check on if the image is actually being set properly. It sounds like your just not setting/refreshing your views properly.

  2. You'll need to set the alpha on this image: image.setAlpha(x); // x is the alpha you want

  3. Remove 'bg.setImageBitmap(background);' and set the alpha last.

In your fadedin xml try removing the repeatCount and interpolator and see if that helps.

Upvotes: 1

Related Questions