//
// ViewController.h
// UIKeyboardTextNotification
//
// Created by tuxiangqi on 12-8-10.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
}
@property (nonatomic, strong) UITableView *myTableView;
@end
//
// ViewController.m
// UIKeyboardTextNotification
//
// Created by tuxiangqi on 12-8-10.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
@implementation ViewController
@synthesize myTableView;
-(void)viewDidLoad{
[super viewDidLoad];
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.myTableView];
}
-(void)viewDidUnload{
[self setMyTableView:nil];
[super viewDidUnload];
}
//页面出现前,添加监听 键盘事件
- (void) viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(handleKeyboardWillShow:)
name:UIKeyboardWillShowNotification //键盘将出现 事件监听
object:nil];
[center addObserver:self selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification //键盘将隐藏 事件监听
object:nil];
}
- (void) viewDidDisappear:(BOOL)paramAnimated {
[super viewDidDisappear:paramAnimated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
/* Make sure the Done button on the keyboard for each text field (accessory views of each cell) dismisses the keyboard */ [textField resignFirstResponder];
return YES;
}
- (void) handleKeyboardWillShow:(NSNotification *)paramNotification{
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *animationDurationObject =[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSUInteger animationCurve = 0;
double animationDuration = 0.0f;
CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);
[animationCurveObject getValue:&animationCurve];
[animationDurationObject getValue:&animationDuration];
[keyboardEndRectObject getValue:&keyboardEndRect];
[UIView beginAnimations:@"changeTableViewContentInset"
context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(window.frame, keyboardEndRect);
CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height;
self.myTableView.contentInset = UIEdgeInsetsMake(0.0f,0.0f,bottomInset,0.0f);
NSIndexPath *indexPathOfOwnerCell = nil;
/* Also, make sure the selected text field is visible on the screen */
NSInteger numberOfCells = [self.myTableView.dataSource tableView:self.myTableView
numberOfRowsInSection:0];
/* So let's go through all the cells and find their accessory text fields.
Once we have the refernece to those text fields, we can see which one of
them is the first responder (has the keyboard) and we will make a call
to the table view to make sure after the keyboard is displayed,
that specific cell is NOT obstructed by the keyboard */
for (NSInteger counter = 0;counter < numberOfCells;counter++){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:counter inSection:0];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
UITextField *textField = (UITextField *)cell.accessoryView;
if ([textField isKindOfClass:[UITextField class]] == NO)
{
continue;
}
if ([textField isFirstResponder])
{
indexPathOfOwnerCell = indexPath;
break;
}
}
[UIView commitAnimations];
if (indexPathOfOwnerCell != nil){
[self.myTableView scrollToRowAtIndexPath:indexPathOfOwnerCell
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
}
- (void) handleKeyboardWillHide:(NSNotification *)paramNotification{
if (UIEdgeInsetsEqualToEdgeInsets(self.myTableView.contentInset, UIEdgeInsetsZero))
{
/* Our table view's content inset is intact so no need to reset it */
return;
}
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSUInteger animationCurve = 0;double animationDuration = 0.0f;
CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);
[animationCurveObject getValue:&animationCurve];
[animationDurationObject getValue:&animationDuration];
[keyboardEndRectObject getValue:&keyboardEndRect];
[UIView beginAnimations:@"changeTableViewContentInset" context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve]; self.myTableView.contentInset = UIEdgeInsetsZero;[UIView commitAnimations];
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
static NSString *CellIdentifier = @"CellIdentifier";
result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (result == nil){
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
result.selectionStyle = UITableViewCellSelectionStyleNone;
}
result.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row];
CGRect accessoryRect = CGRectMake(0.0f,
0.0f,
150.0f,
31.0f);
UITextField *accesssory = [[UITextField alloc] initWithFrame:accessoryRect];
accesssory.borderStyle = UITextBorderStyleRoundedRect;
accesssory.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
accesssory.placeholder = @"Enter Text";
accesssory.delegate = self;
result.accessoryView = accesssory;
return result;
}
@end
SYSZUXpinyin 只能用到arm上, x86下用不了。原因是SYSZUXpinyin的实现,用到了一个QWSinputMethod
的类,这个类是arm平台的qt库所特有的。还记得我们在终端里启用qt程序后面要加个-qws参数,原因就在这。
所以,用SYSZUXpinyin在arm平台尽管移植成功,但pc机上跑不了,增加了调试的复杂性。希望以后SYSZUXpinyin能考虑下这个问题!
就为了在pc上跑通SYSZUXpinyin,浪费了我一个上午时间查资料
先说说我的测试机器:nexus s,操作系统:android 4.1。以下的结果都是通过nexus s上测试通过。
还是直接上代码
package com.myMap; import java.util.ArrayList; import java.util.List; import android.R.drawable; import android.R.integer; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.location.IONSetLocation; import com.location.MyMapABCLocationListener; import com.location.MyMapABCLocationManager; import com.mapabc.mapapi.core.GeoPoint; import com.mapabc.mapapi.core.OverlayItem; import com.mapabc.mapapi.map.ItemizedOverlay; import com.mapabc.mapapi.map.MapActivity; import com.mapabc.mapapi.map.MapController; import com.mapabc.mapapi.map.MapView; import com.mapabc.mapapi.map.Overlay; import com.mapabc.mapapi.map.Projection; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class MyMapActivity extends MapActivity implements OnClickListener{ /** Called when the activity is first created. */ private MapView mMapView;//地图VIEW private MapController mMapController;//控制器 private List<Overlay> mOverlayList; //地图图层容器 private boolean mRegisteredSensor; private MyMapABCLocationListener mlLocationListener; private MyMapABCLocationManager mLocationManager; private MyOverlay mOverlay; //我的位置图层 private Handler mHandler; private boolean isFirstCenter=true; private GeoPoint geoPoint; private boolean isLocated=false; private Button btn; private Sensor orientSensor; private SensorManager mSensorManager; private SensorEventListener OrientSensorListener=new SensorEventListener(){ public void onAccuracyChanged(Sensor sensor,int accuracy){ } public void onSensorChanged(SensorEvent event) {//从方向传感器获取手机所对的方向 // TODO Auto-generated method stub if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){ float x=event.values[SensorManager.DATA_X]; mOverlay.setDegree((int)x); mMapView.postInvalidate();//刷新mapview } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mRegisteredSensor=false; mMapView = (MapView) findViewById(R.id.main_mapView); mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件 mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放 mOverlayList = mMapView.getOverlays(); // 得到图层容器 mMapController.setZoom(mMapView.getMaxZoomLevel()-1);//获取地图放大级别 btn=(Button)findViewById(R.id.search); btn.setOnClickListener(this); mOverlay=new MyOverlay(getResources().getDrawable(R.drawable.marker3)); ///////////////////////////// mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE); orientSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); //////////////////////////// mOverlayList.add(mOverlay); initLogic(); } @Override public void onClick(View v){ switch(v.getId()) { case R.id.search: if(geoPoint!=null){ mMapController.animateTo(geoPoint); } } } private void initLogic(){ mHandler=new Handler(){ @Override public void handleMessage(Message msg){ switch (msg.what) { case 3: Toast.makeText(MyMapActivity.this, "location is null", Toast.LENGTH_SHORT).show(); break; default: break; } } }; mLocationManager=new MyMapABCLocationManager(this); mlLocationListener=new MyMapABCLocationListener(mHandler, new UpdateLocationRunnable()); Location location=mLocationManager.getLastKnowLocation(); if(location!=null){ mOverlay.setLocation(location); GeoPoint focusGeoPoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); mMapController.animateTo(focusGeoPoint); mMapView.postInvalidate(); }//////获取上次所在位置,如果不为null显示出来 } public void firsCenter(Location location) { if (location != null) { GeoPoint focusGeoPoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); mMapController.animateTo(focusGeoPoint); } } @Override protected void onDestroy(){ super.onDestroy(); if(mOverlay!=null){ mOverlayList.clear(); } mLocationManager.clear(); } @Override//////////对于网络断开后重连,重要 protected void onPause(){ super.onPause(); mLocationManager.unRegisterListen(); if(mRegisteredSensor){ mSensorManager.unregisterListener(OrientSensorListener); mRegisteredSensor=false; } } @Override protected void onResume(){//////////对于网络断开后重连,重要 mLocationManager.registerListener(mlLocationListener); Toast.makeText(this, "resume", Toast.LENGTH_SHORT).show(); mRegisteredSensor=mSensorManager.registerListener(OrientSensorListener, orientSensor,SensorManager.SENSOR_DELAY_GAME); if(!mRegisteredSensor){ Toast.makeText(getApplicationContext(), "not support orientSensor", Toast.LENGTH_LONG).show(); } super.onResume(); } class UpdateLocationRunnable implements IONSetLocation{ private Location location; private String address; private boolean mVlid; public UpdateLocationRunnable(){ } @Override public void setAdress(String adress) { // TODO Auto-generated method stub } @Override public void setLocation(Location location) { // TODO Auto-generated method stub this.location=location; }@Override public void run() { // TODO Auto-generated method stub if(location!=null){ }else{ Toast.makeText(MyMapActivity.this, "Gaode -->no location...", Toast.LENGTH_SHORT).show(); } mOverlay.setLocation(location); if(isFirstCenter){ firsCenter(location); } geoPoint= new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6)); mMapView.postInvalidate();//刷新地图 } }
}/* * 我的位置图层 */class MyOverlay extends Overlay{private Location mLastLocation;private GeoPoint mLastGeoPoint;private Drawable drawable;private float degree;//图片旋转角度public MyOverlay(Drawable drawable){this.drawable=drawable;}public void setLocation(Location location){if (location != null){mLastLocation = location;mLastGeoPoint = new GeoPoint((int) (mLastLocation.getLatitude() * 1E6),(int) (mLastLocation.getLongitude() * 1E6));}}public void setDegree(float degree){this.degree=degree;}@Overridepublic void draw(Canvas canvas, MapView mapView, boolean shadow) {super.draw(canvas, mapView, shadow);// TODO Auto-generated method stub if (mLastLocation != null){ Projection projection = mapView.getProjection(); Point screenPts = new Point(); screenPts=projection.toPixels(mLastGeoPoint, null); Paint mPaint=new Paint(); mPaint.setColor(Color.BLUE); mPaint.setAlpha(5); float radius = projection.metersToEquatorPixels(mLastLocation.getAccuracy());canvas.drawCircle(screenPts.x, screenPts.y , radius, mPaint);//以精准度为半径画圆Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();//画出“我的位置”图标Matrix matrix=new Matrix();matrix.setRotate(degree,0,0);bitmap=Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(),matrix,false); canvas.drawBitmap(bitmap, screenPts.x-bitmap.getWidth()/2-0.5f,screenPts.y-bitmap.getHeight()/2-0.5f, new Paint());} }}.MyMapABCLocationListener和MyMapABCLocationManager是参考别人代码封转的,由于篇幅的关系不给出来了(类详见代码:TestMapABCDemo.zip 很不错的参考代码)
我想说是旋转图片的问题。经过实践,如果一个图片经过旋转后,它的中心位置会发生改变。之前我的做法是获取原图的的中心位置,但是发现后面画出来的图片会“漂”
还有的是onResume和onDestory。注销了listener之后一定要把它注册回去,不然就不会监听消息了。
附件:myMap.rar
警告:android camera系列的文章是由一个刚接触android不到一个月的菜鸟所写,所以必然存在很多错误,请大家多多指出