Day4关键帧动画

1.通过遍历id来实现关键帧动画

1.MainActivity
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.创建一个动画资源
        AnimationDrawable anim=new AnimationDrawable();
                //2.添加每一帧的动画
        int[]resID={R.drawable.campfire01,R.drawable.campfire02,R.drawable.campfire03,R.drawable.campfire04,
                R.drawable.campfire05,R.drawable.campfire06,R.drawable.campfire07,R.drawable.campfire08,
                R.drawable.campfire09,R.drawable.campfire10,R.drawable.campfire11,R.drawable.campfire12,
                R.drawable.campfire13,R.drawable.campfire14,R.drawable.campfire15,R.drawable.campfire16,
                R.drawable.campfire17};
        for(int id:resID){
            anim.addFrame(getResources().getDrawable(id,null),100);
        }
        //3.使用这个动画资源
        ImageView iv=findViewById(R.id.iv_anim);
        iv.setImageDrawable(anim);
        //4.启动
        anim.start();
    }
2.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=".MainActivity">

    <ImageView
        android:id="@+id/iv_anim"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/campfire01"
         android:scaleType="fitXY"
        />

</RelativeLayout>

2.自己设计初步的动画效果(移动旋转放大缩小等)

1.知识

关键帧动画 配置了动画的每一帧
补间动画 只关心开始和结束的两个状态 中间的动画由系统自动补全
平移:translation
缩放:scale
旋转:rotate
透明 alpha
1.xml配置动画
res->anim->xxx.xml
2.代码
注意:动画只是一个效果 没有真正改变属性
属性动画才是真正改变了动画的属性值

2.外观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=".MainActivity">

<View
    android:id="@+id/v-anim"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@color/colorAccent"
/>
</RelativeLayout>
3.xml配置动画
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
android:repeatMode="reverse"
    >

<!--
    <translate android:fromXDelta="0"
        android:toXDelta="300"/>
        -->
    <alpha android:fromAlpha="0.1"
        android:toAlpha="1.0"/>

    <scale android:fromXScale="0.1"
        android:toXScale="1.5"
        android:fromYScale="0.1"
        android:toYScale="1.5"
        android:pivotX="50%"
        android:pivotY="50%"
        />
    <rotate android:fromDegrees="0"
android:toDegrees="360"
        android:pivotY="50%"
        android:pivotX="50%"/>
</set>
3.MainActivity
public class MainActivity extends AppCompatActivity {

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

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //1.找到动画的控件
            View v = findViewById(R.id.v_anim);
            /**
             *     //2.加载xml动画文件->得到动画
             *             Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
             *             translate.setInterpolator(new BounceInterpolator());
             *             translate.setRepeatCount(Integer.MAX_VALUE);
             *             //3.将这个动画作用到这个控件上
             *             v.startAnimation(translate);
             */
        //2.创建动画
            /*
             */
            //2.1颜色变化
         // AlphaAnimation alpha =new AlphaAnimation(0,1);
         // alpha.setDuration(1000);
         // alpha.setFillAfter(true);
            //2.2旋转
            RotateAnimation rotateAnimation=new RotateAnimation(0,360,(float)( 0.5*v.getWidth()),(float) (0.5*v.getHeight()));
            rotateAnimation.setDuration(1000);
            rotateAnimation.setFillAfter(true);
            //3.开启动画
            v.startAnimation(rotateAnimation);
        }
        return true;
    }
}

3.文字输入框

1.xml
<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=".MainActivity">
<!-- 输入框  接受用户的输入
1.TextView 显示文本
2.plainText
3.EditText
margin:和父容器的间距
padding:控件的内间距
-->
   <EditText

       android:layout_width="wrap_content"
       android:layout_height="60dp"
       android:background="@drawable/bg"
       android:layout_marginLeft="50dp"
       android:layout_marginRight="50dp"
       android:paddingLeft="40dp"
       android:textColor="#ffffff"
       android:textSize="30sp"
       android:hint="请输入密码"
       android:textColorHint="#999999"

       android:maxLines="1"
       android:inputType="textPassword"
android:cursorVisible="true"
       android:maxLength="6"

    android:imeOptions="actionGo"
       android:id="@+id/et_password"
       />

</RelativeLayout>
2.相关操作和监听者

public class MainActivity extends AppCompatActivity implements  View.OnTouchListener {

    //1.xml配置文件
    //2.使用代码创建
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //通过id获取xml里对应的控件
        final EditText et = findViewById(R.id.et_password);

        //监听键盘的key按下的事件
        //1.创建匿名类对象

        //1.1 创建一个类管理事件
        /*
        PXDListener pl = new PXDListener();
        et.setOnEditorActionListener(pl);
        */
        //et.setOnEditorActionListener(new PXDListener());

        //1.2 匿名类对象

        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return false;
            }
        });


        //1.3 Lambda表达式
    //  et.setOnEditorActionListener((textView,actionId,event) -> {
    //      System.out.println("点击了");
    //      return true;
    //  });

        //2.当前这个Activity来监听事件

        //et.setOnEditorActionListener(this);
        // 监听EditText文本内容改变事件
        et.addTextChangedListener(new TextWatcher() {

            @Override
            /**
             * CharSequence s:显示的文本内容
             * int start:0;操作的起始点 末尾为0中间不为0
             * int before 改变之前最后一个字符的位置
             * int count 改变之后字符个数
             *
             * before<count :添加
             * before>count ;删除字符
             *
             * 限制输入个数是6个
             */
            public void beforeTextChanged(CharSequence s, int start, int before, int count) {
             //   System.out.println("改变前"+et.getText().toString());
                System.out.println(s.toString());
                System.out.println("start"+start);
                System.out.println("before"+before);
                System.out.println("count"+count);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
              //  System.out.println("改变中"+et.getText().toString());
                //获取 目前输入的个数
                int len=s.toString().length();
                if (len>6){
                    //将最后一个删除掉
                    //只要前面6个
                    et.setText(s.subSequence(0,5));
                    //让光标定位到最后
                    et.setSelection(6);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
               // System.out.println("改变后"+et.getText().toString());
                System.out.println(s.toString());
            }
        });
    }

    //键盘被按下的回调事件

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        System.out.println("点击了");
        return false;
    }
}

//创建一个类 管理事件的回调
class PXDListener implements TextView.OnEditorActionListener{

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        System.out.println("点击了");
        return false;
    }
}
3.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容