一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

安卓入门

五、动画

1、逐帧动画(frame-by-frame animation)
  • frame.xml(必须放在drawable下)
<?xml version="1.0" encoding="utf-8"?>
<!--android:oneshot值为true表示只执行一次-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/anim1" android:duration="120"/>
    <item android:drawable="@drawable/anim2" android:duration="120"/>
    <item android:drawable="@drawable/anim3" android:duration="120"/>
    <item android:drawable="@drawable/anim4" android:duration="120"/>
    <item android:drawable="@drawable/anim5" android:duration="120"/>
    <item android:drawable="@drawable/anim6" android:duration="120"/>
    <item android:drawable="@drawable/anim7" android:duration="120"/>
    <item android:drawable="@drawable/anim8" android:duration="120"/>
    <item android:drawable="@drawable/anim9" android:duration="120"/>
    <item android:drawable="@drawable/anim10" android:duration="120"/>
</animation-list>
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/frame" />

</LinearLayout>
  • MainActivity
public class MainActivity extends AppCompatActivity {
    private Boolean flag = true;

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

        ImageView iv = findViewById(R.id.iv);
        AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    anim.start();
                    flag = false;
                } else {
                    anim.stop();
                    flag = true;
                }
            }
        });

    }
}
2、补间动画(tweened animation:不会改变控件的真实坐标)
a、xml方式
  • alpha.xml(必须放在anim下)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:duration="2000"
        android:fromAlpha="0"
        android:toAlpha="1" />

</set>
  • rotate.xml(必须放在anim下)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--android:pivotX="50%":参照物为自己;当参照物为父节点时,值后加p(50%p)-->
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />

</set>
  • scale.xml(必须放在anim下)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" />

</set>
  • translate.xml(必须放在anim下)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100"
        android:toYDelta="100" />

</set>
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:maxWidth="300dp"
        android:maxHeight="300dp"
        android:src="@drawable/img" />

</RelativeLayout>
  • MainActivity
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
                // Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
                // Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
                imageView.startAnimation(animation);
            }
        });
    }
}
b、代码方式
  • MainActivity
public class MainActivity extends AppCompatActivity {
    private TextView tv;

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

        tv = findViewById(R.id.tv);
    }

    public void alphaHandle(View view) {
        //透明程度0~1,0表示透明,1表示不透明
        AlphaAnimation aa = new AlphaAnimation(1, 0);
        aa.setDuration(2000);//设置动画执行时间
        aa.setRepeatCount(1);//设置动画重复次数
        aa.setRepeatMode(Animation.REVERSE);//设置动画重复模式
        tv.startAnimation(aa);
    }

    public void rotateHandle(View view) {
        /**
         * 参数1:起始角度;参数2:结束角度;
         * 参数3:X轴参照物;参数4:X轴参照物位置比值
         * 参数5:Y轴参照物;参数6:Y轴参照物位置比值
         */
        RotateAnimation ra = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        ra.setDuration(2000);//设置动画执行时间
        ra.setRepeatCount(1);//设置动画重复次数
        ra.setRepeatMode(Animation.REVERSE);//设置动画重复模式
        tv.startAnimation(ra);
    }

    public void scaleHandle(View view) {
        /**
         * 参数1:X轴起始倍数;参数2:X轴结束倍数;
         * 参数3:Y轴起始倍数;参数4:Y轴结束倍数;
         * 参数5:X轴参照物;参数6:X轴参照物位置比值
         * 参数7:Y轴参照物;参数8:Y轴参照物位置比值
         */
        ScaleAnimation sa = new ScaleAnimation(1, 2, 1, 2,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(2000);//设置动画执行时间
        sa.setRepeatCount(1);//设置动画重复次数
        sa.setRepeatMode(Animation.REVERSE);//设置动画重复模式
        tv.startAnimation(sa);
    }

    public void translateHandle(View view) {
        /**
         * 参数1:X轴起点参照物;参数2:X轴起点对比原点位移距离与X轴起点参照物长度的比值;
         * 参数3:X轴终点参照物;参数4:X轴终点对比原点位移距离与X轴终点参照物长度的比值;
         * 参数5:Y轴起点参照物;参数6:Y轴起点对比原点位移距离与Y轴起点参照物长度的比值;
         * 参数7:Y轴终点参照物;参数8:Y轴终点对比原点位移距离与Y轴终点参照物长度的比值;
         */
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0.5f);
        ta.setDuration(2000);
        ta.setFillAfter(true);//当动画结束后,停留在结束的位置上
        tv.startAnimation(ta);
    }

    public void composeHandle(View view) {
        AlphaAnimation aa = new AlphaAnimation(1, 0);
        aa.setDuration(2000);//设置动画执行时间
        aa.setRepeatCount(1);//设置动画重复次数
        aa.setRepeatMode(Animation.REVERSE);//设置动画重复模式

        RotateAnimation ra = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        ra.setDuration(2000);//设置动画执行时间
        ra.setRepeatCount(1);//设置动画重复次数
        ra.setRepeatMode(Animation.REVERSE);//设置动画重复模式

        ScaleAnimation sa = new ScaleAnimation(1, 2, 1, 2,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(2000);//设置动画执行时间
        sa.setRepeatCount(1);//设置动画重复次数
        sa.setRepeatMode(Animation.REVERSE);//设置动画重复模式

        AnimationSet as = new AnimationSet(true);
        as.addAnimation(aa);
        as.addAnimation(ra);
        as.addAnimation(sa);
        tv.startAnimation(as);
    }
}
3、属性动画(property animation:会改变控件的真实坐标)
a、单个动画
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="你好世界" />

</RelativeLayout>
  • MainActivity
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ValueAnimator
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("ll", value + "");
            }
        });
        valueAnimator.start();

        // ObjectAnimator
        TextView textView = findViewById(R.id.tv);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f);
        objectAnimator.setDuration(2000);
        objectAnimator.start();
        // 动画监听器
        /*
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }
            @Override
            public void onAnimationEnd(Animator animation) {
            }
            @Override
            public void onAnimationCancel(Animator animation) {
            }
            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        */
        //AnimatorListenerAdapter继承自Animator.AnimatorListener,并将所有方法默认实现
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
    }
}
b、组合动画
  • MainActivity
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.tv);

        //定义动画集
        AnimatorSet as = new AnimatorSet();
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0f, 1f);
        ObjectAnimator oa2 = ObjectAnimator.ofFloat(tv, "scaleX", 1f, 0.5f, 1f);
        ObjectAnimator oa3 = ObjectAnimator.ofFloat(tv, "rotation", 0f, 360f, 0f);
        ObjectAnimator oa4 = ObjectAnimator.ofFloat(tv, "translationX", 0f, 300f, 0f);
        as.setDuration(2000);
        as.setTarget(tv);
        //动画依次执行
        //as.playSequentially(oa1, oa2, oa3, oa4);
        //动画一起执行
        as.playTogether(oa1, oa2, oa3, oa4);
        as.start();
    }
}
c、xml方式动画
  • my_animator.xml(必须放在animator下)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="2000"
        android:propertyName="translationX"
        android:valueFrom="0"
        android:valueTo="100" />
</set>

六、单位和尺寸

1、px与pt的区别
1、px:pixels(像素),不同设备显示效果相同
2、pt:point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用
2、dp与sp的作用
1、dip:device independent pixels(设备独立像素),不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA推荐使用这个,不依赖像素
2、dp:就是dip
3、sp:scaled pixels(放大像素),主要用于字体显示best for textsize
3、LayoutParams是什么
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);

        // LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息
        LinearLayout linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutParams);

        TextView textView = new TextView(this);
        textView.setText("周洁琼");
        textView.setBackgroundColor(0xffff0000);
        // px
        LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(300, 300);
        // textView.setLayoutParams(textLayoutParams);
        // linearLayout.addView(textView);
        linearLayout.addView(textView, textLayoutParams);

        setContentView(linearLayout);
    }
}
posted on 2021-11-12 14:34  一路繁花似锦绣前程  阅读(39)  评论(0编辑  收藏  举报