补间动画与帧动画

帧动画JAVA文件代码:

public class Fragment01 extends AppCompatActivity {

    private AnimationDrawable drawable;
    private ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.fragment01 );

        mIv = (ImageView)findViewById ( R.id.iv );
        //向动画管理器添加对象
        drawable = new AnimationDrawable ();
        //向管理器中添加动画
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim1 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim2 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim3 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim4 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim5 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim6 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim7 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim8 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim9 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim10 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim11 ),100 );
        drawable.addFrame ( getResources ().getDrawable ( R.mipmap.anim12 ),100 );
        //设置执行次数
        drawable.setOneShot(false);//是否执行一次
        //将动画资源设置到图片控件中
        mIv.setImageDrawable(drawable);

    }
    //启动动画
    public void start(View v){
        if(!drawable.isRunning ()){
            drawable.start ();
        }
    }
    //停止动画
    public void stop(View v){
        drawable.stop ();
    }
}


补间动画JAVA文件代码:

public class Fragment02 extends AppCompatActivity {

    private ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.fragment02);

        mIv = (ImageView) findViewById(R.id.iv_id);

        mIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Fragment02.this, "我在这里!!!!!!", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.but_01:
                //透明度
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
                alphaAnimation.setDuration(5000);//设置持续时间
                alphaAnimation.setFillAfter(true);//设置是否保持结束时的状态
                mIv.startAnimation(alphaAnimation);
                break;
            case R.id.but_02:
                //缩放
                /**
                 * X轴的起始
                 * X轴的结束
                 * Y轴的起始
                 * Y轴的结束
                 * X轴的参照位置
                 * X轴的值
                 * Y轴的参照位置
                 * Y轴的值
                 */
                ScaleAnimation scaleAnimation = new ScaleAnimation(1,3,1,3,
                        Animation.RELATIVE_TO_SELF,0.5F,
                        Animation.RELATIVE_TO_SELF,0.5F);
                scaleAnimation.setDuration(5000);
                mIv.startAnimation(scaleAnimation);
                break;
            case R.id.but_03:
                //旋转
                RotateAnimation rotateAnimation = new RotateAnimation(0f,720f,
                        Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,0.5F);
                rotateAnimation.setDuration(5000);
                mIv.startAnimation(rotateAnimation);
                break;
            case R.id.but_04:
                //位移
                TranslateAnimation translateAnimation = new TranslateAnimation(0,700,0,700);
                translateAnimation.setDuration(5000);
                translateAnimation.setFillAfter(true);
                mIv.startAnimation(translateAnimation);
                break;
            case R.id.but_05:
                //集合
                TranslateAnimation tAnimation = new TranslateAnimation(0,700,0,700);
                AlphaAnimation aAnimation = new AlphaAnimation(1.0f,0.0f);

                //1, 实例化动画的集合
                /**
                 * if true:     使用set集合的插值器
                 * if false:    使用动画自己的插值器
                 */
                AnimationSet animationSet = new AnimationSet(false);

                //2, 把动画放入集合
                animationSet.addAnimation(aAnimation);
                animationSet.addAnimation(tAnimation);

                animationSet.setDuration(5000);
                animationSet.setFillAfter(true);

                //启动动画
                mIv.startAnimation(animationSet);

                break;
        }
    }
}

布局文件xml:

fragment01.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.day03_zkli.Fragment01">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@mipmap/ic_launcher"
        android:layout_centerInParent="true"/>
    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动动画"
        android:textSize="20sp"
        android:onClick="start"
        android:layout_below="@+id/iv"/>
    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止动画"
        android:textSize="20sp"
        android:onClick="stop"
        android:layout_below="@+id/start"/>

</RelativeLayout>

fragment02.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.day03_zkli.Fragment02">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/but_01"
            android:text="透明度"
            android:onClick="onClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩放"
            android:id="@+id/but_02"
            android:onClick="onClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转"
            android:id="@+id/but_03"
            android:onClick="onClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="位移"
            android:id="@+id/but_04"
            android:onClick="onClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="集合"
            android:id="@+id/but_05"
            android:onClick="onClick"/>

    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/img_27"
        android:id="@+id/iv_id"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"/>

</LinearLayout>

阅读更多
换一批

没有更多推荐了,返回首页