您的位置:首页 > 其它

实现手机QQ的抖动效果

2015-08-12 01:45 501 查看
其实实现这个效果很简单,只是为这个根布局加了一个动画;

即:

1:为根布局设置一个id;

2:activity 实例化布局中的id,并绑定动画;

3:设置震动效果,最好震动的时长和抖动的时长同步起来。

下面来看一看代码:

1:xml文件:

<LinearLayout 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:orientation="vertical"
android:id="@+id/lin"
tools:context="com.example.zz.DouYiDouActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始抖抖"
/>

</LinearLayout>


2:activity

private LinearLayout layout;
private Button button;
private Vibrator vibrator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dou_yi_dou);

layout = (LinearLayout) findViewById(R.id.lin);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Animation utils = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.dou);
utils.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {

vibrator.cancel();//动画结束时,停止震动
}
});
layout.startAnimation(utils);

vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long [] pattern = {100,800,100};   // 震动开始等待的时间 震动的时长 停止 时间
vibrator.vibrate(pattern,2);           //重复两次上面的pattern 如果只想震动一次,index设为-1

}
});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: