当前位置: 编程技术>移动开发
本页文章导读:
▪处置Gallery控件滑动过快 处理Gallery控件滑动过快
多张大图片浏览,用了Gallery控件,发现一划好几张就过去了。
这个利用网上搜来的方法,重新定义Gallery,覆盖其onFling方法
@Override
public boolean onFling(MotionEvent e1.........
▪ 摘引“UITableVIew” 引用“UITableVIew”
-、建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTable setDelegate:self]; [DataTable setDataSource:self]; [self.view addSubview:DataTable]; [DataTable release];二、.........
▪ 经过搜索获得音乐文件和相应LRC文件的绝对路径 通过搜索获得音乐文件和相应LRC文件的绝对路径
// 请求服务器获取xml文件
public String getMusicXML(String musicName, String singerName) {
try {
musicName = URLEncoder.encode(musicName,"gbk");
singerName = URLEncod.........
[1]处置Gallery控件滑动过快
来源: 互联网 发布时间: 2014-02-18
处理Gallery控件滑动过快
多张大图片浏览,用了Gallery控件,发现一划好几张就过去了。
这个利用网上搜来的方法,重新定义Gallery,覆盖其onFling方法
@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // velocityX = velocityX / 3; // Log.i("SlowGallery", "onFling = velocityX = " + velocityX); // return false; int keyCode; if (isScrollingLeft(e1, e2)) { keyCode = KeyEvent.KEYCODE_DPAD_LEFT; } else { keyCode = KeyEvent.KEYCODE_DPAD_RIGHT; } onKeyDown(keyCode, null); return true; } private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) { return e2.getX() > e1.getX(); }
第二个问题,后来发现虽然用了那个方法,但滑向下一个的时候会出现更下一个item,设置了
mGallery.setCallbackDuringFling(false);
就可以解决了。
这个方法API上面是这么说的:
Whether or not to callback on any getOnItemSelectedListener() while the items are being flinged. If false, only the final selected item will cause the callback. If true, all items between the first and the final will cause callbacks.
不过我试了试,发现callbacks不是网上说的onItemSelected方法,不知道到底是不触发item的哪个方法
反正就是不过出现更下一个了
p.s.现在还有个问题,就是滑动的时候手不松开,我写的Gallery就动来动去的,但系统和快图就一点都不动,不知道为神马?
[2] 摘引“UITableVIew”
来源: 互联网 发布时间: 2014-02-18
引用“UITableVIew”
-、建立 UITableView
DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
[DataTable setDelegate:self];
[DataTable setDataSource:self];
[self.view addSubview:DataTable];
[DataTable release];
二、UITableView各Method说明
//Section总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return TitleData;
}
// Section Titles
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"";
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
cell.imageView.image=image;//未选cell时的图片
cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
cell.text=//.....
return cell;
}
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}
//改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
//定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}
//判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
}
//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}
//返回Section标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
三、在UITableViewCell上建立UILable多行显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
[Datalabel setTag:100];
Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:Datalabel];
[Datalabel release];
}
UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
[Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//选中cell时的颜色
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
//cell右边按钮格式
typedef enum {
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
UITableViewCellAccessoryCheckmark // checkmark. doesn't track
} UITableViewCellAccessoryType
//是否加换行线
typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle
//改变换行线颜色
tableView.separatorColor = [UIColor blueColor];
-、建立 UITableView
DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
[DataTable setDelegate:self];
[DataTable setDataSource:self];
[self.view addSubview:DataTable];
[DataTable release];
二、UITableView各Method说明
//Section总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return TitleData;
}
// Section Titles
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"";
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
cell.imageView.image=image;//未选cell时的图片
cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
cell.text=//.....
return cell;
}
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}
//改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
//定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}
//判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
}
//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}
//返回Section标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
三、在UITableViewCell上建立UILable多行显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
[Datalabel setTag:100];
Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:Datalabel];
[Datalabel release];
}
UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
[Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//选中cell时的颜色
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
//cell右边按钮格式
typedef enum {
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
UITableViewCellAccessoryCheckmark // checkmark. doesn't track
} UITableViewCellAccessoryType
//是否加换行线
typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle
//改变换行线颜色
tableView.separatorColor = [UIColor blueColor];
[3] 经过搜索获得音乐文件和相应LRC文件的绝对路径
来源: 互联网 发布时间: 2014-02-18
通过搜索获得音乐文件和相应LRC文件的绝对路径
获得的xml文件如下:
解析xml文件,获得String musicPath String lrcPath:
得到的musicPath = "http://zhangmenshiting.baidu.com/data2/music/5219955/5219955.mp3?xcode=de853ab52489ef65d21c0ed7334abdcd"
lrcPaht = "http://box.zhangmen.baidu.com/bdlrc/147/14706.lrc"
其中musicPaht会改变,lrcPath不变
// 请求服务器获取xml文件 public String getMusicXML(String musicName, String singerName) { try { musicName = URLEncoder.encode(musicName,"gbk"); singerName = URLEncoder.encode(singerName,"gbk"); } catch (UnsupportedEncodingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } String strUrl = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=" + musicName + "$$" + singerName + "$$$$"; Log.e("url", strUrl); try { url = new URL(/blog_article/strUrl/index.html); } catch (Exception e1) { e1.printStackTrace(); } BufferedReader br = null; String s; try { InputStreamReader in = new InputStreamReader(url.openStream()); Log.e("the encode is ", in.getEncoding()); br = new BufferedReader(in); } catch (IOException e1) { Log.e("tag", "br is null"); } try { while ((s = br.readLine()) != null) { sb.append(s + "\r\n"); br.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.e("sb=", sb.toString()); return sb.toString(); }
获得的xml文件如下:
引用
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<result>
<count>1</count>
<url>
<encode>
<![CDATA[
http://zhangmenshiting.baidu.com/data2/music/8453675/aWZoZ2ttbGaeomZzrZmmnJZva2RlmGluYWtsaWuYanBoY5ZrZ2mYb2pnlWpsbJtsaWVZoZ6adGhfamdpaGpuaGdpbGZqb244
]]>
</encode>
<decode>
<![CDATA[
8453675.mp3?xcode=800b2609956b3871c723a795b676d483&mid=0.84534706792597
]]>
</decode>
<type>8</type>
<lrcid>14706</lrcid>
<flag>1</flag>
</url>
<durl>
<encode>
<![CDATA[
http://zhangmenshiting2.baidu.com/data2/music/7339015/aGVmbWVnbGaeomZzrZmmnJZva2RlmGluYWtsaWuYanBoY5ZrZ2mYb2pnlWpsbJtsaWVZoZ6adGhfamdpaGpuaGdpbGZqb244
]]>
</encode>
<decode>
<![CDATA[
7339015.mp3?xcode=800b2609956b3871c723a795b676d483&mid=0.84534706792597
]]>
</decode>
<type>8</type>
<lrcid>14706</lrcid>
<flag>1</flag>
</durl>
<p2p>
<hash>f87e8f12130d017ab33622e2be5a444cbf3ae24a</hash>
<url>
<![CDATA[
http://zhangmenshiting.baidu.com/data2/music/5219955/5219955.mp3?xcode=800b2609956b3871c723a795b676d483
]]>
</url>
<type>mp3</type>
<size>1857593</size>
<bitrate>64</bitrate>
</p2p>
</result>
<result>
<count>1</count>
<url>
<encode>
<![CDATA[
http://zhangmenshiting.baidu.com/data2/music/8453675/aWZoZ2ttbGaeomZzrZmmnJZva2RlmGluYWtsaWuYanBoY5ZrZ2mYb2pnlWpsbJtsaWVZoZ6adGhfamdpaGpuaGdpbGZqb244
]]>
</encode>
<decode>
<![CDATA[
8453675.mp3?xcode=800b2609956b3871c723a795b676d483&mid=0.84534706792597
]]>
</decode>
<type>8</type>
<lrcid>14706</lrcid>
<flag>1</flag>
</url>
<durl>
<encode>
<![CDATA[
http://zhangmenshiting2.baidu.com/data2/music/7339015/aGVmbWVnbGaeomZzrZmmnJZva2RlmGluYWtsaWuYanBoY5ZrZ2mYb2pnlWpsbJtsaWVZoZ6adGhfamdpaGpuaGdpbGZqb244
]]>
</encode>
<decode>
<![CDATA[
7339015.mp3?xcode=800b2609956b3871c723a795b676d483&mid=0.84534706792597
]]>
</decode>
<type>8</type>
<lrcid>14706</lrcid>
<flag>1</flag>
</durl>
<p2p>
<hash>f87e8f12130d017ab33622e2be5a444cbf3ae24a</hash>
<url>
<![CDATA[
http://zhangmenshiting.baidu.com/data2/music/5219955/5219955.mp3?xcode=800b2609956b3871c723a795b676d483
]]>
</url>
<type>mp3</type>
<size>1857593</size>
<bitrate>64</bitrate>
</p2p>
</result>
解析xml文件,获得String musicPath String lrcPath:
// 解析xml文件,得到music文件和lrc文件的地址 public String getLyricPath() { int begin = 0, end = 0, number = 0; String lrcPath = ""; begin = sb.indexOf("<lrcid>"); Log.e("test", "sb = " + sb); if (begin != -1) { end = sb.indexOf("</lrcid>", begin); lrcPath = sb.substring(begin + 7, end); number = Integer.parseInt(lrcPath); } lrcPath = "http://box.zhangmen.baidu.com/bdlrc/" + number / 100 + "/" + number + ".lrc"; Log.e("test", "lrcPath = " + lrcPath); return lrcPath; } public String getMusicPath(){ int begin = 0, end = 0; String musicPath = ""; begin = sb.indexOf("<p2p>"); if(begin != -1){ end = sb.indexOf("</p2p>", begin); musicPath = sb.substring(begin, end + 5); } begin = musicPath.indexOf("http://"); if (begin != -1) { end = musicPath.indexOf("]]>"); musicPath = musicPath.substring(begin, end); } Log.e("test", "musicPath = " + musicPath); return musicPath; }
得到的musicPath = "http://zhangmenshiting.baidu.com/data2/music/5219955/5219955.mp3?xcode=de853ab52489ef65d21c0ed7334abdcd"
lrcPaht = "http://box.zhangmen.baidu.com/bdlrc/147/14706.lrc"
其中musicPaht会改变,lrcPath不变
最新技术文章: