进度条没有动画
Progress bar is not animating
我试图让进度条在 3 秒内淡出,但它不起作用。
// Animate progress bar.
progressBar1 = (ProgressBar) findViewById(R.id.mainImageView1);
ObjectAnimator animato = ObjectAnimator.ofFloat(progressBar1, "alpha", 1.0f, 0.0f);
animato.setInterpolator(new DecelerateInterpolator());
animato.setDuration(3000);
animato.start();
布局(xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#FFFFFF">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:id="@+id/mainImageView1"/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="48dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:indeterminate="true"
android:alpha="0.9"/>
</RelativeLayout>
最后,我"working"的意思是进度条不会消失。
使用 AlphaAnimation 代替 ObjectAnimator >>>
AlphaAnimation fade = new AlphaAnimation(1.0f, 0.0f); // from 1 to 0 transparancy
fade.setDuration(1000);
fade.setFillAfter(true) //Keeps ProgressBar at 0 opacity after animation
myProgressBar.startAnimation(fadeOutAnimation);
希望它有效:)
顺便说一句,你还没有为你的进度条分配 id,
为您的进度条分配 ID
然后声明一个名为 myProgressBar 的变量
然后用你的进度条对应的id初始化它。
您正在尝试使用以下行将 ImageView 转换为 ProgressBar:
progressBar1 = (ProgressBar) findViewById(R.id.mainImageView1);
R.id.mainImageView1 已分配给您 xml.
中的 ImageView
在您的 xml 中为 ProgressBar 分配一个 id :
<ProgressBar
...
android:id="@+id/progress_bar"
/>
然后
progressBar1 = (ProgressBar) findViewById(R.id.progress_bar);
我试图让进度条在 3 秒内淡出,但它不起作用。
// Animate progress bar.
progressBar1 = (ProgressBar) findViewById(R.id.mainImageView1);
ObjectAnimator animato = ObjectAnimator.ofFloat(progressBar1, "alpha", 1.0f, 0.0f);
animato.setInterpolator(new DecelerateInterpolator());
animato.setDuration(3000);
animato.start();
布局(xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#FFFFFF">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:id="@+id/mainImageView1"/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="48dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:indeterminate="true"
android:alpha="0.9"/>
</RelativeLayout>
最后,我"working"的意思是进度条不会消失。
使用 AlphaAnimation 代替 ObjectAnimator >>>
AlphaAnimation fade = new AlphaAnimation(1.0f, 0.0f); // from 1 to 0 transparancy
fade.setDuration(1000);
fade.setFillAfter(true) //Keeps ProgressBar at 0 opacity after animation
myProgressBar.startAnimation(fadeOutAnimation);
希望它有效:)
顺便说一句,你还没有为你的进度条分配 id, 为您的进度条分配 ID
然后声明一个名为 myProgressBar 的变量
然后用你的进度条对应的id初始化它。
您正在尝试使用以下行将 ImageView 转换为 ProgressBar:
progressBar1 = (ProgressBar) findViewById(R.id.mainImageView1);
R.id.mainImageView1 已分配给您 xml.
中的 ImageView在您的 xml 中为 ProgressBar 分配一个 id :
<ProgressBar
...
android:id="@+id/progress_bar"
/>
然后
progressBar1 = (ProgressBar) findViewById(R.id.progress_bar);