歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android代碼實現長按顯示波紋外擴動畫

項目兩張圖片:

wave btn

代碼:

package com.example.waveanimation;

import Android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {
 private static final int ANIMATIONEACHOFFSET = 600; // 每個動畫的播放時間間隔
 private AnimationSet aniSet, aniSet2, aniSet3;
 private ImageView btn, wave1, wave2, wave3;
 private Handler handler = new Handler() {

  @Override
  public void handleMessage(Message msg) {
   if (msg.what == 0x222) {
    wave2.startAnimation(aniSet2);
   } else if (msg.what == 0x333) {
    wave3.startAnimation(aniSet3);
   }
   super.handleMessage(msg);
  }

 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  aniSet = getNewAnimationSet();
  aniSet2 = getNewAnimationSet();
  aniSet3 = getNewAnimationSet();
  setContentView(R.layout.activity_main);
  btn = (ImageView) findViewById(R.id.btn);
  wave1 = (ImageView) findViewById(R.id.wave1);
  wave2 = (ImageView) findViewById(R.id.wave2);
  wave3 = (ImageView) findViewById(R.id.wave3);
  btn.setOnTouchListener(new OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
     showWaveAnimation();
     break;
    case MotionEvent.ACTION_UP:
     cancalWaveAnimation();
     break;
    case MotionEvent.ACTION_CANCEL:
     cancalWaveAnimation();
     break;
    }
    return true;
   }
  });
 }

 private AnimationSet getNewAnimationSet() {
  AnimationSet as = new AnimationSet(true);
  ScaleAnimation sa = new ScaleAnimation(1f, 2.3f, 1f, 2.3f,
    ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
    ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
  sa.setDuration(ANIMATIONEACHOFFSET * 3);
  sa.setRepeatCount(-1);// 設置循環
  AlphaAnimation aniAlp = new AlphaAnimation(1, 0.1f);
  aniAlp.setRepeatCount(-1);// 設置循環
  as.setDuration(ANIMATIONEACHOFFSET * 3);
  as.addAnimation(sa);
  as.addAnimation(aniAlp);
  return as;
 }

 private void showWaveAnimation() {
  wave1.startAnimation(aniSet);
  handler.sendEmptyMessageDelayed(0x222, ANIMATIONEACHOFFSET);
  handler.sendEmptyMessageDelayed(0x333, ANIMATIONEACHOFFSET * 2);

 }

 private void cancalWaveAnimation() {
  wave1.clearAnimation();
  wave2.clearAnimation();
  wave3.clearAnimation();
 }

}

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/wave1"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_centerInParent="true"
                android:background="@drawable/wave"
                />

            <ImageView
                android:id="@+id/wave2"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_centerInParent="true"
                android:background="@drawable/wave"/>

            <ImageView
                android:id="@+id/wave3"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_centerInParent="true"
                android:background="@drawable/wave" />

            <ImageView
                android:id="@+id/btn"
                android:layout_width="166dp"
                android:layout_height="166dp"
                android:layout_centerInParent="true"
                android:background="@drawable/btn" />

</RelativeLayout>

效果圖:

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved