设置背景图像在背景中淡入淡出

发布于 2024-12-22 06:57:38 字数 1984 浏览 6 评论 0原文

我正在尝试做一个类似维基的活动,其中有一个从存储库下载的褪色背景图像,并淡入线性布局后面的背景。我遇到三个问题。

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)我可以显示图像,但 LinearLayout 中的某些元素并不透明,直到我打开另一个应用程序,然后返回我的应用程序。我认为 Android 中可能内置了一些奇怪的视图缓存,导致线性布局等内容具有坚实的背景。

2)我无法让图像在背景中“褪色”。我几乎想把图像变暗,这样上面的文字就更突出了。这会导致图像不透明吗?

3)淡入动画不起作用。我正在使用 AsyncTask 回调侦听器以设置背景。这是我在活动 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);
    }
}

在 fadein_bg.xml 中我有:

<?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>

非常感谢任何帮助。如果需要的话我会提供赏金。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2024-12-29 06:57:38
  1. 这很可能不是由于缓存造成的。我会检查图像是否确实设置正确。听起来您只是没有正确设置/刷新您的视图。

  2. 您需要在此图像上设置 Alpha:
    图像.setAlpha(x); // x 是您想要的 alpha

  3. Remove 'bg.setImageBitmap(background);'最后设置 alpha。

在您的 fadedin xml 中尝试删除重复计数和插值器,看看是否有帮助。

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文