Android 如何淡入一张图像淡出第二张图像?
我找到了这个教程,其中淡入淡出相同的图像如何更改此代码以使用两个图像淡入淡出1图像自动淡出2Android 如何淡入一张图像淡出第二张图像?,android,Android,我找到了这个教程,其中淡入淡出相同的图像如何更改此代码以使用两个图像淡入淡出1图像自动淡出2 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; impor
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class AndroidAnimTranslateActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonFadeOut = (Button)findViewById(R.id.fadeout);
final ImageView image = (ImageView)findViewById(R.id.image);
Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
image.startAnimation(animationFadeIn);
animationFadeIn.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeOut);
}
});
final Animation animationFadeOut = AnimationUtils.loadAnimation(this,
R.anim.fadeout);
buttonFadeOut.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeOut);
}});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/fadein"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fade In"/>
<Button
android:id="@+id/fadeout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fade Out"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
<!-------fadein.xml-->>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="2000"
/>
</set>
<!--------fadeout.xml----->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"
/>
</set>
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
导入android.widget.Button;
导入android.widget.ImageView;
公共类AndroidAnimTranslateActivity扩展了活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Button fadeout=(按钮)findViewById(R.id.fadeout);
最终ImageView图像=(ImageView)findViewById(R.id.image);
Animation animationFadeIn=AnimationUtils.loadAnimation(this,R.anim.fadein);
图像.startAnimation(animationFadeIn);
setAnimationListener(新的AnimationListener(){
@凌驾
onAnimationStart上的公共无效(动画){
//TODO自动生成的方法存根
}
@凌驾
onAnimationRepeat上的公共无效(动画){
//TODO自动生成的方法存根
}
@凌驾
onAnimationEnd上的公共无效(动画){
//TODO自动生成的方法存根
图像。开始动画(动画淡出);
}
});
最终动画animationFadeOut=AnimationUtils.loadAnimation(此,
R.anim.fadeout);
buttonFadeOut.setOnClickListener(新建Button.OnClickListener()){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
图像。开始动画(动画淡出);
}});
}
}
>
我认为您应该使用Animationlistener:
image.startAnimation(animationFadeIn);
animationFadeIn.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeOut);
}
});
所以第二个动画将开始,而第一个动画将结束您可以使用ImageSwitcher。[这里][1]你可以找到一个例子。[1] :Declare final Animation Animation fadeout=AnimationUtils.loadAnimation(this,R.anim.fadeout);开始动画之前(动画淡出);我认为您应该调用buttonFadeIn的onClick,如果有两个不同的ImageView,您可以使用image2.startAnimation()。或者,如果您有相同的imageview,并且希望动态更改其源,则可以在image.startAnimation(animationFadeOut)之前添加该行;线路。如果有,一定要告诉我problem@LovelyGuy这是正确的答案。如果要在同一个实例中为它们设置动画,则在onAnimationStart方法中简单调用第二个动画对我没有帮助,找不到setAnimationListener(new AnimationListener(){类型animation中的方法setAnimationListener(animation.AnimationListener)不适用于参数(new AnimationListener(){})@LovelyGuy是否存在其他编译器错误,即重写侦听器的方法,或者说导入侦听器?