实现手机QQ的抖动效果
2015-08-12 01:45
501 查看
其实实现这个效果很简单,只是为这个根布局加了一个动画;
即:
1:为根布局设置一个id;
2:activity 实例化布局中的id,并绑定动画;
3:设置震动效果,最好震动的时长和抖动的时长同步起来。
下面来看一看代码:
1:xml文件:
2:activity
即:
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 } }); }
相关文章推荐
- 【转载】PyTables Windows平台安装说明
- !HDU 5371 最长双回文串(多校7)-卡时间-(manacher+排序+set+lower_bound())
- Keil使用malloc异常进入hardfault调试故障手记
- 08 JAVA 线程 内存模型(二)
- 天猫首页迷思之-jquery实现左侧广告牌图片轮播
- 用WinHex找回丢失的分区
- HDU 3577 Fast Arrangement
- Android中自定义属性(attrs.xml,TypedArray的使用)
- 设计模式:5 依赖倒转原则
- 60.电话 信息 邮件 评分 打开其他应用
- 设计模式: 4开放封闭原则
- SQL语法之查询语句
- hdu 3693 Math teacher's homework(数位dp)
- ACM 2015 中国赛区时间
- 设计模式: 3 单一职责原则
- 设计模式:2策略模式
- POJ 3321 Apple Tree 【树形结构转变为线性结构+线段树OR树状数组】
- 设计模式:1.3 抽象工厂模式
- java mavn 1. 概要 安装
- 黑马程序员--Java基础--集合(一)