原来在dialog的onCreate onStart调用的时候UI还没出来 这时候调用动画是不会运行的。
解决办法可以看这里。
http://googlers.iteye.com/blog/907136
其实也就是加一个handler, 让它延时去做
下面是我的LoadingDialog
package com.example;
import android.app.Dialog;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.ImageView;
/**
* Created by IntelliJ IDEA.
* User: denny
* Date: 11-9-27
* Time: 下午3:21
* To change this template use File | Settings | File Templates.
*/
public class LoadingDialog extends Dialog{
private Handler handler = new Handler();
private ImageView iv;
private AnimationDrawable ad;
public LoadingDialog(Context context) {
super(context, android.R.style.Theme_Translucent_NoTitleBar);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.loading_dialog);
iv = (ImageView) findViewById(R.id.loadingImg);
iv.setBackgroundResource(R.drawable.loading);
ad = (AnimationDrawable) iv.getBackground();
}
@Override
public void show() {
super.show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
ad.start();
}
}, 50);
}
@Override
public void dismiss() {
super.dismiss();
ad.stop();
}
}
转自cocoachina:http://www.cocoachina.com/bbs/read.php?tid=73570&page=13
MapKit学习笔记
1、概述
插入MapView,设置Delegate(一般为Controller),Annotations记录兴趣位置点(AnnotationView用来显示兴趣位置点),annotation是可选的,选中的annotation会显示callout,用来显示信息。
2、设置地图显示类型:
mapView.mapType = MKMapTypeStandard;
mapView.mapType = MKMapTypeSatellite;
mapView.mapType = MKMapTypeHybrid;
3、显示用户位置
设置为可以显示用户位置:
mapView.showsUserLocation = YES;
判断用户当前位置是否可见(只读属性):
userLocationVisible
得到用户位置坐标:当userLocationVisible为YES时
CLLocationCoordinate2D coords = mapView.userLocation.location.coordinate;
4、坐标范围
MKCoordinateRegion用来设置坐标显示范围。
包括两部分:Center(CLLocationCoordinate2D struct,包括latitude和longitude),坐标中心
和Span(MKCoordinateSpan struct,包括latitudeDelta和longitudeDelta),缩放级别
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(center,2000, 2000);
以上代码创建一个以center为中心,上下各1000米,左右各1000米得区域,但其是一个矩形,不符合MapView的横纵比例
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
以上代码创建出来一个符合MapView横纵比例的区域
[mapView setRegion:adjustedRegion animated:YES];
以上代码为:最终显示该区域
5、Delegate
使用MapView须符合MKMapViewDelegate协议
5.1、地图加载Delegate
当需要从Google服务器取得新地图时
mapViewWillStartLoadingMap:
当成功地取得地图后
mapViewDidFinishLoadingMap:
当取得地图失败后(建议至少要实现此方法)
mapViewDidFailLoadingMap:withError:
5.2、范围变化Delegate
当手势开始(拖拽,放大,缩小,双击)
mapView:regionWillChangeAnimated:
当手势结束(拖拽,放大,缩小,双击)
mapView:regionDidChangeAnimated:
判断坐标是否在MapView显示范围内:
CLLocationDegrees leftDegrees = mapView.region.center.longitude –(mapView.region.span.longitudeDelta / 2.0);
CLLocationDegrees rightDegrees = mapView.region.center.longitude +(mapView.region.span.longitudeDelta / 2.0);
CLLocationDegrees bottomDegrees = mapView.region.center.latitude –(mapView.region.span.latitudeDelta / 2.0);
CLLocationDegrees topDegrees = self.region.center.latitude +(mapView.region.span.latitudeDelta / 2.0);
if (leftDegrees > rightDegrees) { // Int'l Date Line in View
leftDegrees = -180.0 - leftDegrees;
if (coords.longitude > 0) // coords to West of Date Line
coords.longitude = -180.0 - coords.longitude;
}
If (leftDegrees <= coords.longitude && coords.longitude <= rightDegrees && bottomDegrees <= coords.latitude && coords.latitude <= topDegrees) {
// 坐标在范围内
}
6、Annotation
Annotation包含两部分:Annotation Object和Annotation View
Annotation Object必须符合协议MKAnnotation,包括两个方法:title和subtitle,分别用于显示注释的标题和子标题。还有coordinate属性,返回CLLocationCoordinate2D,表示Annotation的位置
然后,需使用mapView:viewForAnnotation: 方法来返回MKAnnotationView或者MKAnnotationView的子类用来显示Annotation(注意:这里显示的不是选中Annotation后的弹出框)
你可以子类化MKAnnotationView,然后再drawRect:方法里面进行自己的绘制动作(这个方法很蠢)
你完全可以实例化一个MKAnnotationView,然后更改它的image属性,这样很简单。
7、添加移除Annotation
添加一个Annotation
[mapView addAnnotation:annotation];
添加一个Annotation数组
[mapView addAnnotations:[NSArray arrayWithObjects:annotation1, annotation2, nil]];
移除一个Annotation
removeAnnotation:
移除一个Annotation数组
removeAnnotations:
移除所有Annotation
[mapView removeAnnotations:mapView.annotations];
8、选中Annotation
一次只能有一个Annotation被选中,选中后会出现CallOut(浮动框)
简单的CallOut显示Title和SubTitle,但你也可以自定义一个UIView作为CallOut(与自定义的TableViewCell一样)
可通过代码选中Annotation:
selectAnnotation:animated:
或者取消选择:
deselectAnnotation:animated:
9、显示Annotation
通过mapView:viewForAnnotation: 方法显示Annotation,每在MapView中加入一个Annotation,就会调用此方法
示例(与tableView:cellForRowAtIndexPath: 很相似)
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
static NSString *placemarkIdentifier = @"my annotation identifier";
if ([annotation isKindOfClass:[MyAnnotation class]]) {
MKAnnotationView *annotationView = [theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
annotationView.image = [UIImage imageNamed:@"blood_orange.png"];
}
else
annotationView.annotation = annotation;
return annotationView;
}
return nil;
}
10、取得真实地址
示例:
初始化MKReverseGeocoder
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinates];
geocoder.delegate = self;
[geocoder start];
如果无法处理坐标,则调用reverseGeocoder:didFailWithError: 方法
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(@"Error resolving coordinates: %@", [error localizedDescription]);
geocoder.delegate = nil;
[geocoder autorelease];
}
如果成功,则调用reverseGeocoder:didFindPlacemark: 并把信息存储在MKPlacemark 中
didFindPlacemark:(MKPlacemark *)placemark {
NSString *streetAddress = placemark.thoroughfare;
NSString *city = placemark.locality;
NSString *state = placemark.administrativeArea;
NSString *zip = placemark.postalCode;
// Do something with information
geocoder.delegate = nil;
[geocoder autorelease];
}
要传输自定类型必须继承KvmSerializable接口
public class User implements KvmSerializable {
private String name = null;
private int age = 0;
@Override
public Object getProperty(int arg0) {
// TODO Auto-generated method stub
Object res = null;
switch(arg0){
case 0:
res = this.name;
break;
case 1:
res = this.age;
break;
default:
break;
}
return res;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
// TODO Auto-generated method stub
switch(arg0){
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Name";
break;
case 1:
arg2.type = PropertyInfo.INTEGER_CLASS;
arg2.name = "Age";
break;
default:
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
// TODO Auto-generated method stub
if(arg1 == null) return;
switch(arg0){
case 0:
this.name = arg1.toString();
break;
case 1:
this.age = Integer.valueOf(arg1.toString());
break;
default:
break;
}
}
}
//调用webservice的具体方法
public String SayHello(){
String nameSpace = "http://tempuri.org/";
String methodName = "HelloWorld";
String soapAction = "http://tempuri.org/HelloWorld";
String url = "http://192.168.2.51/Default.asmx?wsdl";//后面加不加那个?wsdl参数影响都不大
//建立webservice连接对象
org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;//是否是调试模式
//设置连接参数
//重要必须的
SoapObject soapObject = new SoapObject(nameSpace, methodName);
User user = new User();
user.setProperty(0, "zhi");
user.setProperty(1, 18);
PropertyInfo pi = new PropertyInfo();
pi.setName("user");//webservice接口的参数名,大小写必须跟dotnet中的webservice暴露出来的名字一致
pi.setValue(user);
pi.setType(user.getClass());
soapObject.addProperty(pi);//将自定参数加入请求对象中
//设置返回参数
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//soap协议版本必须用SoapEnvelope.VER11(Soap V1.1)
envelope.dotNet = false;//注意:这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice 不指定rpc方式则用true否则要用false
//envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);//设置请求参数
//重要必须的
envelope.addMapping(nameSpace, "User", user.getClass());//传对象时必须,参数namespace是webservice中指定的, name是服务器类型的名称, claszz是自定义类的类型
try {
transport.call(soapAction, envelope);
SoapObject sb = (SoapObject)envelope.bodyIn;//服务器返回的对象存在envelope的bodyIn中
//重要必须的
User us= (User)envelope.getResponse();//直接将返回值强制转换为已知对象
return us.getName() + us.getAge();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
return "";
}