android等待曲线动画,android--AnimationDrawable实现等待动画效果

例如登陆页面中需要显示等待动画, Animation Drawable可以让你把一系列的图片像幻灯片一样一张一张地显示在屏幕上。虽然Animation Drawable的效果看起来像动画一样,但在实际的代码处理中应该将其视为Drawable,而事实上它继承于Drawable,其XML配置文件也是放置在res/drawable目录下。

代码:login_loading.xml

android:oneshot="false">

注释:(1)android:duration="100" 每个帧都会延时100ms

(2)如果android:oneshot属性设置为true,那么该Animation Drawable只会运行一次,并最后会定格在最后一帧上,要是android:oneshot属性设为false,那么便会无限循环地显示上面的这几个帧

MainActivity.java

package com.tbx.android.waiting;

import com.tbx.android.R;

public class MainActivity extends Activity {

private Button button ;

private View loginLoading;

private AnimationDrawable loadingAnimation;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

button = (Button)findViewById(R.id.button1);

loginLoading = (View)findViewById(R.id.login_loading);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if(loginLoading.getVisibility() == View.GONE){

loginLoading.setVisibility(View.VISIBLE);

loadingAnimation = (AnimationDrawable)loginLoading.getBackground();

loadingAnimation.start();

}else{

if(loadingAnimation!=null){

loadingAnimation.stop();

loginLoading.setVisibility(View.GONE);

}

}

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

注释: 开始播放动画 loadingAnimation.start()

mian.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:text="@string/hello_world"

tools:context=".MainActivity" />

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_marginRight="20dp"

android:layout_marginTop="48dp"

android:layout_toLeftOf="@+id/textView1"

android:text="Button" />

android:id="@+id/login_loading"

android:layout_width="135.0dip"

android:layout_height="135.0dip"

android:layout_gravity="center"

android:visibility="gone"

android:background="@anim/login_loading"/>

ok,运行后可以看到效果了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值