怎样使android的view动画循环弹动

在res中建立文件夹anim,分别写下cycles.xml,shake1.xml,shake2.xml

cycles.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <!--  android:cycles代表移动的速度 -->
3 <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
4     android:cycles="1" />

shake1.xml:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--水平移动 -->
 3 <!--android:duration代表运行时间 -->
 4 <!-- android:fromXDelta代表起始横坐标位置  (0,0)-->
 5 <!-- android:toXDelta代表离横坐标起始位置的X距离为100 (100,0)-->
 6 <translate xmlns:android="http://schemas.android.com/apk/res/android"
 7     android:duration="3000"
 8     android:fromXDelta="0.0"
 9     android:toXDelta="100.0"
10     android:interpolator="@anim/cycles"/>
复制代码

shake2.xml:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--垂直移动 -->
 3 <!--android:duration代表运行时间 -->
 4 <!-- android:fromXDelta代表起始纵坐标位置  (0,0)-->
 5 <!-- android:toXDelta代表离横坐标起始位置的Y距离为100(0,100)-->
 6 <translate xmlns:android="http://schemas.android.com/apk/res/android"
 7     android:duration="3000"
 8     android:fromYDelta="0.0"
 9     android:toYDelta="100.0"
10     android:interpolator="@anim/cycles"/>
复制代码

activity_main.xml:

<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"
    tools:context="com.zzw.testshake.MainActivity" >

    <ImageView
        android:id="@+id/image2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="78dp"
        android:src="@drawable/b" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/image2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="87dp"
        android:src="@drawable/a" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/image2"
        android:layout_toLeftOf="@+id/image2"
        android:text="Button" />


    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_toLeftOf="@+id/image1"
        android:text="Button" />

</RelativeLayout>

activity_main.xml
<pre name="code" class="html">activity_main.xml


 
package com.zzw.testshake;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ImageView image1 = (ImageView) findViewById(R.id.image1);
        image1.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake1));

        ImageView image2 = (ImageView) findViewById(R.id.image2);
        image2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake2));

        Button bt1 = (Button) findViewById(R.id.button1);
        bt1.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake1));

        Button bt2 = (Button) findViewById(R.id.button2);
        bt2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake2));

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值