亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)簡單手機震動效果

 更新時間:2021年09月27日 10:30:21   作者:冰點藍欣  
這篇文章主要為大家詳細介紹了Android實現(xiàn)手機震動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

手機開發(fā)中,有時候我們需要使用震動效果提示用戶當前的軟件狀態(tài),下面以一個簡單的例子實現(xiàn)這個功能。

1.新建Android應用程序

2.在AndroidManifest.xml中申明需要使用的震動權(quán)限

<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數(shù)組的下標為1的元素開始重復.
    SystemClock.sleep(10000);//震動10s以后停止
    vibrator.cancel();
    break;
   default:
    break;
   }
  }
 };
}

5.運行效果

源碼下載:Android手機震動效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論