当前位置:  编程技术>移动开发
本页文章导读:
    ▪HashMap的两种遍历模式        HashMap的两种遍历方式 第一种:  Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue.........
    ▪ 访问iPod Library及MPMusicPlayerController的一些小结        访问iPod Library及MPMusicPlayerController的一些总结     1.访问音乐库的两种方法, (只能访问音频文件,如music,podcast,audiobook等) 2.MPMusicPlayerController的使用 有两种播放器可以选择,一种是app.........
    ▪ TableLayout设立行与行之间的分割线       TableLayout设置行与行之间的分割线 <View android:layout_height="1dip" android:background="#B7B7B7" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" .........

[1]HashMap的两种遍历模式
    来源: 互联网  发布时间: 2014-02-18
HashMap的两种遍历方式

第一种: 

Map map = new HashMap(); 
Iterator iter = map.entrySet().iterator(); 
while (iter.hasNext()) { 
    Map.Entry entry = (Map.Entry) iter.next(); 
    Object key = entry.getKey(); 
    Object val = entry.getValue(); 
} 
 
效率高,以后一定要使用此种方式! 
第二种: 
Map map = new HashMap(); 
Iterator iter = map.keySet().iterator(); 
while (iter.hasNext()) { 
    Object key = iter.next(); 
    Object val = map.get(key); 
} 
 
效率低,以后尽量少使用! 

例: 
HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例: 

public class HashMapTest { 
public static void main(String[] args) ...{ 
  HashMap hashmap = new HashMap(); 
  for (int i = 0; i < 1000; i ) ...{ 
   hashmap.put("" i, "thanks"); 
  } 

  long bs = Calendar.getInstance().getTimeInMillis(); 
  Iterator iterator = hashmap.keySet().iterator();   
  while (iterator.hasNext()) ...{    
   System.out.print(hashmap.get(iterator.next())); 
  } 
  System.out.println(); 
  System.out.println(Calendar.getInstance().getTimeInMillis() - bs); 
  listHashMap(); 
} 

  public static void listHashMap() ...{ 
  java.util.HashMap hashmap = new java.util.HashMap(); 
  for (int i = 0; i < 1000; i ) ...{ 
   hashmap.put("" i, "thanks"); 
  } 
  long bs = Calendar.getInstance().getTimeInMillis();   
  java.util.Iterator it = hashmap.entrySet().iterator(); 
  while (it.hasNext()) ...{ 
   java.util.Map.Entry entry = (java.util.Map.Entry) it.next(); 
   // entry.getKey() 返回与此项对应的键 
   // entry.getValue() 返回与此项对应的值 
   System.out.print(entry.getValue()); 
  } 
  System.out.println(); 
  System.out.println(Calendar.getInstance().getTimeInMillis() - bs); 
} 
} 
 

对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。 

    
[2] 访问iPod Library及MPMusicPlayerController的一些小结
    来源: 互联网  发布时间: 2014-02-18
访问iPod Library及MPMusicPlayerController的一些总结

 

 

1.访问音乐库的两种方法,

(只能访问音频文件,如music,podcast,audiobook等)

2.MPMusicPlayerController的使用

有两种播放器可以选择,一种是application music player,另外一种是iPod music player。

第一种播放器是一种内部播放器,当程序对出后停止播放;而第二种播放器则与iPod播放器内的信息相关,退出之后不会停止播放。获取方式如下:

  • + applicationMusicPlayer
  • + iPodMusicPlayer

播放之前需要设置播放器的播放队列

  • – setQueueWithQuery:
  • – setQueueWithItemCollection:

管理播放模式和播放状态的一些属性

  •   currentPlaybackTime  property
  •   nowPlayingItem  property
  •   playbackState  property
  •   repeatMode  property
  •   shuffleMode  property
  •   volume  property

播放状态 MPMusicPlaybackState

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum {
 
   MPMusicPlaybackStateStopped,
 
   MPMusicPlaybackStatePlaying,
 
   MPMusicPlaybackStatePaused,
 
   MPMusicPlaybackStateInterrupted,
 
   MPMusicPlaybackStateSeekingForward,
 
   MPMusicPlaybackStateSeekingBackward
 
};
 
typedef NSInteger MPMusicPlaybackState;

 

播放控制方法

  • – play
  • – pause
  • – stop
  • – beginSeekingForward
  • – beginSeekingBackward
  • – endSeeking
  • – skipToNextItem
  • – skipToBeginning
  • – skipToPreviousItem

播放状态发生变化时可以发送通知

  • – beginGeneratingPlaybackNotifications
  • – endGeneratingPlaybackNotifications

MPMusicPlayerControllerPlaybackStateDidChangeNotification

可以通过该通知来改变播放按钮的样式

MPMusicPlayerControllerNowPlayingItemDidChangeNotification

MPMusicPlayerControllerVolumeDidChangeNotification

