快好知 kuaihz

Image+.XML的方式,实现动画效果

步骤

创立.XML文件(如下图1所示),并将添加图片

图 1

步骤

将<selector修改成<animation-list


步骤

在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 >= Build.VERSION_CODES.JELLY_BEAN) {

img.setBackground(close_door);

}

close_door.start();

}

public void btn_Open(View view) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

img.setBackground(open_door);

}

open_door.start();

}

}

本站资源来自互联网,仅供学习,如有侵权,请通知删除,敬请谅解!
搜索建议:效果  效果词条  实现  实现词条  方式  方式词条  动画  动画词条  Image  Image词条