Android实现振动效果
Android实现振动效果看下面代码
布局文件main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="振动测试" />
- </LinearLayout>
Activity文件
- package com.suxh.activity;
- import android.app.Activity;
- import android.app.Service;
- import android.content.pm.ActivityInfo;
- import android.os.Bundle;
- import android.os.Vibrator;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.Button;
- public class Activity1 extends Activity {
- private Button btn;
- private Vibrator mVibrator01; //声明一个振动器对象
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置为无标题栏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- // 设置为全屏模式
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
- // 设置为横屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- setContentView(R.layout.main);
- btn = (Button)findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- mVibrator01 = ( Vibrator ) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
- mVibrator01.vibrate( new long[]{100,10,100,1000},-1);
- }
- });
- }
- }
效果