源码地址:https://github.com/chenwenping863/ViewDemo.git
1、xml布局
在FrameLayout xml布局中,先定义的view是位于底层的。。。所以,将ViewStub空间当做TextView的背景。。。在实际控制动画的时候,只需要放大或缩小ViewStub即可。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.chenwenping.animationdemo.AnimationActivity"> <FrameLayout android:id="@+id/textview_container" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ViewStub android:id="@+id/viewstub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:layout="@layout/text_back"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文" android:textSize="30dp" android:textColor="#ffffff" android:gravity="center" android:layout_margin="20dp" android:background="@drawable/textview_bg_shape"/> </FrameLayout> </FrameLayout>
viewStub 的layout代码:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text_back_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/textview_bg_shape"/>
textView 背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#e12aa9" /> <size android:width="50dp" android:height="50dp" /> </shape>
2、java代码实现
对ViewStub进行操作的时候,需要先对其进行inflate()
package com.example.chenwenping.animationdemo; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ViewStub; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.FrameLayout; import android.widget.TextView; public class AnimationActivity extends AppCompatActivity { private Animation textAnimation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_animation); FrameLayout textContainer = (FrameLayout) findViewById(R.id.textview_container) ; ((ViewStub) textContainer.findViewById(R.id.viewstub)).inflate(); final TextView textView = (TextView) findViewById(R.id.text_back_back); textContainer.postDelayed(new Runnable() { @Override public void run() { startBlinkAnimation(textView); } }, 1000); } private void startBlinkAnimation(TextView textView) { if (textAnimation == null) { textAnimation = AnimationUtils.loadAnimation(this, R.anim.view_blink); } textView.startAnimation(textAnimation); } }
3、动画xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:shareInterpolator="true" android:duration="1500"> <alpha android:fromAlpha="1.0" android:toAlpha="0.01" android:repeatCount="infinite" android:repeatMode="restart" /> <scale android:fromXScale="1.0" android:toXScale="1.45" android:fromYScale="1.0" android:toYScale="1.45" android:repeatCount="infinite" android:repeatMode="restart" android:pivotX="50%" android:pivotY="50%"/> </set>