Android4.0状态栏添加虚拟按键

1. 平板上状态栏上加虚拟按键
1). 书接上文,我们看到状态栏用的view主要来自status_bar,搜索status_bar.xml可以找到2个
res/layout/status_bar.xml
res/layout-sw600sp/status_bar.xml //平板width > 600,所以用到的是这个配置文件

2).学习back键的处理
<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"
			android:layout_width="80dip"
			android:layout_height="match_parent"
			android:src="@drawable/ic_sysbar_back"
			systemui:keyCode="4"
			android:contentDescription="@string/accessibility_back"
			systemui:glowBackground="@drawable/ic_sysbar_highlight"
		/>
原来也就是增加一个systemui:keyCode="4" ,也就是点击这个图标时,报键值4
<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/volume_up"
			android:layout_width="80dip"
			android:layout_height="match_parent"
			android:src="@drawable/ic_sysbar_volume_up"
			systemui:keyCode="24"
			android:contentDescription="@string/accessibility_volume_up"
			systemui:glowBackground="@drawable/ic_sysbar_highlight"
		/>
这样点击这个音量加按钮就会模拟发送一个24的键值

2. 平板上状态栏上加虚拟按键的另外一种方法

在frameworks/base/packages/SystemUI/res/layout-sw600dp/status_bar.xml 加入:

<?xml version="1.0" encoding="utf-8"?>
<!--
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
-->


<!-- TabletStatusBarView extends FrameLayout -->
<com.android.systemui.statusbar.tablet.TabletStatusBarView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui"
    android:background="@drawable/status_bar_background"
    >
    
    <FrameLayout
        android:id="@+id/bar_contents_holder"
        android:layout_width="match_parent"
        android:layout_height="@*android:dimen/status_bar_height"
        android:layout_gravity="bottom"
        >
        <RelativeLayout
            android:id="@+id/bar_contents"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipChildren="false"
            >


            <!-- notification icons & panel access -->
            <include layout="@layout/status_bar_notification_area" 
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginTop="1dp"
                />


            <!-- navigation controls -->
            <LinearLayout
                android:id="@+id/navigationArea"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:orientation="horizontal"
                android:clipChildren="false"
                android:clipToPadding="false"
                >
                <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"
                    android:layout_width="80dip"
                    android:layout_height="match_parent"
                    android:src="@drawable/ic_sysbar_back"
                    systemui:keyCode="4"
                    android:contentDescription="@string/accessibility_back"
                    systemui:glowBackground="@drawable/ic_sysbar_highlight"
                    />
                <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/home"
                    android:layout_width="80dip"
                    android:layout_height="match_parent"
                    android:src="@drawable/ic_sysbar_home"
                    systemui:keyCode="3"
                    android:contentDescription="@string/accessibility_home"
                    systemui:glowBackground="@drawable/ic_sysbar_highlight"
                    />
                <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"
                    android:layout_width="80dip"
                    android:layout_height="match_parent"
                    android:src="@drawable/ic_sysbar_recent"
                    android:contentDescription="@string/accessibility_recent"
                    systemui:glowBackground="@drawable/ic_sysbar_highlight"
                    />
                <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/menu"
                    android:layout_width="80dip"
                    android:layout_height="match_parent"
                    android:src="@drawable/ic_sysbar_menu"
                    systemui:keyCode="82"
                    android:visibility="invisible"
                    android:contentDescription="@string/accessibility_menu"
                    systemui:glowBackground="@drawable/ic_sysbar_highlight"
                    />
                    
                 <com.android.systemui.statusbar.policy.KeyButtonView
					android:id="@+id/status_bar_volume_up"
					android:layout_width="80dip" 
					android:layout_height="match_parent"
					android:src="@drawable/ic_sysbar_volume_up"
					systemui:glowBackground="@drawable/ic_sysbar_highlight"/>
            </LinearLayout>
2. 在frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java中加入按键的方法

mVolumeUp = mNavigationArea.findViewById(R.id.status_bar_volume_up);

		mVolumeUp.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					is_down = true;
					Adjust_Volume(true);
					mVolumeUpHandler.postDelayed(mVolumeUpRun, ADJUST_VOLUME_DELAY * 2);
					mVolumeUp.setImageResource(R.drawable.add_pressed);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					is_down = false;
					mVolumeUpHandler.removeCallbacks(mVolumeUpRun);
					mVolumeUp.setImageResource(R.drawable.add_normal);
				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					if ((event.getX() < 0) || (event.getY() < 0)
							|| (event.getX() > v.getWidth())
							|| (event.getY() > v.getHeight())) {
						is_down = false;
						mVolumeUpHandler.removeCallbacks(mVolumeUpRun);
						mVolumeUp.setImageResource(R.drawable.add_normal);
					}
				} else {
					
				}
				return true;
			}
		});
		
		    protected boolean is_down;
    private final int ADJUST_VOLUME_DELAY = 500;
	public void Adjust_Volume(boolean opition) {
		AudioManager audioManager = (AudioManager) getContext()
				.getSystemService(Context.AUDIO_SERVICE);
		if (audioManager != null) {
			//
			// Adjust the volume in on key down since it is more
			// responsive to the user.
			//
			if (opition) {
				audioManager.adjustSuggestedStreamVolume(
						AudioManager.ADJUST_RAISE,
						// AudioManager.USE_DEFAULT_STREAM_TYPE,
						// AudioManager.STREAM_MUSIC,
						AudioManager.STREAM_MUSIC,
						// AudioManager.FLAG_SHOW_UI |
						// AudioManager.FLAG_PLAY_SOUND |
						// AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE );
						AudioManager.FLAG_SHOW_UI
								| AudioManager.FLAG_PLAY_SOUND);
			} else {
				audioManager.adjustSuggestedStreamVolume(
						AudioManager.ADJUST_LOWER,
						// AudioManager.USE_DEFAULT_STREAM_TYPE,
						AudioManager.STREAM_MUSIC,
						// AudioManager.FLAG_SHOW_UI |
						// AudioManager.FLAG_PLAY_SOUND |
						// AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE );
						AudioManager.FLAG_SHOW_UI
								| AudioManager.FLAG_PLAY_SOUND);
			}
		}
	}

	private Handler mVolumeUpHandler = new Handler();
	private Runnable mVolumeUpRun = new Runnable() {
		public void run() {
			mVolumeUpHandler.removeCallbacks(mVolumeUpRun);
			if (is_down) {
				Adjust_Volume(true);
				mVolumeUpHandler.postDelayed(mVolumeUpRun, ADJUST_VOLUME_DELAY);
			}
		}
	};
	private Handler msubHandler = new Handler();
	private Runnable msubRun = new Runnable() {
		public void run() {
			msubHandler.removeCallbacks(msubRun);
			if (is_down) {
				Adjust_Volume(false);
				msubHandler.postDelayed(msubRun, ADJUST_VOLUME_DELAY);
			}
		}
	};
	
	private Handler removerButtonHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			is_down = false;
			msubHandler.removeCallbacks(msubRun);
			mVolumeUpHandler.removeCallbacks(mVolumeUpRun);
			mVolumeUp.setImageResource(R.drawable.add_normal);
		}
	};
		


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值