快生活 - 生活常识大全

的方式实现动画效果


  步骤一
  创立.XML文件(如下图1所示),并将添加图片
  图 1
  步骤二
  将<selector修改成<animation-list
  <animation-list xmlns:android=""http://schemas.android.com/apk/res/android"" android:oneshot=""true""><item android:duration=""500"" android:drawable=""@drawable/door4"/"></item></animation-list>
  <item android:duration=""500"" android:drawable=""@drawable/door3"/"></item>
  <item android:duration=""500"" android:drawable=""@drawable/door2"/"></item>
  <item android:duration=""500"" android:drawable=""@drawable/door1"/"></item>
  步骤三
  在activity_main.xml的Image属性注释
  // app:srcCompat="@drawable/door1"
  MainActivity文件编程
  ImageView img;
  AnimationDrawable open_door;//两个.XML文件
  AnimationDrawable close_door;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  intiview();
  }
  private void intiview() {
  open_door = (AnimationDrawable) getResources().getDrawable(R.drawable.opendoor);
  close_door=(AnimationDrawable) getResources().getDrawable(R.drawable.closedoor);
  img = findViewById(R.id.imageView);
  }
  public void btn_close(View view) {
  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {
  img.setBackground(close_door);
  }
  close_door.start();
  }
  public void btn_Open(View view) {
  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {
  img.setBackground(open_door);
  }
  open_door.start();
  }
  }
网站目录投稿:夏之