現在可以在 Windows OS 安裝 BlueStacks beta-1 版本, 運行 Android OS.
網站: http://bluestacks.com/
演示視頻:
2012年5月30日星期三
2012年5月24日星期四
在百度地图(Baidu Map)顯示我的位置(MyLocation)和指南針(Compass)
只要在百度地图(Baidu Map)的地圖視圖(MapView)添加我的位置覆蓋(MyLocationOverlay), 便可以很容易顯示我的位置和指南針.
示例代碼:
MKLocationManager mLocationManager = mBMapMan.getLocationManager();
mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
mLocationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER);
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mMapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
mMapView.getOverlays().add(myLocationOverlay);
修改前文"編寫簡單的百度地图(Baidu Map)應用程序"的 BaiduMapActivity.java
示例代碼:
MKLocationManager mLocationManager = mBMapMan.getLocationManager();
mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
mLocationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER);
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mMapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
mMapView.getOverlays().add(myLocationOverlay);
修改前文"編寫簡單的百度地图(Baidu Map)應用程序"的 BaiduMapActivity.java
package com.BaiduMap;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import android.os.Bundle;
public class BaiduMapActivity extends MapActivity {
//使用你自己的密鑰
//參考準備工作 http://goo.gl/j6HkK
final static String MY_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
BMapManager mBMapMan;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBMapMan = new BMapManager(getApplication());
mBMapMan.init(MY_KEY, null);
super.initMapActivity(mBMapMan);
MapView mMapView = (MapView) findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true);
MapController mMapController = mMapView.getController();
GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
(int) (116.404 * 1E6));
mMapController.setCenter(point);
mMapController.setZoom(12);
//Add MyLocationOverlay
MKLocationManager mLocationManager = mBMapMan.getLocationManager();
mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
mLocationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER);
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mMapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
mMapView.getOverlays().add(myLocationOverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onDestroy() {
if (mBMapMan != null) {
mBMapMan.destroy();
mBMapMan = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
if (mBMapMan != null) {
mBMapMan.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if (mBMapMan != null) {
mBMapMan.start();
}
super.onResume();
}
}
2012年5月23日星期三
編寫簡單的百度地图(Baidu Map)應用程序
完成前文的準備工作, 現在我們可以動手編寫百度地图應用程序.
- 修改 main.xml, 加入 com.baidu.mapapi.MapView, 這是百度地图的視圖.
<?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" />
<com.baidu.mapapi.MapView
android:id="@+id/bmapsView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
- 修改活動的主要 Java 代碼, 注意它是擴展 MapActivity, 不是 Activity. MY_KEY字串須使用你自己的密鑰, 參考前文"準備工作".
package com.BaiduMap;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import android.os.Bundle;
public class BaiduMapActivity extends MapActivity {
//使用你自己的密鑰
//參考準備工作 http://goo.gl/j6HkK
final static String MY_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
BMapManager mBMapMan;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBMapMan = new BMapManager(getApplication());
mBMapMan.init(MY_KEY, null);
super.initMapActivity(mBMapMan);
MapView mMapView = (MapView) findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true);
MapController mMapController = mMapView.getController();
GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
(int) (116.404 * 1E6));
mMapController.setCenter(point);
mMapController.setZoom(12);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onDestroy() {
if (mBMapMan != null) {
mBMapMan.destroy();
mBMapMan = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
if (mBMapMan != null) {
mBMapMan.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if (mBMapMan != null) {
mBMapMan.start();
}
super.onResume();
}
}
相關文章:
- 在百度地图(Baidu Map)顯示我的位置(MyLocation)和指南針(Compass)
2012年5月22日星期二
在Android應用程序使用百度地图(Baidu Map) - 準備工作
類似谷歌地圖(Google Maps), 百度地图移动版API(Android)是一套基于Android 1.5及以上版本设备的应用程序接口,通过该接口,您可以轻松访问百度服务和数据,构建功能丰富、交互性强的地图应用程序。百度地图移动版API不仅包含构建地图的基本接口,还提供了诸如本地搜索、路线规划等数据服务,你可以根据自己的需要进行选择。
在動手編寫百度地图應用程序以前, 我們必須準備一些工作:
- 申请Key: 在使用API之前需要获取百度地图移动版API Key,该Key与你的百度账户相关联,您必须先有百度帐户,才能获得API KEY。并且,该KEY与您引用API的程序名称有关,具体流程请参照获取密钥。请妥善保存Key,地图初始化时需要用到Key。
- 把API开发包添加到Android工程的libs中: 瀏覽Android平台/相关下载網頁, 點擊API开发包下载Android工程中引用的jar和so文件。首先在工程里新建libs文件夹,并API开发包里的baidumapapi.jar拷贝到libs根目录下,将libBMapApiEngine.so拷贝到libs\armeabi目录下。
- 添加API开发包入工程的構建路徑:
右擊工程, 選擇 Properties.
選擇 Java Build Path, 按Add JARS...按鈕.
打開工程下面的libs文件夾,選擇baidumapapi.jar, 按OK按鈕.
再按OK按鈕完成.
- 修改AndroidManifest.xml添加使用权限Android版本支持:
<supports-screens android:largeScreens="true"
android:normalScreens="true" android:smallScreens="true"
android:resizeable="true" android:anyDensity="true"/>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
- 接下來我們就可以開始編寫百度地图應用程序...待續.
使用 HorizontalScrollView 和 ScrollView 顯示大圖
我們可以使用 HorizontalScrollView 和 ScrollView 顯示比屏幕大的圖像.
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/bigpicture"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="center"/>
</ScrollView>
</HorizontalScrollView>
2012年5月18日星期五
蜂巢(Honeycomb)的對話片段(DialogFragment)
來到 Android 3, 蜂巢(Honeycomb), 為支持應用程序中的多個活動間(multiple activities)的UI和邏輯重用, showDialog()和dismissDialog()方法被棄用, 取而代之的是對話片段(DialogFragment).
實現話片段(DialogFragment), 先創建它的佈局文件和擴展 DialogFragment 類.
實現話片段(DialogFragment)的佈局文件, /res/layout/dialogfragments.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="match_parent"
android:layout_height="wrap_content"
android:text="I'm Dialog Fragment" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
創建擴展 DialogFragment 的類, MyDialogFragment.java.
package com.Android3DialogFragments;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyDialogFragment extends DialogFragment {
public MyDialogFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myDialogFragmentView = inflater.inflate(R.layout.dialogfragments, container);
getDialog().setTitle("MyDialogFragment");
return myDialogFragmentView;
}
}
修改主佈局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/opendialogfragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Dialog Fragment" />
</LinearLayout>
主活動的Java代碼
package com.Android3DialogFragments;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Android3DialogFragmentsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button openDialogFragment = (Button)findViewById(R.id.opendialogfragment);
openDialogFragment.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
openMyDialogFragment();
}});
}
private void openMyDialogFragment(){
FragmentManager fragmentManager = getFragmentManager();
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(fragmentManager, "tag_myDialogFragment");
}
}
2012年5月13日星期日
Android 4, Ice Cream Sandwich (ICS), 的網格佈局(GridLayout)
Android 4, Ice Cream Sandwich (ICS) API Level 14, 提供新的的網格佈局(GridLayout). 注意, 網格佈局(GridLayout)不同於網格視圖(GridView). 下面是一個網格佈局的簡單例子.
<?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" />
<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:useDefaultMargins="true"
android:columnCount="4"
android:rowCount="3"
>
<TextView
android:background="@android:color/background_light"
android:text="A"/>
<TextView
android:layout_column="1"
android:layout_row="0"
android:background="@android:color/background_light"
android:text="B"/>
<ImageView
android:layout_column="2"
android:layout_row="0"
android:background="@android:color/background_light"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_column="3"
android:layout_row="0"
android:background="@android:color/background_light"
android:text="D"/>
<TextView
android:layout_column="1"
android:layout_row="1"
android:text="F"/>
<TextView
android:text="G"/>
<TextView
android:text="H"/>
<Button
android:layout_columnSpan="3"
android:text="I, J, K"/>
<TextView
android:text="L"/>
</GridLayout>
</LinearLayout>
Android Developers Blog上有一編講解網格佈局很好的文章.
訂閱:
留言 (Atom)









