Android学习——Frame动画、Shape动画

来源:互联网 发布:js中控制div隐藏 编辑:程序博客网 时间:2024/05/27 14:15

1. Frame帧动画
animation-list
属性oneshot : 帧动画的自动执行。如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。
示例:
新建/drawable/anim_img.xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item        android:drawable="@drawable/img01"        android:duration="1000"/>    <item        android:drawable="@drawable/img02"        android:duration="1000"/>    <item        android:drawable="@drawable/img03"        android:duration="1000"/>    <item        android:drawable="@drawable/img04"        android:duration="2000"/>    <item        android:drawable="@drawable/img05"        android:duration="2000"/>    <item        android:drawable="@drawable/img06"        android:duration="1000"/>    <item        android:drawable="@drawable/img07"        android:duration="1000"/>    <item        android:drawable="@drawable/img08"        android:duration="1000"/></animation-list>

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <ImageView        android:id="@+id/imgv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true" />    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:text="frame动画测试" /></RelativeLayout>

java文件:

public class MainActivity extends Activity {    private ImageView imgv;    private Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        imgv = (ImageView) this.findViewById(R.id.imgv);        btn = (Button) this.findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                imgv.setBackgroundResource(R.drawable.anim_img);                AnimationDrawable animationDrawable = (AnimationDrawable) imgv.getBackground();                animationDrawable.start();            }        });    }}

运行效果:
这里写图片描述
2. shape动画
背景选择器:
gradient渐变
stroke描边
conners圆角
新建背景选择器res/layout/btn_selector.xml:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true">        <shape>            <!-- 渐变 -->            <gradient                 android:endColor="#FFFFFF"                 android:startColor="#ff0000"                 android:type="linear"                android:gradientRadius="20"/>              <!-- 描边  -->            <stroke                android:width="8dp"                android:color="#00ff00"                android:dashWidth="10dp"                android:dashGap="5dp"/>               <!-- 圆角 -->             <corners android:radius="10dp"/>             <padding                android:left="10dp"                android:top="5dp"                android:right="10dp"                 android:bottom="5dp"/>                  </shape>    </item>    <item >        <shape>            <!-- 渐变 -->            <gradient                 android:endColor="#ff0000"                 android:startColor="#ff0000"/>              <!-- 描边  -->            <stroke                android:color="#000000"/>             <padding                android:left="10dp"                android:top="5dp"                android:right="10dp"                 android:bottom="5dp"/>                  </shape>    </item></selector>

布局文件中添加一个Button按钮:

 <Button         android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"                android:background="@layout/btn_selector"        android:text="shape测试" />

运行效果:
这里写图片描述
点击按钮之后:

这里写图片描述

0 0
原创粉丝点击