Android实现手机震动抖动效果的方法

Android手机震动抖动效果的实现

(1)布局文件如下

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <EditText
    android:id="@+id/et_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp"
    android:ems="10" >
    <requestFocus />
  </EditText>
  <Button
    android:id="@+id/btn_submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/et_text"
    android:layout_below="@+id/et_text"
    android:layout_marginTop="38dp"
    android:text="提交" />
</RelativeLayout>

(2)MainActivity.java

package com.example.test11;
import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
  private EditText et_text;
  private Button btn_submit;
  /**
   * 手机振动器
   */
  private Vibrator vibrator;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_text = (EditText) this.findViewById(R.id.et_text);
    btn_submit = (Button) this.findViewById(R.id.btn_submit);
    // 震动效果的系统服务
    vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    btn_submit.setOnClickListener(new OnClickListener() {
      String text = et_text.getText().toString().trim();
      public void onClick(View v) {
        if (TextUtils.isEmpty(text)) {
          Toast.makeText(MainActivity.this, "内容为空", 0).show();
          Animation shake = AnimationUtils.loadAnimation(
              MainActivity.this, R.anim.shake);
          et_text.startAnimation(shake);
          /*
           * 震动的方式
           */
          // vibrator.vibrate(2000);//振动两秒
          // 下边是可以使震动有规律的震动  -1:表示不重复 0:循环的震动
          long[] pattern = { 200, 2000, 2000, 200, 200, 200 };
          vibrator.vibrate(pattern, -1);
        }
      }
    });
  }
}

(3)使用到的两个动画文件如下:

cycle_7.xml

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
  android:cycles="7" />

shake.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="1000"
  android:fromXDelta="0"
  android:interpolator="@anim/cycle_7"
  android:toXDelta="10" />

(4)需要使用的权限:

<uses-permission android:name="android.permission.VIBRATE" />

这个效果一般只有在真机上可以做到,不在上图展示。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

(0)

相关推荐

  • Android为textView设置setText的时候报错的讲解方案

    在对中TextView setText 覆值int 时报错,网上查下原因是setText整型表明是设值R.id.xxx,当然找不到. 解决方法是将int转化为string,用String.valueOf(xxx) 一.我的代码如下:就是我textView设置值 if (list != null) { for (Student stu : list) { //如果一下子赋值的话是不正确的 tv_name.setText(stu.getName()); tv_sex.setText(stu.getS

  • Android列表组件ListView使用详解之动态加载或修改列表数据

    在使用ListView组件来显示列表数据时,有的时候我们需要改变列表中的数据,有以下方法: 1.重新给ListView组件设置适配器 这种方法重新创建了ListView,效率不好. 2.使用适配器中的方法 /** * Notifies the attached observers that the underlying data has been changed * and any View reflecting the data set should refresh itself. */ pu

  • Android中Gallery和ImageSwitcher的使用实例

    效果如下: 布局文件activity_main.xml如下: <RelativeLayout 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

  • Android侧滑菜单之DrawerLayout用法详解

    onConfigurationChanged最早的时候实现侧滑菜单功能大多时候需要通过github上一个叫做SlidingMenu的开源通过依赖包来实现,后来谷歌在v4包中添加了DrawerLayout来实现这个功能,完全可以替代SlidingMenu,这里我们来学习DrawerLayout的用法 一)创建DrawerLayout 1)在布局文件里将布局设置为DrawerLaout,而且因为是v4包中的功能,所以必须写全包名,注意第一必须先写主视图布局,然后再写抽屉里的视图,这里我们放了List

  • Android实现电子罗盘(指南针)方向传感器的应用

    简介 现在每部Android手机里边都会内置有许多传感器,如光照传感器.加速度传感器.地磁传感器.压力传感器.温度传感器等,它们能够监测到各种发生在手机撒花姑娘的物理事件.当然Android系统只是负责将这些传感器所输出的信息传递给我们,然后我们可以利用这些信息去开发一些好玩的应用. 图片神马的在网上搜个指南针图片就好了,方便学习 main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo

  • Android手机屏幕px与dp互转的工具类

    dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素.dp也就是dip,这个和sp基本类似.如果设置表示长度.高度等属性时可以使用dp 或sp.但如果设置字体,需要使用sp.dp是与密度无关,sp除了与密度无关外,还与scale无关.如果屏幕密度为160,这时dp和sp和px是一 样的.1dp=1sp=1px,但如果使用px作单位,如果屏幕大小不

  • Android编程程序实现一键锁屏的方法讲解

    Android程序之一键锁屏 (1)布局文件activity_main.xml如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height

  • Android开源框架的SlidingFragment的使用示例

    效果如下: 直接上代码,留着以后用,代码目录结构如下: 其中BaseFragment.java是另外5个Fragment的基类,代码如下: package com.xuliugen.newsclient.fragment.base; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; impor

  • Android实现手机定位的案例代码

    Android手机定位案例代码 代码如下: package com.xuliugen.gpsdemo; import com.itheima.gpsdemo.R; import android.app.Activity; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.Locat

  • Android中GridView插件的使用方法

    布局文件activity_main.xml <RelativeLayout 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&q

随机推荐