简体   繁体   English

框架动画有透明背景

[英]Frame animation with transparent background

I want to use animation like progress bar. 我想使用像进度条这样的动画。

I have the xml file prog_drawable.xml 我有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>

File progress0x.png looks like this (for example) 文件progress0x.png看起来像这样(例如)

png与透明背景

Background in this file is transparent. 此文件中的背景是透明的。

In activity's layout xml I have imageview 在活动的布局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>

Then in java file 然后在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();

When I launch my application I see this animation, but with black background. 当我启动我的应用程序时,我看到了这个动画,但是有黑色背景。 How can I get transparent background? 如何获得透明背景?

Even though you are setting android:background="@android:color/transparent" to your ImageView for transparent background, but calling iv_pb.setBackgroundResource(R.drawable.prog_drawable); 即使您将android:background="@android:color/transparent"ImageView以获得透明背景,但调用iv_pb.setBackgroundResource(R.drawable.prog_drawable); overrides this, causing your animation-drawable to be set as new background. 覆盖它,使您的动画可绘制设置为新背景。

So, eventually you need to implement transparency in your image files. 因此,最终您需要在图像文件中实现透明度。 Make sure all the files are in .png with their background set as transparent. 确保所有文件都在.png ,并将其背景设置为透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM