android中利用animation实现Image的循环移动

来源:互联网 发布:使命召唤高级战争优化 编辑:程序博客网 时间:2024/06/03 12:42

本文利用TranslateAnimation实现图片的移动动画效果,利用setAnimationListener实现图片的循环移动!

思想很简单,就是在第一个动画结束的时候开始下一个动画。做这个的过程让我感觉很像是在做ppt。

现在感觉很多东西原理真的都是相通的。


首先是设置四个动画效果,利用TranlateAnimation,然后在每个animation的setAnimationListener中启动下一个动画效果。


效果图片如下:



布局文件如下:

<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" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    <RelativeLayout        android:id="@+id/newIcon"        android:layout_centerInParent="true"        android:layout_width="match_parent"        android:layout_height="80dp">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginLeft="40dp"            android:layout_marginRight="20dp"            android:orientation="horizontal">            <RelativeLayout                android:id="@+id/dynamicView"                android:layout_marginTop="10dp"                android:layout_width="60dp"                android:layout_height="60dp">                <ImageView                    android:id="@+id/pic_0"                    android:src="@drawable/pic1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:alpha="1.0"/>                <ImageView                    android:id="@+id/pic_1"                    android:src="@drawable/pic2"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:alpha="0.0"/>                <ImageView                    android:id="@+id/pic_2"                    android:src="@drawable/pic2"                    android:layout_toRightOf="@id/pic_1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:alpha="0.0" />                <ImageView                    android:id="@+id/pic_3"                    android:src="@drawable/pic2"                    android:layout_below="@id/pic_2"                    android:layout_toRightOf="@id/pic_1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:alpha="0.0"/>                <ImageView                    android:id="@+id/pic_4"                    android:src="@drawable/pic2"                    android:layout_toLeftOf="@id/pic_3"                    android:layout_below="@id/pic_1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:alpha="0.0"/>            </RelativeLayout>            <TextView                android:id="@+id/shuaxin"                android:paddingLeft="10dp"                android:text="正在刷新数据..."                android:layout_marginTop="22dp"                android:textSize="27sp"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />        </LinearLayout>    </RelativeLayout></RelativeLayout>

Java代码如下:

public class MainActivity extends Activity {    private ImageView pictureDong,pictureJing_1,pictureJing_2,pictureJing_3,pictureJing_4;    private static Boolean occurFlag = true;    private static float offsetValue ;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        pictureDong = (ImageView) findViewById(R.id.pic_0);        pictureJing_1 = (ImageView) findViewById(R.id.pic_1);        pictureJing_2 = (ImageView) findViewById(R.id.pic_2);        pictureJing_3 = (ImageView) findViewById(R.id.pic_3);        pictureJing_4 = (ImageView) findViewById(R.id.pic_4);        offsetValue = getResources().getDimension(R.dimen.offsetValue);        pictureJing_1.setAlpha(1.0f);        final TranslateAnimation animation0 = new TranslateAnimation(0,offsetValue,0,0);        animation0.setDuration(700);        animation0.setFillAfter(true);        final TranslateAnimation animation1 = new TranslateAnimation(offsetValue, offsetValue, 0, offsetValue);        animation1.setDuration(700);        animation1.setFillAfter(true);        final TranslateAnimation animation2 = new TranslateAnimation(offsetValue,0,offsetValue,offsetValue);        animation2.setDuration(700);        animation2.setFillAfter(true);        final TranslateAnimation animation3 = new TranslateAnimation(0,0,offsetValue,0);        animation3.setDuration(700);        animation3.setFillAfter(true);        animation0.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                if (occurFlag == true){                    pictureJing_2.setAlpha(1.0f);                }else {                    pictureJing_2.setAlpha(0.0f);                }                pictureDong.startAnimation(animation1);            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });        animation1.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                if (occurFlag == true){                    pictureJing_3.setAlpha(1.0f);                }else {                    pictureJing_3.setAlpha(0.0f);                }                pictureDong.startAnimation(animation2);            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });        animation2.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                if (occurFlag == true){                    pictureJing_4.setAlpha(1.0f);                }else {                    pictureJing_4.setAlpha(0.0f);                }                pictureDong.startAnimation(animation3);            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });        animation3.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                if (occurFlag == true){                    occurFlag = false;                }else {                    occurFlag = true;                }                if (occurFlag == false){                    pictureJing_1.setAlpha(0.0f);                }else {                    pictureJing_1.setAlpha(1.0f);                }                pictureDong.startAnimation(animation0);            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });        pictureDong.startAnimation(animation0);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

这里在dimes.xml文件中还要定义一个offsetValue 为30dp   !

大家可以试试最终的实现效果!有什么好的建议和改进也欢迎评论!


1 0
原创粉丝点击