具体步骤

1.注册和开始发送通知

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Listing 2-1  Registering for and activating music player notifications
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  
[notificationCenter
    addObserver: self
    selector:    @selector (handle_NowPlayingItemChanged:)
    name:        MPMusicPlayerControllerNowPlayingItemDidChangeNotification
    object:      musicPlayer];
  
[notificationCenter
    addObserver: self
    selector:    @selector (handle_PlaybackStateChanged:)
    name:        MPMusicPlayerControllerPlaybackStateDidChangeNotification
    object:      musicPlayer];
  
[musicPlayer beginGeneratingPlaybackNotifications];
1
2
3
4
5
6
7
8
9
10
11
12
Listing 2-2  Unregistering and deactivating music player notifications
[[NSNotificationCenter defaultCenter]
    removeObserver: self
    name:           MPMusicPlayerControllerNowPlayingItemDidChangeNotification
    object:         musicPlayer];
  
[[NSNotificationCenter defaultCenter]
    removeObserver: self
    name:           MPMusicPlayerControllerPlaybackStateDidChangeNotification
    object:         musicPlayer];
  
[musicPlayer endGeneratingPlaybackNotifications];
2.创建并配置一个Music Player 

 

 

1
2
3
4
5
6
Listing 2-3  Creating an application music player
MPMusicPlayerController* appMusicPlayer =
    [MPMusicPlayerController applicationMusicPlayer];
  
[appMusicPlayer setShuffleMode: MPMusicShuffleModeOff];
[appMusicPlayer setRepeatMode: MPMusicRepeatModeNone];
1
2
3
4
5
6
7
8
Listing 2-4  Creating an iPod music player
MPMusicPlayerController* iPodMusicPlayer =
    [MPMusicPlayerController iPodMusicPlayer];
  
if ([iPodMusicPlayer nowPlayingItem]) {
    // Update the UI (artwork, song name, volume indicator, etc.)
    //        to reflect the iPod state
}
3.设置播放队列

 

 

  • – setQueueWithQuery:
  • – setQueueWithItemCollection:
4.控制播放

3.MPMediaPickerController的使用
1
2
3
4
5
6
7
8
9
10
- (IBAction)addSongsToMusicPlayer:(id)sender
{
    MPMediaPickerController *mpController = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    mpController.delegate = self;
    mpController.prompt = @"Add songs to play";
    mpController.allowsPickingMultipleItems = YES;
     
    [self presentModalViewController:mpController animated:YES];
    [mpController release];
}

主要是设置代理和选择多媒体类型,然后通过代理方法来获取选中的歌曲

 

1
2
3
4
5
6
7
8
9
10
11
12
#pragma mark - Media Picker Delegate Methods
 
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];
    [self dismissModalViewControllerAnimated:YES];
}
 
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
    [self dismissModalViewControllerAnimated:YES];
}

 

4.MPMediaItem
用此方法来获取item的metadata
1
- (id) valueForProperty: (NSString *) property

NSString *const MPMediaItemPropertyTitle;                   

NSString *const MPMediaItemPropertyAlbumTitle;              

NSString *const MPMediaItemPropertyArtist;                 

 

 

5.MPMediaItemCollection
collection是一组有序的item集合,可用同样的方法来获取collection的metadata
1
- (id) valueForProperty: (NSString *) property
创建
  • + collectionWithItems:
  • – initWithItems:

属性

  •   items  property
  •   representativeItem  property
  •   count  property
  •   mediaTypes  property
6.MPMediaPlaylist
1
2
3
4
5
6
7
8
9
10
11
12
13
MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playlists = [myPlaylistsQuery collections];
  
for (MPMediaPlaylist *playlist in playlists) {
    NSLog (@"%@", [playlist valueForProperty: MPMediaPlaylistPropertyName]);
  
    NSArray *songs = [playlist items];
    for (MPMediaItem *song in songs) {
        NSString *songTitle =
            [song valueForProperty: MPMediaItemPropertyTitle];
        NSLog (@"\t\t%@", songTitle);
    }
}
7.MPMediaQuery

需要设置两个属性: filter  and  grouping type

filter描述查询内容,grouping type 描述返回内容的排列方式

查询可以获取items,也可以获取collections

  • When you ask for items, the query returns a collection containing all the items that match the filter. The items are in “natural” order, meaning that they are ordered as iTunes shows them on the desktop.
  • When you ask for collections, the media query employs not only its filter but also its grouping type.
获取全部歌曲
1
2
3
4
5
6
7
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSLog(@"Logging items from a generic query...");
NSArray *itemsFromGenericQuery = [everything items];
for (MPMediaItem *song in itemsFromGenericQuery) {
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
    NSLog (@"%@", songTitle);
}
获取名为“Happy the Clown”的艺术家的歌曲
1
2
3
4
5
6
7
8
MPMediaPropertyPredicate *artistNamePredicate =
    [MPMediaPropertyPredicate predicateWithValue: @"Happy the Clown"
                                     forProperty: MPMediaItemPropertyArtist];
  
MPMediaQuery *myArtistQuery = [[MPMediaQuery alloc] init];
[myArtistQuery addFilterPredicate: artistNamePredicate];
  
NSArray *itemsFromArtistQuery = [myArtistQuery items];
多个查找条件,查找名为"Sad the Joker"的艺术家的"Stair Tumbling"专辑
1
2
3
4
5
6
7
8
9
10
11
12
MPMediaPropertyPredicate *artistNamePredicate =
    [MPMediaPropertyPredicate predicateWithValue: @"Sad the Joker"
                                     forProperty: MPMediaItemPropertyArtist];
  
MPMediaPropertyPredicate *albumNamePredicate =
    [MPMediaPropertyPredicate predicateWithValue: @"Stair Tumbling"
                                     forProperty: MPMediaItemPropertyAlbumTitle];
  
MPMediaQuery *myComplexQuery = [[MPMediaQuery alloc] init];
  
[myComplexQuery addFilterPredicate: artistNamePredicate];
[myComplexQuery addFilterPredicate: albumNamePredicate];
1
2
3
4
5
6
Listing 4-4  Applying multiple predicates when initializing a media query
NSSet *predicates =
    [NSSet setWithObjects: artistNamePredicate, albumNamePredicate, nil];
  
MPMediaQuery *specificQuery =
    [[MPMediaQuery alloc] initWithFilterPredicates: predicates];
1
2
3
4
5
6
7
Listing 4-5  Testing if a property key can be used for a media property predicate
if ([MPMediaItem canFilterByProperty: MPMediaItemPropertyGenre]) {
    MPMediaPropertyPredicate *rockPredicate =
        [MPMediaPropertyPredicate predicateWithValue: @"Rock"
                                         forProperty: MPMediaItemPropertyGenre];
    [query addFilterPredicate: rockPredicate];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Listing 4-6  Using grouping type to specify media item collections
MPMediaQuery *query = [[MPMediaQuery alloc] init];
  
[query addFilterPredicate: [MPMediaPropertyPredicate
                               predicateWithValue: @"Moribund the Squirrel"
                                      forProperty: MPMediaItemPropertyArtist]];
// Sets the grouping type for the media query
[query setGroupingType: MPMediaGroupingAlbum];
  
NSArray *albums = [query collections];
for (MPMediaItemCollection *album in albums) {
    MPMediaItem *representativeItem = [album representativeItem];
    NSString *artistName =
        [representativeItem valueForProperty: MPMediaItemPropertyArtist];
    NSString *albumName =
        [representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];
    NSLog (@"%@ by %@", albumName, artistName);
  
    NSArray *songs = [album items];
    for (MPMediaItem *song in songs) {
        NSString *songTitle =
            [song valueForProperty: MPMediaItemPropertyTitle];
        NSLog (@"\t\t%@", songTitle);
    }
}
query的一些简便构造方法

专辑封面的使用
1
2
3
4
5
6
7
8
9
10
Listing 4-7  Displaying album artwork for a media item
MPMediaItemArtwork *artwork =
    [mediaItem valueForProperty: MPMediaItemPropertyArtwork];
UIImage *artworkImage =
    [artwork imageWithSize: albumImageView.bounds.size];
if (artworkImage) {
    albumImageView.image = artworkImage;
} else {
    albumImageView.image = [UIImage imageNamed: @"noArtwork.png"];
}




    
[3] TableLayout设立行与行之间的分割线
    来源: 互联网  发布时间: 2014-02-18
TableLayout设置行与行之间的分割线
<View
            android:layout_height="1dip"
            android:background="#B7B7B7" />


<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/list_bg_border" >

        <TableRow android:padding="10dp" >

            <me.mcar.parking.control.AutoAjustSizeTextView
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/parkdetail_rushHours_text" />

            <TextView
                android:id="@+id/parkdetail_rushHours"
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <View
            android:layout_height="1dip"
            android:background="#B7B7B7" />

        <TableRow android:padding="10dp" >

            <TextView
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/parkdetail_amount_text" />

            <TextView
                android:id="@+id/parkdetail_amount"
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <View
            android:layout_height="1dip"
            android:background="#B7B7B7" />

        <TableRow android:padding="10dp" >

            <TextView
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/parkdetail_price_text" />

            <TextView
                android:id="@+id/parkdetail_price"
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="10"
                android:ellipsize="end"
                android:singleLine="true"
                android:width="150dp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:contentDescription="@string/parkdetail_price_detail"
                android:src="/blog_article/@drawable/detail_btn/index.html" />
        </TableRow>
    </TableLayout>


    
最新技术文章:
▪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