淡入淡出的切换效果很常见呢!
无论是Html5,JQuery,都经常用到呢,
这首Android的动画效果:一个深入,一个淡入淡出
实现起来也很简单:
import android.app.Activity; import android.graphics.drawable.Drawable; import android.graphics.drawable.TransitionDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { private ImageView iv_alpha; private AlphaAnimation alphaAnimation; private TransitionDrawable transitionDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_play1 = (Button) findViewById(R.id.btn_play1); Button btn_play2 = (Button) findViewById(R.id.btn_play2); btn_play1.setOnClickListener(this); btn_play2.setOnClickListener(this); iv_alpha = (ImageView) findViewById(R.id.iv_alpha); //一开始先设置透明,这样图片不会显示,等点击按钮时再显示 iv_alpha.setAlpha(0.0f); alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(3000); //深浅动画持续时间 alphaAnimation.setFillAfter(true); //动画结束时保持结束的画面 } @Override public void onClick(View v) { if (v.getId() == R.id.btn_play1) { iv_alpha.setImageResource(R.drawable.a1); iv_alpha.setAlpha(1.0f); iv_alpha.setAnimation(alphaAnimation); alphaAnimation.start(); } else if (v.getId() == R.id.btn_play2) { //淡入淡出动画需要先设置一个Drawable数组,用于变换图片 Drawable[] drawableArray = { getResources().getDrawable(R.drawable.a1), getResources().getDrawable(R.drawable.a2) }; transitionDrawable = new TransitionDrawable(drawableArray); iv_alpha.setImageDrawable(transitionDrawable); transitionDrawable.startTransition(3000); } } }
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="example.com.cartoon_danrudanchu.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_play1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="播放深入动画"/> <Button android:id="@+id/btn_play2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="淡入淡出动画"/> </LinearLayout> <ImageView android:id="@+id/iv_alpha" android:layout_width="260dp" android:layout_height="320dp" android:layout_gravity="center" android:scaleType="fitXY" android:layout_marginTop="10dp"/> </LinearLayout>
感谢:“湖前琴亭”
博主 !
作者:ssh159 发表于2017/4/4 15:33:10 原文链接
阅读:113 评论:0 查看评论