欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现仿iphone抖动效果的方法(附源码)

程序员文章站 2023-12-09 13:41:09
本文实例讲述了android编程实现仿iphone抖动效果的方法。分享给大家供大家参考,具体如下: 布局文件:

本文实例讲述了android编程实现仿iphone抖动效果的方法。分享给大家供大家参考,具体如下:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 <linearlayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <textview
   android:id="@+id/tv0"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv1"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv2"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv3"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
 </linearlayout>
 <linearlayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <textview
   android:id="@+id/tv4"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv5"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv6"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv7"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
 </linearlayout>
 <linearlayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <textview
   android:id="@+id/tv8"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv9"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv10"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv11"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
 </linearlayout>
 <linearlayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <textview
   android:id="@+id/tv12"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv13"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv14"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv15"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
 </linearlayout>
 <linearlayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <textview
   android:id="@+id/tv16"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv17"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv18"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
  <textview
   android:id="@+id/tv19"
   android:layout_width="80dip"
   android:layout_height="94dip"
   android:drawabletop="@drawable/ic_launcher"
   android:text="@string/hello" />
 </linearlayout>
</linearlayout>

代码:

package com.android.shake;
import android.app.activity;
import android.os.bundle;
import android.util.displaymetrics;
import android.view.animation.animation;
import android.view.animation.animation.animationlistener;
import android.view.animation.rotateanimation;
import android.widget.textview;
import android.view.view;
import android.view.window;
public class shaketestactivity extends activity implements view.onclicklistener {
 private textview mtv0;
 private textview mtv1;
 private textview mtv2;
 private textview mtv3;
 private textview mtv4;
 private textview mtv5;
 private textview mtv6;
 private textview mtv7;
 private textview mtv8;
 private textview mtv9;
 private textview mtv10;
 private textview mtv11;
 private textview mtv12;
 private textview mtv13;
 private textview mtv14;
 private textview mtv15;
 private textview mtv16;
 private textview mtv17;
 private textview mtv18;
 private textview mtv19;
 private boolean mneedshake = false;
 private boolean mstartshake = false;
 private static final int icon_width = 80;
 private static final int icon_height = 94;
 private static final float degree_0 = 1.8f;
 private static final float degree_1 = -2.0f;
 private static final float degree_2 = 2.0f;
 private static final float degree_3 = -1.5f;
 private static final float degree_4 = 1.5f;
 private static final int animation_duration = 80;
 private int mcount = 0;
 float mdensity;
 /** called when the activity is first created. */
 @override
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  requestwindowfeature(window.feature_no_title);
  setcontentview(r.layout.main);
  displaymetrics dm = new displaymetrics();
  getwindowmanager().getdefaultdisplay().getmetrics(dm);
  if (dm != null) {
   mdensity = dm.density;
  }
  mtv0 = (textview) findviewbyid(r.id.tv0);
  mtv0.setonclicklistener(this);
  mtv1 = (textview) findviewbyid(r.id.tv1);
  mtv1.setonclicklistener(this);
  mtv2 = (textview) findviewbyid(r.id.tv2);
  mtv2.setonclicklistener(this);
  mtv3 = (textview) findviewbyid(r.id.tv3);
  mtv3.setonclicklistener(this);
  mtv4 = (textview) findviewbyid(r.id.tv4);
  mtv4.setonclicklistener(this);
  mtv5 = (textview) findviewbyid(r.id.tv5);
  mtv5.setonclicklistener(this);
  mtv6 = (textview) findviewbyid(r.id.tv6);
  mtv6.setonclicklistener(this);
  mtv7 = (textview) findviewbyid(r.id.tv7);
  mtv7.setonclicklistener(this);
  mtv8 = (textview) findviewbyid(r.id.tv8);
  mtv8.setonclicklistener(this);
  mtv9 = (textview) findviewbyid(r.id.tv9);
  mtv9.setonclicklistener(this);
  mtv10 = (textview) findviewbyid(r.id.tv10);
  mtv10.setonclicklistener(this);
  mtv11 = (textview) findviewbyid(r.id.tv11);
  mtv11.setonclicklistener(this);
  mtv12 = (textview) findviewbyid(r.id.tv12);
  mtv12.setonclicklistener(this);
  mtv13 = (textview) findviewbyid(r.id.tv13);
  mtv13.setonclicklistener(this);
  mtv14 = (textview) findviewbyid(r.id.tv14);
  mtv14.setonclicklistener(this);
  mtv15 = (textview) findviewbyid(r.id.tv15);
  mtv15.setonclicklistener(this);
  mtv16 = (textview) findviewbyid(r.id.tv16);
  mtv16.setonclicklistener(this);
  mtv17 = (textview) findviewbyid(r.id.tv17);
  mtv17.setonclicklistener(this);
  mtv18 = (textview) findviewbyid(r.id.tv18);
  mtv18.setonclicklistener(this);
  mtv19 = (textview) findviewbyid(r.id.tv19);
  mtv19.setonclicklistener(this);
 }
 @override
 public void onclick(view v) {
  if (!mstartshake) {
   mstartshake = true;
   mneedshake = true;
   shakeanimation(mtv0);
   shakeanimation(mtv1);
   shakeanimation(mtv2);
   shakeanimation(mtv3);
   shakeanimation(mtv4);
   shakeanimation(mtv5);
   shakeanimation(mtv6);
   shakeanimation(mtv7);
   shakeanimation(mtv8);
   shakeanimation(mtv9);
   shakeanimation(mtv10);
   shakeanimation(mtv11);
   shakeanimation(mtv12);
   shakeanimation(mtv13);
   shakeanimation(mtv14);
   shakeanimation(mtv15);
   shakeanimation(mtv16);
   shakeanimation(mtv17);
   shakeanimation(mtv18);
   shakeanimation(mtv19);
  }
 }
 private void shakeanimation(final view v) {
  float rotate = 0;
  int c = mcount++ % 5;
  if (c == 0) {
   rotate = degree_0;
  } else if (c == 1) {
   rotate = degree_1;
  } else if (c == 2) {
   rotate = degree_2;
  } else if (c == 3) {
   rotate = degree_3;
  } else {
   rotate = degree_4;
  }
  final rotateanimation mra = new rotateanimation(rotate, -rotate, icon_width * mdensity / 2, icon_height * mdensity / 2);
  final rotateanimation mrb = new rotateanimation(-rotate, rotate, icon_width * mdensity / 2, icon_height * mdensity / 2);
  mra.setduration(animation_duration);
  mrb.setduration(animation_duration);
  mra.setanimationlistener(new animationlistener() {
   @override
   public void onanimationend(animation animation) {
    if (mneedshake) {
     mra.reset();
     v.startanimation(mrb);
    }
   }
   @override
   public void onanimationrepeat(animation animation) {
   }
   @override
   public void onanimationstart(animation animation) {
   }
  });
  mrb.setanimationlistener(new animationlistener() {
   @override
   public void onanimationend(animation animation) {
    if (mneedshake) {
     mrb.reset();
     v.startanimation(mra);
    }
   }
   @override
   public void onanimationrepeat(animation animation) {
   }
   @override
   public void onanimationstart(animation animation) {
   }
  });
  v.startanimation(mra);
 }
 @override
 public void onbackpressed() {
  if (!mneedshake) {
   super.onbackpressed();
  } else {
   mneedshake = false;
   mcount = 0;
   mstartshake = false;
  }
 }
}

完整实例代码代码点击此处本站下载

希望本文所述对大家android程序设计有所帮助。