0

我认为从标题来看,这个问题有点令人困惑,并且会吸引很多(-),但您会发现它非常有趣。

我已经为 iOS 编写了我的不错的应用程序,我们想和一个朋友一起将它移植到Android中。一切都很顺利,除了一段代码。

在我的 iOS 应用程序中,我在加载 UIWebview 时显示了一个 UIView,对其进行动画处理。当 UIWebview 完成加载时,我将其隐藏。我用这个animateWithDuration函数制作的动画。我们到了:

界面视图:

@property (nonatomic,retain) IBOutlet UIView *animationLayer;
_animationLayer.hidden=YES;

在 Viewdidload 中初始化

_animationLayer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _animationLayer.backgroundColor=[UIColor colorWithRed:(arc4random() % 255)/255.0f green:(arc4random() % 255)/255.0f blue:(arc4random() % 255)/255.0f alpha:1.0f];
    _animationLayer.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

加载时显示 UIView 并开始对其进行动画处理:

- (void) webViewDidStartLoad:(UIWebView *)webView {
[UIView animateWithDuration:7.0
                      delay:0.0
                    options: UIViewAnimationOptionCurveEaseOut
                 animations:^{ _animationLayer.backgroundColor=[UIColor colorWithRed:(arc4random() % 255)/255.0 green:(arc4random() % 255)/255.0 blue:(arc4random() % 255)/255.0 alpha:1.0f];}
                 completion:nil];
}

在完成加载时隐藏它

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    _animationLayer.hidden=YES;
}

这就是我认为它可以在Android中制作的方式。在加载时显示自定义进度条,完成后将其隐藏。进度条可以在全屏画布中自定义,就像上面的 iOS 代码一样改变它的颜色。但没有运气!:( 有什么建议或简单的方法吗?

谢谢!

4

3 回答 3

1

好的,为了在进行网络通话时在后台为 Android 制作动画,请使用 AsyncTask。看起来像这样

   private class WebCallOperation extends AsyncTask<String, Void, String>
        {
            private final ProgressDialog dialog = new ProgressDialog(context);

            @Override
            protected String doInBackground(String... params)
            {

                //web call code here

                //response returned here
                return "";
            }

            @SuppressWarnings("rawtypes")
            @Override
            protected void onPostExecute(String result)
            {
                if (this.dialog.isShowing())
                {
                    this.dialog.dismiss();
                }
            }

            @Override


protected void onPreExecute()
        {
            this.dialog.setMessage("Loading");
            this.dialog.show();
        }
    }

在我刚刚给出的示例代码中,我添加了 ProgressDialog,这通常是我在进行网络呼叫时使用的,但是您可以将其替换为您想要的任何内容。您可以添加自己的视图,并为其设置动画。在 pre execute 方法中对其进行动画处理,然后在 post execute 方法中完成 web 调用。

关于 IOS 应用程序的一点建议,我在评论中已经提到了持续时间问题。我还认为您应该研究用于执行网络呼叫的块代码。如果你希望我可以提供一些示例代码,会让你的生活更轻松

编辑:

Objective-C 块代码。在此处创建此类

接口类

#import <Foundation/Foundation.h>
#import "WebCall.h"

@interface WebCall : NSObject
{
    void(^webCallDidFinish)(NSString *response);

}

@property (nonatomic, retain) NSMutableData *responseData;

-(void)setWebCallDidFinish:(void (^)(NSString *))wcdf;

-(void)webServiceCall :(NSString *)sURL_p : (NSMutableArray *)valueList_p : (NSMutableArray *)keyList_p;

@end

实现类

#import "WebCall.h"
#import "AppDelegate.h"
@implementation WebCall

@synthesize responseData;

-(void)setWebCallDidFinish:(void (^)(NSString *))wcdf
{
    webCallDidFinish = [wcdf copy];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    int responseStatusCode = [httpResponse statusCode];

    NSLog(@"Response Code = %i", responseStatusCode);
    if(responseStatusCode < 200 || responseStatusCode > 300)
    {
        webCallDidFinish(@"failure");
    }



    [responseData setLength:0];
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data]; 
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    NSLog(@"WebCall Error: %@", error);
    webCallDidFinish(@"failure");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

        NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        response = [response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        webCallDidFinish(response);

}

-(void)webServiceCall :(NSString *)sURL_p : (NSMutableArray *)valueList_p : (NSMutableArray *)keyList_p
{
    NSMutableString *sPost = [[NSMutableString alloc] init];

    //If any variables need passed in - append them to the POST
    //E.g. if keyList object is username and valueList object is adam will append like
    //http://test.jsp?username=adam
    if([valueList_p count] > 0)
    {
        for(int i = 0; i < [valueList_p count]; i++)
        {
            if(i == 0)
            {
                    [sPost appendFormat:@"%@=%@", [valueList_p objectAtIndex:i],[keyList_p objectAtIndex:i]];
            }
            else
            {

                    [sPost appendFormat:@"&%@=%@", [valueList_p objectAtIndex:i], [keyList_p objectAtIndex:i]];
            }
        }
    }


    NSData * postData = [sPost dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
    NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];



    NSURL * url = [NSURL URLWithString:sURL_p];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    if (theConnection)
    {
        self.responseData = [NSMutableData data];
    }


}

@end

然后你打这个网络电话,你这样称呼它

 WebCall *webCall = [[WebCall alloc] init];


    [webCall setWebCallDidFinish:^(NSString *str){

        //This method is called as as soon as the web call is finished
        NSLog(@"%@", str);
    }];


    //Make web call here
    [webCall webServiceCall:@"http://www.bbc.co.uk/" :nil :nil];

参见 setWebCallDidFinish 方法,直到 webcall 结束才会调用它。因此,如果您需要在网络调用完成后立即执行一些代码,例如停止动画,请在该方法中调用它。希望有帮助

于 2013-06-19T15:03:11.820 回答
1

尝试类似Apportable会容易得多。它使用 clang 并将所有的 Objective-c 编译为 Android 的原生 ARM 代码。您所做的一切都将在 android 上的 SDK 之上开箱即用。

于 2013-07-05T02:15:34.663 回答
0

你可以试试ObjC2J

ObjC2J 是一个开源库工具,可将 Objective-C (Mac OS X) 代码转换为适用于 PC 平台的 Java。

于 2013-06-19T14:57:03.750 回答