android - 透明背景帧动画

标签 android animation

我想使用进度条之类的动画。

我有 xml 文件 prog_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
    android:drawable="@drawable/progress01"
    android:duration="100"/>
<item
    android:drawable="@drawable/progress02"
    android:duration="100"/>
<item
    android:drawable="@drawable/progress03"
    android:duration="100"/>
<item
    android:drawable="@drawable/progress04"
    android:duration="100"/>
<item
    android:drawable="@drawable/progress05"
    android:duration="100"/>
</animation-list>

文件 progress0x.png 看起来像这样(例如)

png with transparent background

此文件中的背景是透明的。

在 Activity 的布局 xml 我有 imageview

<RelativeLayout
    android:id="@+id/loadingPanel"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_logo2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/iv_pb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_logo2"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/app_name" />
</RelativeLayout>

然后在java文件中

AnimationDrawable ad;
ImageView iv_pb;
...
iv_pb = (ImageView)findViewById(R.id.iv_pb);
iv_pb.setBackgroundResource(R.drawable.prog_drawable);

ad = (AnimationDrawable)iv_pb.getBackground();
ad.start();

当我启动我的应用程序时,我看到了这个动画,但背景是黑色的。如何获得透明背景?

最佳答案

即使您将 android:background="@android:color/transparent" 设置为您的 ImageView 以获得透明背景,但调用 iv_pb.setBackgroundResource( R.drawable.prog_drawable); 覆盖它,导致你的 animation-drawable 被设置为新的背景。

因此,最终您需要在图像文件中实现透明度。确保所有文件都在 .png 中且背景设置为透明。

关于android - 透明背景帧动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117764/

相关文章:

ios - 动画 UIWebView 在动画期间不响应触摸

html - 如何使用 css3 动画将宽度和高度设置为 100%?

android - 打破阿拉伯语字体

jquery - 如何将图像形状动画化为梯形,希望使用 jquery 插件

css - div 悬停动画

android :layout_gravity 的 Java 方法

animation - 无法在一个循环结束时停止动画

android - 当应用程序被用户终止时,工作管理器不工作。为什么?

java - RecyclerView,notifyItemRemoved有时会删除无效项目,notifyDataSetChanged工作完美

android - 蓝牙低功耗通知间隔