当前位置:  编程技术>移动开发
本页文章导读:
    ▪单行及多行图文混排,聊天应用较比常用,本文只提供算法        单行及多行图文混排,聊天应用较常用,本文只提供算法。// TTSingleLineView.h #import <UIKit/UIKit.h> #import "TTLineView.h" #define kChatRoomSingleLineSendFontSize (18.0f) @interface TTSingleLineView : TTLineView .........
    ▪ 学习好资源 - 硬件基础        学习好资源 ---- 硬件基础1、硬件基础: http://www.allaboutcircuits.com/ http://stardict.sourceforge.net/ ......
    ▪ 发送通报:Notification       发送通知:NotificationIntent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种 操作的意图。   PendingIntent表示的是暂时执行的一种意图,是一种在产生某一事件之.........

[1]单行及多行图文混排,聊天应用较比常用,本文只提供算法
    来源: 互联网  发布时间: 2014-02-18
单行及多行图文混排,聊天应用较常用,本文只提供算法。
// TTSingleLineView.h
#import <UIKit/UIKit.h>
#import "TTLineView.h"

#define kChatRoomSingleLineSendFontSize        (18.0f)

@interface TTSingleLineView : TTLineView

+ (TTSingleLineView *)singleLineView:(NSArray *)views;

@end

//TTSingleLineView.m
#import "TTSingleLineView.h"

#define kChatRoomSingleLineDefaultHeight (30.0f)

@implementation TTSingleLineView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.backgroundColor = [UIColor colorWithWhite:0.4f alpha:0.4f];
    }
    return self;
}

+ (TTSingleLineView *)singleLineView:(NSArray *)views
{
    TTSingleLineView *slv = [[TTSingleLineView alloc] initWithFrame:CGRectZero];
    [slv chatRoomSingleLineView:views];
    return slv;
}

- (CGFloat)chatRoomSingleLineView:(NSArray *)array
{
    CGFloat lineWidth = 0.0f;
    for (NSDictionary *dict in array)
    {
        switch ([self contentType:dict])
        {
            case kChatRoomText:
            {
                NSString *contentText = [self chatContentText:dict];
                CGFloat fontSize = [self chatContentTextFont:dict];
                CGFloat textWidth = [self getStringWidth:contentText size:fontSize];
                ChatRoomFontColorMode colorMode = [[dict valueForKey:kChatTextColorTypeKeyName] integerValue];
                UIColor *color = [self chatTextColor:colorMode];
                
                UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(lineWidth,
                                                                       0.0f,
                                                                       textWidth,
                                                                       kChatRoomSingleLineDefaultHeight)];
                l.backgroundColor = [UIColor clearColor];
                l.font = kFontOfSize(fontSize);
                l.text = contentText;
                l.textColor = color;
                l.lineBreakMode = NSLineBreakByCharWrapping;
                [self addSubview:l];
                
                lineWidth += textWidth;
            }
                break;
            case kChatRoomRemoteDynamicImage:
            case kChatRoomRemoteStaticImage:
            {
                NSString *imageStr = [self chatImagePath:dict];
                CGFloat imageWidth = [self chatImageWidth:dict];
                CGFloat imageHeight = [self chatImageHeight:dict];
                CGFloat imagePaddingX = [self chatImagePaddingX:dict];
                CGFloat imagePaddingY = [self chatImagePaddingY:dict];
                
                imageStr = [imageStr substringFromIndex:kChatCutOffContentRemoteStillImageType.length];
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                                          CGRectMake(lineWidth + imagePaddingX,
                                                     0.0f + imagePaddingY,
                                                     imageWidth,
                                                     imageHeight)];
                [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil];
                [self addSubview:imageView];
                
                lineWidth += imageWidth;
                lineWidth += imagePaddingX;
            }
                break;
            case kChatRoomLocalDynamicImage:
            case kChatRoomLocalStaticImage:
            {
                NSString *imageStr = [self chatImagePath:dict];
                CGFloat imageWidth = [self chatImageWidth:dict];
                CGFloat imageHeight = [self chatImageHeight:dict];
                CGFloat imagePaddingX = [self chatImagePaddingX:dict];
                CGFloat imagePaddingY = [self chatImagePaddingY:dict];
                               
                imageStr = [imageStr substringFromIndex:kChatCutOffContentRemoteStillImageType.length];
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                                          CGRectMake(lineWidth + imagePaddingX,
                                                     0.0f + imagePaddingY,
                                                     imageWidth,
                                                     imageHeight)];
                imageView.image = kImageNamed(imageStr);
                [self addSubview:imageView];
                
                lineWidth += imageWidth;
                lineWidth += imagePaddingX;
            }
                break;
                
            default:
                break;
        }
    }

    self.frame = CGRectMake(0.0f, 0.0f, lineWidth, kChatRoomSingleLineDefaultHeight);
    
    return kChatRoomSingleLineDefaultHeight;
}

- (CGFloat)getStringWidth:(NSString *)text size:(CGFloat)fontSize
{
    CGSize size = [text sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatRoomSingleLineDefaultHeight)];
    return size.width;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

// TTMultiLineView.h
#import <UIKit/UIKit.h>
#import "TTLineView.h"

@interface TTMultiLineView : TTLineView

@property (nonatomic, assign) CGFloat previousHeight;
// View's total height.
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat padding;

+ (TTMultiLineView *)multiLineView:(NSArray *)views;

@end

// TTMultiLineView.m
#import "TTMultiLineView.h"

#define kChatTextHeightDefault         (24.0f)
#define kChatViewWidthMax              (kScreenWidth - self.padding * 2)
#define kChatSplitSingleWordWidth      (15.0f)
#define kChatContentPadding            (0.0f)

// Parse.
#define kChatParseTextWidthKeyName     @"chat.parse.text.width"
#define kChatParseTextContentKeyName   @"chat.parse.text.content"
#define kChatParseTextCutPointKeyName  @"chat.parse.text.cutpoint"

@implementation TTMultiLineView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+ (TTMultiLineView *)multiLineView:(NSArray *)views
{
    TTMultiLineView *mlv = [[TTMultiLineView alloc] initWithFrame:CGRectZero];
    [mlv chatMultiLineView:views];
    return mlv;
}

- (CGFloat)chatMultiLineView:(NSArray *)array
{
    CGFloat viewWidth = 0.0f;
    CGFloat viewHeightMax = 0.0f;
    
    if (self.padding == 0.0f)
    {
        self.padding = kChatContentPadding;
    }
    
    for (NSDictionary *dict in array)
    {
        switch ([self contentType:dict])
        {
            case kChatRoomText:
            {
                NSString *contentText = [self chatContentText:dict];
                CGFloat fontSize = [self chatContentTextFont:dict];
                
                if (contentText != nil && contentText.length > 0)
                {
                    CGSize firstCharSize = [[contentText substringToIndex:1] sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatTextHeightDefault)];
                    
                    if (viewWidth + firstCharSize.width > kChatViewWidthMax)
                    {
                        // insert new line if last string reach end.
                        self.previousHeight += kChatTextHeightDefault;
                        viewWidth = 0.0f;
                    }
                }
                
                NSArray *sectionStrs = [self splitString:contentText start:viewWidth size:fontSize];
                
                // Multiline.
                NSUInteger line = 0;
                for (NSDictionary *d in sectionStrs)
                {
                    NSUInteger width = [[d valueForKey:kChatParseTextWidthKeyName] unsignedIntegerValue];
                    NSString *text = [d valueForKey:kChatParseTextContentKeyName];
                    
                    ChatRoomFontColorMode colorMode = [[dict valueForKey:kChatTextColorTypeKeyName] integerValue];
                    UIColor *color = [self chatTextColor:colorMode];
                    
                    if (line != 0)
                    {
                        viewWidth = 0.0f;
                        if (line == 1)
                        {
                            // First Line
                            self.previousHeight += MAX(kChatTextHeightDefault, viewHeightMax);
                            viewHeightMax = kChatTextHeightDefault;
                        }
                        else
                        {
                            // > 1 line.
                            self.previousHeight += kChatTextHeightDefault;
                        }
                    }
                    else
                    {
                        viewHeightMax = MAX(viewHeightMax, kChatTextHeightDefault);
                    }
                    
                    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth,
                                                                           self.previousHeight,
                                                                           width,
                                                                           kChatTextHeightDefault)];
                    l.backgroundColor = [UIColor clearColor];
                    l.font = kFontOfSize(fontSize);
                    l.text = text;
                    l.textColor = color;
                    l.lineBreakMode = NSLineBreakByCharWrapping;
//                    l.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
                    [self addSubview:l];
                    
                    if (line == [sectionStrs count] - 1)
                    {
                        // Last Line, recode width.
                        viewWidth += width;
                    }
                    
                    line++;
                }
            }
                break;
            case kChatRoomRemoteDynamicImage:
            case kChatRoomLocalDynamicImage:
            {
                NSString *imageStr = [self chatImagePath:dict];
                CGFloat imageWidth = [self chatImageWidth:dict];
                CGFloat imageHeight = [self chatImageHeight:dict];
                CGFloat imagePaddingX = [self chatImagePaddingX:dict];
                CGFloat imagePaddingY = [self chatImagePaddingY:dict];
                
                if (viewWidth + imageWidth > kChatViewWidthMax)
                {
                    // new line
                    self.previousHeight += viewHeightMax;
                    
                    viewHeightMax = MAX(viewHeightMax, imageHeight);
                    viewWidth = 0.0f;
                }
                
                imageStr = [imageStr substringFromIndex:kChatCutOffContentLocalDynamicImageType.length];
                //                LOGINFO(@"imageStr = %@", imageStr);
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                                          CGRectMake(viewWidth + imagePaddingX,
                                                     self.previousHeight + imagePaddingY,
                                                     imageWidth,
                                                     imageHeight)];
                imageView.image = [UIImage sd_animatedGIFNamed:imageStr];
                [self addSubview:imageView];
                
                viewWidth += imageWidth;
                viewWidth += imagePaddingX;
                viewHeightMax = MAX(imageHeight, viewHeightMax);
            }
                break;
            case kChatRoomLocalStaticImage:
            case kChatRoomRemoteStaticImage:
            {
                NSString *imageStr = [self chatImagePath:dict];
                CGFloat imageWidth = [self chatImageWidth:dict];
                CGFloat imageHeight = [self chatImageHeight:dict];
                CGFloat imagePaddingX = [self chatImagePaddingX:dict];
                CGFloat imagePaddingY = [self chatImagePaddingY:dict];
                
                //                LOGINFO(@"imageStr = %@", imageStr);
                
                if (viewWidth + imageWidth > kChatViewWidthMax)
                {
                    // new line
                    self.previousHeight += viewHeightMax;
                    
                    viewHeightMax = MAX(viewHeightMax, imageHeight);
                    viewWidth = 0.0f;
                }
                
                imageStr = [imageStr substringFromIndex:kChatCutOffContentRemoteStillImageType.length];
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                                          CGRectMake(viewWidth + imagePaddingX,
                                                     self.previousHeight + imagePaddingY,
                                                     imageWidth,
                                                     imageHeight)];
                [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil];
                [self addSubview:imageView];
                
                viewWidth += imageWidth;
                viewWidth += imagePaddingX;
                viewHeightMax = MAX(imageHeight, viewHeightMax);
            }
                break;
                
            default:
                break;
        }
    }
    
    CGFloat totalHeight = self.previousHeight + viewHeightMax;
    if (totalHeight == 0.0f)
    {
        // Text Single Line;
        totalHeight = kChatTextHeightDefault;
    }
    self.frame = CGRectMake(self.padding, 0.0f, kChatViewWidthMax, totalHeight);
    
    return totalHeight;
}

- (NSArray *)splitString:(NSString *)str start:(CGFloat)loc size:(CGFloat)fontSize
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    NSUInteger i = 1;
    NSUInteger location = 0;
    NSUInteger startPoint = 0;
    CGFloat startPaddingX = loc;
    
//    LOGINFO(@"str = %@ padding = %.2f", str, x);
    
    while (i < str.length)
    {
        NSRange range = NSMakeRange(location, i - startPoint);
        
        NSString *str1 = [str substringWithRange:range];
        CGSize size = [str1 sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatTextHeightDefault)];
//        LOGINFO(@"str1 = %@ size.width = %.2f", str1, size.width);
        
        if (size.width + startPaddingX + kChatSplitSingleWordWidth > kChatViewWidthMax)
        {
            startPaddingX = 0.0f;
            location += str1.length;
            startPoint = i;
            
            NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];
            [dictionary setValue:@(size.width) forKey:kChatParseTextWidthKeyName];
            [dictionary setValue:str1 forKey:kChatParseTextContentKeyName];
            [dictionary setValue:@(i) forKey:kChatParseTextCutPointKeyName];
            [array addObject:dictionary];
        }
        
        i++;
    }
    
    // Last Section String.
    NSString *lastSectionStr = [str substringFromIndex:location];
    CGSize lastStrSize = [lastSectionStr sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatTextHeightDefault)];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];
    [dictionary setValue:@(lastStrSize.width) forKey:kChatParseTextWidthKeyName];
    [dictionary setValue:lastSectionStr forKey:kChatParseTextContentKeyName];
    [dictionary setValue:@(i) forKey:kChatParseTextCutPointKeyName];
    [array addObject:dictionary];
    
    return array;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end


    
[2] 学习好资源 - 硬件基础
    来源: 互联网  发布时间: 2014-02-18
学习好资源 ---- 硬件基础

1、硬件基础:

http://www.allaboutcircuits.com/

http://stardict.sourceforge.net/


    
[3] 发送通报:Notification
    来源: 互联网  发布时间: 2014-02-18
发送通知:Notification

Intent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种

操作的意图。

  PendingIntent表示的是暂时执行的一种意图,是一种在产生某一事件之后才进行操作的Intent对象。

面试题:请解释Intent与PendingIntent的区别:

  Intent表示立即执行;

  PendingIntent表示暂缓执行,遇到特殊条件才执行。

 

 

发送通知:Notification

  Notification表示一种提示用户操作的组件。

  Notification表示的是一个通知,而NotificationManager表示的是一个发送通知的操作类。

 



 

 

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:gravity="center_horizontal"

    android:background="#000000">

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="8dp"

        android:textColor="#ffffff"

        android:text="请看上面的通知!"/>

</LinearLayout>

 

 

 

 

 

 

 

在MyNotificationDemo.java中:

 

package com.li.notification;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.view.Menu;

import android.view.MenuItem;

import android.support.v4.app.NavUtils;

 

public class MyNotificationDemo extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

        NotificationManager notificationManager =

           (NotificationManager)super.getSystemService(Activity

               .NOTIFICATION_SERVICE);

        Notification notification = new Notification(R.drawable.pic_m,"来自李叶文的消息",

           System.currentTimeMillis());  //立刻发送一个消息

        PendingIntent contentIntent = PendingIntent.getActivity(

           this, 0, super.getIntent(), PendingIntent

           .FLAG_CANCEL_CURRENT); //创建了一个PendingIntent对象

        notification.setLatestEventInfo(this, "广西", "北海", contentIntent);

        notificationManager.notify("LYW",R.drawable.pic_m,notification);

    }

}

 

 


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3