Android图形动画 使用ScaleAnimation
使用ScaleAnimation实现了一个类似于翻转的动画效果。
感觉ScaleAnimation算是一个比较好用的动画类了,看了一下API感觉方法和构造方法也都很简单。
就不再赘述太多直接上代码吧- -
第一步:
准备两张照片,放置在res/drawble下。
首先在layout中写好布局文件,这里要用framelayout布局,让两张图片一张覆盖在另一张上。
相信聪明的你看到这里已经秒懂等下的图片处理方式了。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dfanzhuan.MainActivity" > <ImageView android:id="@+id/ivA" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/image_a" /> <ImageView android:id="@+id/ivB" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/image_b" /> </FrameLayout>
第二部:
MainActivity.java
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.ScaleAnimation; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imgA; private ImageView imgB; private ScaleAnimation sato0 = new ScaleAnimation(1, 0, 1, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); private ScaleAnimation sato1 = new ScaleAnimation(0, 1, 1, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); findViewById(R.id.root).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (imgA.getVisibility() == View.VISIBLE) { imgA.startAnimation(sato0); }else { imgB.startAnimation(sato0); } } }); } private void showImageA() { imgA.setVisibility(View.VISIBLE); imgB.setVisibility(View.INVISIBLE); } private void showImageB() { imgA.setVisibility(View.INVISIBLE); imgB.setVisibility(View.VISIBLE); } private void initView() { //指定执行时间 imgA = (ImageView) findViewById(R.id.ivA); imgB = (ImageView) findViewById(R.id.ivB); showImageA(); //动画执行时间 sato0.setDuration(500); sato1.setDuration(500); sato0.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) { if (imgA.getVisibility() == View.VISIBLE) { imgA.setAnimation(null); showImageB(); imgB.startAnimation(sato1); } else { imgB.setAnimation(null); showImageA(); imgA.startAnimation(sato1); } } }); } }
个人感觉难点在于对时间和图片翻转情况的理解,不过写几次以后就just soso了~
下面是效果图,因为还不会做git。。。所以发四张好了:
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28