Android 动画效果实例

本文详细介绍如何在Android应用中实现各种动画效果,包括系统自带的渐显渐隐动画及自定义动画,如摇动效果等,并通过实例展示如何在ViewFlipper中实现背景图片的切换动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家都应该知道,当我们在应用中没有动画效果的话,那么你的应用做的真的会没有一个人玩,这个我都不用多说什么,大家就应该明白了,当我们的应用有了很炫的动画时,你这个就是一个比较不错的应用了,因为大多数的人还是比较爱看动画的,那么我们今天就教大家怎么样才能实现动画效果,废话不多说,来看看代码吧:

1.在图片显示过程中使用动画效果,可以给人一种感觉。比如渐进渐出的效果。

Java代码:
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);

mSwitcher.setFactory(this);

mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));

mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
复制代码 android.R.anim.fade_in, android.R.anim.fade_out两种动画效果是系统自带的效果。

2.下面介绍自定义的动画效果。

Java代码:
// 实现动画效果

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);

findViewById(R.id.pw).startAnimation(shake);
复制代码 里面用到的shake.xml文件,存放在anim目录下面。代码如下:

Java代码:<translate xmlns:android="http://schemas.android.com/apk/res/android"

android:fromXDelta="0"

android:toXDelta="10"

android:duration="1000"

android:interpolator="@anim/cycle_7" />
复制代码 而里面的cycle_7.xml,代码如下所示:

Java代码:
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"

android:cycles="7"

/>
复制代码 下面介绍APIDEMO中的动画效果。

第一种ViewFlipper中各背景图片的切换效果。

Java代码:
public class Animation2 extends Activity implements


AdapterView.OnItemSelectedListener {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.animation_2);

mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));

mFlipper.startFlipping();

Spinner s = (Spinner) findViewById(R.id.spinner);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_spinner_item, mStrings);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

s.setAdapter(adapter);

s.setOnItemSelectedListener(this);

}


public void onItemSelected(AdapterView parent, View v, int position, long id) {

switch (position) {

case 0:

mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in));

mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_out));

break;


case 1:

mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));

mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));

break;


case 2:

mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));

mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

break;


default:

mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_in));

mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_out));

break;

}

}


public void onNothingSelected(AdapterView parent) {

}

private String[] mStrings = { "Push up", "Push left", "Cross fade", "Hyperspace"};

private ViewFlipper mFlipper;

}
复制代码
animation_2.xml文件如下:

Java代码:
<ViewFlipper

android:id="@+id/flipper"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:flipInterval="2000"

android:layout_marginBottom="20dip" >


<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:textSize="26sp"

android:text="@string/animation_2_text_1"/>


<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:textSize="26sp"

android:text="@string/animation_2_text_2"/>


<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:textSize="26sp"

android:text="@string/animation_2_text_3"/>


<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:textSize="26sp"

android:text="@string/animation_2_text_4"/>

</ViewFlipper>


<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="5dip"

android:text="@string/animation_2_instructions"

/>


<Spinner

android:id="@+id/spinner"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值