Android编程实现仿iphone抖动效果的方法(附源码)
本文实例讲述了Android编程实现仿iphone抖动效果的方法。分享给大家供大家参考,具体如下:
布局文件:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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>
代码:
packagecom.android.shake;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.DisplayMetrics;
importandroid.view.animation.Animation;
importandroid.view.animation.Animation.AnimationListener;
importandroid.view.animation.RotateAnimation;
importandroid.widget.TextView;
importandroid.view.View;
importandroid.view.Window;
publicclassShakeTestActivityextendsActivityimplementsView.OnClickListener{
privateTextViewmtv0;
privateTextViewmtv1;
privateTextViewmtv2;
privateTextViewmtv3;
privateTextViewmtv4;
privateTextViewmtv5;
privateTextViewmtv6;
privateTextViewmtv7;
privateTextViewmtv8;
privateTextViewmtv9;
privateTextViewmtv10;
privateTextViewmtv11;
privateTextViewmtv12;
privateTextViewmtv13;
privateTextViewmtv14;
privateTextViewmtv15;
privateTextViewmtv16;
privateTextViewmtv17;
privateTextViewmtv18;
privateTextViewmtv19;
privatebooleanmNeedShake=false;
privatebooleanmStartShake=false;
privatestaticfinalintICON_WIDTH=80;
privatestaticfinalintICON_HEIGHT=94;
privatestaticfinalfloatDEGREE_0=1.8f;
privatestaticfinalfloatDEGREE_1=-2.0f;
privatestaticfinalfloatDEGREE_2=2.0f;
privatestaticfinalfloatDEGREE_3=-1.5f;
privatestaticfinalfloatDEGREE_4=1.5f;
privatestaticfinalintANIMATION_DURATION=80;
privateintmCount=0;
floatmDensity;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
DisplayMetricsdm=newDisplayMetrics();
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
publicvoidonClick(Viewv){
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);
}
}
privatevoidshakeAnimation(finalViewv){
floatrotate=0;
intc=mCount++%5;
if(c==0){
rotate=DEGREE_0;
}elseif(c==1){
rotate=DEGREE_1;
}elseif(c==2){
rotate=DEGREE_2;
}elseif(c==3){
rotate=DEGREE_3;
}else{
rotate=DEGREE_4;
}
finalRotateAnimationmra=newRotateAnimation(rotate,-rotate,ICON_WIDTH*mDensity/2,ICON_HEIGHT*mDensity/2);
finalRotateAnimationmrb=newRotateAnimation(-rotate,rotate,ICON_WIDTH*mDensity/2,ICON_HEIGHT*mDensity/2);
mra.setDuration(ANIMATION_DURATION);
mrb.setDuration(ANIMATION_DURATION);
mra.setAnimationListener(newAnimationListener(){
@Override
publicvoidonAnimationEnd(Animationanimation){
if(mNeedShake){
mra.reset();
v.startAnimation(mrb);
}
}
@Override
publicvoidonAnimationRepeat(Animationanimation){
}
@Override
publicvoidonAnimationStart(Animationanimation){
}
});
mrb.setAnimationListener(newAnimationListener(){
@Override
publicvoidonAnimationEnd(Animationanimation){
if(mNeedShake){
mrb.reset();
v.startAnimation(mra);
}
}
@Override
publicvoidonAnimationRepeat(Animationanimation){
}
@Override
publicvoidonAnimationStart(Animationanimation){
}
});
v.startAnimation(mra);
}
@Override
publicvoidonBackPressed(){
if(!mNeedShake){
super.onBackPressed();
}else{
mNeedShake=false;
mCount=0;
mStartShake=false;
}
}
}
完整实例代码代码点击此处本站下载。
希望本文所述对大家Android程序设计有所帮助。