Android实现简单手机震动效果

手机开发中,有时候我们需要使用震动效果提示用户当前的软件状态,下面以一个简单的例子实现这个功能。

1.新建Android应用程序

2.在AndroidManifest.xml中申明需要使用的震动权限

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

3.界面上添加按钮

<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: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"
    android:orientation="vertical" >

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

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="震动1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="震动2"/>

    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="震动3"/>

</LinearLayout>

4.源码中处理按钮效果

package com.sl.vibratordemo;

import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity
{
 private Button btn1 = null;
 private Button btn2 = null;
 private Button btn3 = null;

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

  btn1 = (Button)findViewById(R.id.btn1);
  btn2 = (Button)findViewById(R.id.btn2);
  btn3 = (Button)findViewById(R.id.btn3);
  btn1.setOnClickListener(listener);
  btn2.setOnClickListener(listener);
  btn3.setOnClickListener(listener);
 }

 OnClickListener listener = new View.OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
   switch (v.getId())
   {
   case R.id.btn1:
    vibrator.vibrate(1000);//震动1秒
    break;
   case R.id.btn2:
    long[] pattern1 = {1000, 2000, 1000, 3000}; //等待1秒,震动2秒,等待1秒,震动3秒
    vibrator.vibrate(pattern1, -1);
    break;
   case R.id.btn3:
    long[] pattern2 = {1000, 500, 1000, 1000};
    vibrator.vibrate(pattern2, 2);//-1表示不重复, 如果不是-1, 比如改成1, 表示从前面这个long数组的下标为1的元素开始重复.
    SystemClock.sleep(10000);//震动10s以后停止
    vibrator.cancel();
    break;
   default:
    break;
   }
  }
 };
}

5.运行效果

源码下载:Android手机震动效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android中手机震动的设置(Vibrator)的步骤简要说明

    Android中手机震动的设置(Vibrator)的步骤: a.通过系统服务获得手机震动服务,Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE); b.得到震动服务后检测vibrator是否存在: vibrator.hasVibrator(); 检测当前硬件是否有vibrator,如果有返回true,如果没有返回false. c.根据实际需要进行适当的调用, vibrator.vibrate(long millisec

  • 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:la

  • 浅析Android手机卫士之抖动输入框和手机震动

    查看apiDemos,找到View/Animation/shake找到对应的动画代码,直接拷贝过来 当导入一个项目的时候,报R文件不存在,很多情况是xml文件出错了 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); et_phone.startAnimation(shake); 动画的xml文件shake.xml android:interpolator="@anim/cycle_7" interpo

  • Android编程实现手机震动功能的方法

    本文实例讲述了Android编程实现手机震动功能的方法.分享给大家供大家参考,具体如下: 在与用户交互时,常常会用到震动功能,以提醒用户.该功能实现比较简单,请参阅下面主要代码: import android.app.Activity; import android.app.Service; import android.os.Vibrator; public class TipHelper { public static void Vibrate(final Activity activity

  • 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_p

  • Android实现简单手机震动效果

    手机开发中,有时候我们需要使用震动效果提示用户当前的软件状态,下面以一个简单的例子实现这个功能. 1.新建Android应用程序 2.在AndroidManifest.xml中申明需要使用的震动权限 <uses-permission android:name="android.permission.VIBRATE" /> 3.界面上添加按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk

  • Android实现简单的分页效果

    本文实例为大家分享了Android分页效果的具体代码,供大家参考,具体内容如下 1.实现分页最主要的就是封装分页代码,然后在按钮里实现相关的操作 /** * 分页工具 * * @Project App_Page * @Package com.android.dividepage * @author chenlin * @version 1.0 * @Date 2012年6月2日 * @Note TODO * @param <T> */ public class PageHelper<T&

  • Android实现简单水波纹效果

    本文为大家分享了Android实现水波纹效果展示的具体代码,供大家参考,具体内容如下 一.效果 二.实现原理 自定义view,使用Path和贝塞尔曲线绘制,然后不断刷新,并且改变X.Y的值 主要知识点rQuadTo的使用 三.实现 WaveView.java public class WaveView extends View { private Paint mPaint; private final Path mPath; //波长 private int wavelength = 500;

  • Android 实现为点击事件添加震动效果

    Android 点击Button 实现震动效果教程 Overview 在Android 的点击效果中,遇到震动效果的还是很多的. 接下来就让我们看一下如何实现震动效果. 所需要的权限 如果我们在开发中需要使用到我们的震动,那么我们就需要申请一下权限: <uses-permission android:name="android.permission.VIBRATE"/> 这样我们的权限就申请好了. 我们震动效果的帮助类 创建一个名为VibrateHelp的点击震动的帮助类.

  • Android 使用Vibrator服务实现点击按钮带有震动效果

    Vibrator 振动器,是手机自带的振动器哦,不要想成岛国用的那种神秘东西哦~~ Vibrator是Android给我们提供的用于机身震动的一个服务哦 更多详情可见官方API文档:Vibrator 如何使用? 首先添加震动权限: <uses-permission android:name="android.permission.VIBRATE" /> 获得Vibrator实例: Vibrator mVibrator= (Vibrator) getSystemService

  • Android登陆界面实现清除输入框内容和震动效果

    本文为大家分享Android登陆界面实现清除输入框内容和震动效果的全部代码,具体内容如下: 效果图: 主要代码如下 自定义的一个EditText,用于实现有文字的时候显示可以清楚的按钮: import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.uti

随机推荐