首页 文章 精选 留言 我的

精选列表

搜索[网站开发],共10000篇文章
优秀的个人博客,低调大师

Android开发学习笔记:手机震动控制浅析

Android系统中Vibrator对象负责对手机震动的处理,具体的实现方法: 1.获取振动器Vibrator的实例: Vibratorvibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE); getSystemService(VIBRATOR_SERVICE):获得一个震动的服务 2.调用vibrate方法来产生震动: 只向vibrate()传递一个参数,这个参数用来指定振动的毫秒数 //震动5秒 vibrator.vibrate(5000); 向vibrate()方法传递多个参数 //等待1秒,震动2秒,等待1秒,震动3秒 long[]pattern={1000,2000,1000,3000}; //-1表示不重复,如果不是-1,比如改成1,表示从前面这个long数组的下标为1的元素开始重复. vibrator.vibrate(pattern,-1); 3.取消震动 vibrator.cancel(); 4.在AndroidManifest.xml文件添加权限 <uses-permissionandroid:name="android.permission.VIBRATE"/> 下面的实例包含了产生震动的两个方法 VibratorDemoActivity.java packagecom.lingdududu.test; importandroid.app.Activity; importandroid.os.Bundle; importandroid.os.Vibrator; importandroid.view.MotionEvent; /* *@authorlingdududu *当程序开始运行结束时,手机等待1秒就开始震动2秒,再等待1秒,开始震动3秒 *当我们点击手机屏幕,触发onTouchEvent事件,手机就开始震动1秒 */ publicclassVibratorDemoActivityextendsActivity{ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); Vibratorvibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE); long[]pattern={1000,2000,1000,3000};//OFF/ON/OFF/ON...... vibrator.vibrate(pattern,-1); } @Override publicbooleanonTouchEvent(MotionEventevent){ if(event.getAction()==MotionEvent.ACTION_MOVE){ Vibratorvibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE); vibrator.vibrate(1000); } returnsuper.onTouchEvent(event); } } 注:记得在AndroidManifest.xml文件添加权限,还有程序要在真机上运行才能有震动的效果,模拟器上不支持震动的。 PS:我正在参加IT博客大赛,欢迎大家来投我一票的 http://blog.51cto.com/contest2011/3061169 本文转自 lingdududu 51CTO博客,原文链接:http://blog.51cto.com/liangruijun/724042

优秀的个人博客,低调大师

Android开发学习笔记:Notification和NotificationManager浅析

NotificationManager 获取NotificationManager String service = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager =(NotificationManager)getSystemService(service); Notification //实例化Notification Notificationnotification=newNotification(); 3.设置Notification的属性 //设置显示图标,该图标会在状态栏显示 inticon=notification.icon=R.drawable.happy; //设置显示提示信息,该信息也在状态栏显示 StringtickerText="测试Notification"; //显示时间 longwhen=System.currentTimeMillis();notification.icon=icon; notification.tickerText=tickerText; notification.when=when; //也可以这样设置 Notificationnotification_2=newNotification(icon,tickerText,when) 调用setLatestEventInfo()方法在视图中设置图标和时间。 //实例化Intent Intentintent=newIntent(MainActivity.this,MainActivity.class); //获得PendingIntent PendingIntentpIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0); //设置事件信息 notification.setLatestEventInfo(MainActivity.this,"Title","Content",pIntent); 4.发出通知 //Notification标示ID privatestaticfinalintID=1; //发出通知 mNotificationManager.notify(ID,n); 下面是具体的例子,在这个例子里定义了一个MainActivity发出广播通知,定义一个MyReceiver类继承Broadcasts接受通知,当接收完通知之后,启动一个SecondActivity,在SecondActivity类中通过Notification和NotificationManager来可视化显示广播通知。具体的步骤如下: MainActivity.java packagecom.android.notification; importandroid.app.Activity; importandroid.content.Intent; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; publicclassMainActivityextendsActivity{ //声明Button privateButtonbtn; //定义BroadcastReceiveraction privatestaticfinalStringMY_ACTION="com.android.notification.MY_ACTION"; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //设置当前布局视图 setContentView(R.layout.main); //实例化Button btn=(Button)findViewById(R.id.Button1); //添加事件监听器 btn.setOnClickListener(listener); } //创建事件监听器 privateOnClickListenerlistener=newOnClickListener(){ @Override publicvoidonClick(Viewv){ //实例化Intent Intentintent=newIntent(); //设置Intentaction属性 intent.setAction(MY_ACTION); //发起广播 sendBroadcast(intent); } }; } MyReceiver.java packagecom.android.notification; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; publicclassMyReceiverextendsBroadcastReceiver{ @Override publicvoidonReceive(Contextcontext,Intentintent){ //实例化Intent Intenti=newIntent(); //在新的任务中启动Activity i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //设置Intent启动的组件名称 i.setClass(context,SecondActivity.class); //启动Activity显示通知 context.startActivity(i); } } SecondActivity.java packagecom.android.notification; importandroid.app.Activity; importandroid.app.Notification; importandroid.app.NotificationManager; importandroid.app.PendingIntent; importandroid.content.Intent; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; publicclassSecondActivityextendsActivity{ //声明按钮 privateButtoncancelBtn; //声明Notification privateNotificationnotification; //声明NotificationManager privateNotificationManagermNotification; //Notification标示ID privatestaticfinalintID=1; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.second); //实例化按钮 cancelBtn=(Button)findViewById(R.id.cancelButton2); //获得NotificationManager实例 Stringservice=NOTIFICATION_SERVICE; mNotification=(NotificationManager)getSystemService(service); //实例化Notification notification=newNotification(); //设置显示图标,该图标会在状态栏显示 inticon=notification.icon=android.R.drawable.stat_notify_chat; //设置显示提示信息,该信息也会在状态栏显示 StringtickerText="TestNotification"; //显示时间 longwhen=System.currentTimeMillis(); notification.icon=icon; notification.tickerText=tickerText; notification.when=when; //实例化Intent Intentintent=newIntent(this,MainActivity.class); //获得PendingIntent PendingIntentpi=PendingIntent.getActivity(this,0,intent,0); //设置事件信息 notification.setLatestEventInfo(this,"消息","HelloAndroid",pi); //发出通知 mNotification.notify(ID,notification); //为按钮添加监听器 cancelBtn.setOnClickListener(cancelListener); } //取消通知监听器 privateOnClickListenercancelListener=newOnClickListener(){ @Override publicvoidonClick(Viewv){ //取消通知 mNotification.cancel(ID); } }; } main.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="发出广播通知" android:id="@+id/Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> second.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="显示通知界面" android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="取消通知" android:id="@+id/cancelButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 在AndroidManifest.xml文件中16~21加入对receiver,SecondActivity的声明 <?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.android.notification" android:versionCode="1" android:versionName="1.0"> <uses-sdkandroid:minSdkVersion="10"/> <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> <activityandroid:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiverandroid:name="MyReceiver"> <intent-filter> <actionandroid:name="com.android.notification.MY_ACTION"/> </intent-filter> </receiver> <activityandroid:name="SecondActivity"/> </application> </manifest> 效果图: Notification丰富的提示方式: 声音提醒 ·使用默认声音 notification.defaults |= Notification.DEFAULT_SOUND; ·使用自定义声音 notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); ·注:如果定义了默认声音,那么自定义声音将被覆盖 振动提醒 ·使用默认振动 notification.defaults |= Notification.DEFAULT_VIBRATE; ·使用自定义振动 long[] vibrate = {0,100,200,300};notification.vibrate = vibrate; ·注:如果定义了默认振动,那么自定义振动将被覆盖 灯光闪烁提醒 ·使用默认闪烁 notification.defaults |= Notification.DEFAULT_LIGHTS; ·使用自定义闪烁 notification.ledARGB = 0xff00ff00;// LED灯的颜色,绿灯notification.ledOnMS = 300;// LED灯显示的毫秒数,300毫秒notification.ledOffMS = 1000;// LED灯关闭的毫秒数,1000毫秒notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志 更多特性 可以通过 Notification 的相关字段或标志(flags)为提醒设置更多的特性。 ·FLAG_AUTO_CANCEL标志:当提醒被用户点击之后会自动被取消(cancel); ·FLAG_INSISTENT标志:在用户响应之前会一直重复提醒音; ·FLAG_ONGOING_EVENT标志:Add this to theflagsfield to group the notification under the "Ongoing" title in the Notifications window. ·FLAG_NO_CLEAR标志:当在提醒栏中点击“清除提醒”按钮时,该提醒将不会被清除; ·number字段:This value indicates the current number of events represented by the notification. The appropriate number is overlaid on top of the status bar icon. If you intend to use this field, then you must start with "1" when the Notification is first created. (If you change the value from zero to anything greater during an update, the number is not shown.) ·iconLevel字段:This value indicates the current level of aLevelListDrawablethat isused for the notification icon. You can animate the icon in the status bar by changingthis value to correlate with the drawable's defined in a LevelListDrawable. ·contentView字段:To define your own layout for the expanded message, instantiate a RemoteViews object and pass it to thecontentViewfield of your Notification. Pass the PendingIntent to thecontentIntentfield. 本文转自 lingdududu 51CTO博客,原文链接: http://blog.51cto.com/liangruijun/657502

优秀的个人博客,低调大师

Android开发学习笔记:TextView的属性详解

android:autoLink :设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web /email/phone/map/all) android:autoText :如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。 android:bufferType : 指定getText()方式取得的文本类别。选项editable 类似于StringBuilder :可追加字符,也就是说getText后可调用append方法设置文本内容。spannable :则可在给定的字符区域使用样式,参见这里1、这里2。 android:capitalize :设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。 android:cursorVisible:设定光标为显示/隐藏,默认显示。 android:digits:设置允许输入哪些字符。如“1234567890.+-*/% ()” android:drawableBottom:在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。 android:drawableLeft:在text的左边输出一个drawable,如图片。 android:drawablePadding:设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。 android:drawableRight:在text的右边输出一个drawable。 android:drawableTop:在text的正上方输出一个drawable。 android:editable:设置是否可编辑。 android:editorExtras:设置文本的额外的输入数据。 android:ellipsize:设置当文字过长时,该控件该如何显示。有如下值设置:”start”—?省略号显示在开头;”end” ——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动) android:freezesText:设置保存文本的内容以及光标的位置。 android:gravity:设置文本位置,如设置成“center”,文本将居中显示。 android:hintText:为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在 EditView中使用,但是这里也可以用。 android:imeOptions:附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中再详细说明,此处无用。 android:imeActionId:设置IME动作ID。 android:imeActionLabel:设置IME动作标签。 android:includeFontPadding:设置文本是否包含顶部和底部额外空白,默认为true。 android:inputMethod:为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。 android:inputType:设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果。 android:linksClickable:设置链接是否点击连接,即使设置了autoLink。 android:marqueeRepeatLimit:在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为 marquee_forever时表示无限次。 android:ems:设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度 android:maxEms:设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:minEms:设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:maxLength:限制显示的文本长度,超出部分不显示。 android:lines:设置文本的行数,设置两行就显示两行,即使第二行没有数据。 android:maxLines:设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。 android:minLines:设置文本的最小行数,与lines类似。 android:lineSpacingExtra:设置行间距。 android:lineSpacingMultiplier:设置行间距的倍数。如”1.2” android:numeric:如果被设置,该TextView有一个数字输入法。此处无用,设置后唯一效果是TextView有点击效果,此属性在EdtiView将详细说明。 android:password:以小点”.”显示文本 android:phoneNumber:设置为电话号码的输入方式。 android:privateImeOptions:设置输入法选项,此处无用,在EditText将进一步讨论。 android:scrollHorizontally:设置文本超出TextView的宽度的情况下,是否出现横拉条。 android:selectAllOnFocus:如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。 TextView中设置后无效果。 android:shadowColor:指定文本阴影的颜色,需要与shadowRadius一起使用。 android:shadowDx:设置阴影横向坐标开始位置。 android:shadowDy:设置阴影纵向坐标开始位置。 android:shadowRadius:设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。 android:singleLine:设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行 android:text:设置显示文本. android:textAppearance:设置文字外观。如 “?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse android:textColor:设置文本颜色 android:textColorHighlight:被选中文字的底色,默认为蓝色 android:textColorHint:设置提示信息文字的颜色,默认为灰色。与hint一起使用。 android:textColorLink:文字链接的颜色. android:textScaleX:设置文字之间间隔,默认为1.0f。 android:textSize:设置文字大小,推荐度量单位”sp”,如”15sp” android:textStyle:设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开 android:typeface:设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3] android:height:设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米) android:maxHeight:设置文本区域的最大高度 android:minHeight:设置文本区域的最小高度 android:width:设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width 的区别看这里。 android:maxWidth:设置文本区域的最大宽度 android:minWidth:设置文本区域的最小宽度 附件:Android中的长度单位详解 Android中的长度单位详解(dp、sp、px、in、pt、mm)有很多人不太理解dp、sp 和px 的区别:现在这里介绍一下dp 和sp。dp 也就是dip。这个和sp 基本类似。如果设置表示长度、高度等属性时可以使用dp 或sp。但如果设置字体,需要使用sp。dp 是与密度无关,sp 除了与密度无关外,还与scale 无关。如果屏幕密度为160,这时dp 和sp 和px 是一样的。1dp=1sp=1px,但如果使用px 作单位,如果屏幕大小不变(假设还是3.2 寸),而屏幕密度变成了320。那么原来TextView 的宽度设成160px,在密度为320 的3.2 寸屏幕里看要比在密度为160 的3.2 寸屏幕上看短了一半。但如果设置成160dp 或160sp 的话。系统会自动将width 属性值设置成320px 的。也就是160 * 320 / 160。其中320 / 160 可称为密度比例因子。也就是说,如果使用dp 和sp,系统会根据屏幕密度的变化自动进行转换。 其他单位的含义 px:表示屏幕实际的象素。例如,320*480 的屏幕在横向有320个象素,在纵向有480 个象素。 in:表示英寸,是屏幕的物理尺寸。每英寸等于2.54 厘米。例如,形容手机屏幕大小,经常说,3.2(英)寸、3.5(英)寸、4(英)寸就是指这个单位。这些尺寸是屏幕的对角线长度。如果手机的屏幕是3.2 英寸,表示手机的屏幕(可视区域)对角线长度是3.2*2.54 = 8.128 厘米。读者可以去量一量自己的手机屏幕,看和实际的尺寸是否一致。 mm:表示毫米,是屏幕的物理尺寸。 pt:表示一个点,是屏幕的物理尺寸。大小为1 英寸的1/72。 本文转自 lingdududu 51CTO博客,原文链接:http://blog.51cto.com/liangruijun/627123

优秀的个人博客,低调大师

iOS开发-简单的图片查看器

现在你只要拿着手机,不管你Android还是iOS,新闻类的App不可避免都有一个功能就是图片查看,做个专题,查看一下内容,App Store中也有专门针对图片浏览的App,鉴于目前所知有限,无法做到那么高大上的App,简单的做个美女查看的Demo。没有太多的功能,上一张,下一张,标签,图片,简简单的,深刻的感觉到知识就是力量,目前知识有限的结果就是Demo简单,且学且珍惜吧。 1.新建项目(如果不会可以参考本人之前的文章),然后在StoryBoard中进行布局,将Girl文件夹中的图片拖入项目中; 2.将UIImageView,UILabel,UIButton拖入StoryBoard中,并且设置背景图片; 设置背景图片: 3.ViewController.h中定义成员变量: 1 2 3 4 5 6 7 8 9 10 #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic ) IBOutlet UIImageView *imageView; @property (weak, nonatomic ) IBOutlet UILabel *pageLabel; @end 4.上一张和下一张的事件代码: 定义一个图片数组: 1 2 3 4 5 6 7 8 9 10 11 @interface ViewController () @property NSArray *imageArr; @end @implementation ViewController - ( void )viewDidLoad { [ super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _imageArr=@[@ "girl0.jpg" ,@ "girl1.jpg" ,@ "girl2.jpg" ]; } 上一张的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 //显示上一张 - ( IBAction )preview:( id )sender { NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==1) { currentIndex=allCount; } else { currentIndex=currentIndex-1; } //设置标签 [_pageLabel setText:[ NSString stringWithFormat:@ "%ld/%ld" ,currentIndex,( long )allCount]]; //获取图片的名称 NSString *imageName=[ NSString stringWithFormat:@ "girl%ld.jpg" ,( long )currentIndex-1]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; } 下一张代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //显示下一张 - ( IBAction )nextView:( id )sender { //截取标签上面的数字 NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==allCount) { currentIndex=0; } NSString *imageName=[ NSString stringWithFormat:@ "girl%ld.jpg" ,( long )currentIndex]; [_pageLabel setText:[ NSString stringWithFormat:@ "%ld/%ld" ,currentIndex+1,( long )allCount]]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; } 以上的代码基本上都是OC基础,UIImage设置图片的时候只需要传递一下图片名称即可,不需要传递路径; 5.最终效果如下: 效果很简单,就是在上一张和下一张到临界点的时候判断一下,两者的代码类似,其实可以封装一下,周末愉快; ViewController.m中的代码: 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 // // ViewController.m // MyPicture // // Created by keso on 15/1/17. // Copyright (c) 2015年 keso. All rights reserved. // #import "ViewController.h" @interface ViewController () @property NSArray *imageArr; @end @implementation ViewController - ( void )viewDidLoad { [ super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _imageArr=@[@ "girl0.jpg" ,@ "girl1.jpg" ,@ "girl2.jpg" ]; } - ( void )didReceiveMemoryWarning { [ super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //显示上一张 - ( IBAction )preview:( id )sender { NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==1) { currentIndex=allCount; } else { currentIndex=currentIndex-1; } //设置标签 [_pageLabel setText:[ NSString stringWithFormat:@ "%ld/%ld" ,currentIndex,( long )allCount]]; //获取图片的名称 NSString *imageName=[ NSString stringWithFormat:@ "girl%ld.jpg" ,( long )currentIndex-1]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; } //显示下一张 - ( IBAction )nextView:( id )sender { //截取标签上面的数字 NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==allCount) { currentIndex=0; } NSString *imageName=[ NSString stringWithFormat:@ "girl%ld.jpg" ,( long )currentIndex]; [_pageLabel setText:[ NSString stringWithFormat:@ "%ld/%ld" ,currentIndex+1,( long )allCount]]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; } @end 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4230348.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

开发者指南-Android如何绘制View

Android画的操作是由Framework层来进行处理的,整个 draw是从根View开始的,ViewGroup向子View发出 draw的请求,然后子View负责自己重画它们的invalid区域。Drawing一个Layout必须通过两个步骤: 1. 测量。measure() 从根节点到叶子节点依次测量,这样每个View都会存有各自的dimension.在我们的程序种我们可以重写onMeasure来设置更为精确的content大小,重写完后必须调用setMeasureDimension来存储View的宽和高 。 2.布局。layout也是从父节点到子节点,父节点根据刚才传递进来的measure信息来拜访它们的子节点。 当一个视图的measure()方法返回时,它的getMeasuredWidth()和getMeasuredHeight() 值必须被设置,以及所有 这个视图子节点的值。一个view的measure的宽度和高度值必须符合父视图的限制。这确保在度量过程之后,所有父节点接受所有它们的子节点的度量值。一个父视图可能会在其子视图上多次调用measure()方法。比如,父视图可能会通过未指定的尺寸调用measure来找到它们的大小,然后使用实际数值再次调用measure(),如果所有子视图未做限制的尺寸总合过大或过小(也即是,如果子视图之间不能对各自占据的空间达成共识的话,父视图将会干预并且使用第二个过程的规则)。 View.MeasureSpec 用于子View告诉父View它们想如何被测量和怎么放置。而 LayoutParams则是描述了View的宽和高是多少。对于每一个区域可以指定如下的值: 一个准确的数值。 ·FILL_PARENT,这意味着视图想和父视图一样大(减掉填充padding)。 WRAP_CONTENT,这意味着视图只想有刚好包装其内容那么大(加上padding)。 对于不同的ViewGroup子类,有相应的LayoutParams子类。比如,相对布局RelativeLayout有它自己的LayoutParams子类,这包含了能够让子视图横向和竖向居中显示的能力。 度量规格(MeasureSpecs)被用来沿着树从父到子的下传度量需求。一个MeasureSpecs可以是下面三种模式之一: UNSPECIFIED:父视图来决定其子视图的理想尺寸。比如,一个线性布局可能在它的子视图上调用measure() on its child,通过设置其高度为UNSPECIFIED 以及一个宽度为EXACTLY 240,来找出这个子视图在给定240像素宽度的情况下需要显示多高。 EXACTLY:父视图用来给子视图强加一个准确的尺寸。子视图必须使用这个大小,并确保其所有的后代将适合这个尺寸。 AT_MOST:这被父视图用来给子视图强加一个最大尺寸。子视图必须确保它自己以及所有的后代都适合这个尺寸。 本文转自 最牛傻蛋 51CTO博客,原文链接:http://blog.51cto.com/zuiniuwang/718274,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS开发-自定义UIAlterView(iOS 7)

App中不可能少了弹框,弹框是交互的必要形式,使用起来也非常简单,不过最近需要自定义一个弹框,虽然iOS本身的弹框已经能满足大部分的需求,但是不可避免还是需要做一些自定义的工作。iOS7之前是可以自定义AlterView的,就是继承一下UIAlterView,然后初始化的时候通过addSubview添加自定义的View,但是iOS7之后这样做就不行了,不过还好有开源项目可以解决这个问题。 iOS默认弹框 viewDidLoad中添加两个按钮,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 UIButton *orignalBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, 40, 100, 50)]; [orignalBtn setBackgroundColor:[UIColor greenColor]]; [orignalBtn setTitle:@ "iOS弹框" forState:UIControlStateNormal]; [orignalBtn addTarget: self action: @selector (orignalShow) forControlEvents:UIControlEventTouchUpInside]; [ self .view addSubview:orignalBtn]; UIButton *customlBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, 140, 100, 50)]; [customlBtn setBackgroundColor:[UIColor redColor]]; [customlBtn setTitle:@ "自定义弹框" forState:UIControlStateNormal]; [customlBtn addTarget: self action: @selector (customShow) forControlEvents:UIControlEventTouchUpInside]; [ self .view addSubview:customlBtn]; 响应默认弹框事件: 1 2 3 4 -( void )orignalShow{ UIAlertView *alterView=[[UIAlertView alloc]initWithTitle:@ "提示" message:@ "博客园-Fly_Elephant" delegate: self cancelButtonTitle:@ "取消" otherButtonTitles:@ "确定" , nil ]; [alterView show]; } 效果如下: 自定义弹框 主要解决iOS7之后的系统无法自定义弹框的问题,使用开源项目,项目地址:https://github.com/wimagguc/ios-custom-alertview,其实就是自定义了一个类: CustomIOSAlertView.h 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 26 27 28 29 30 31 32 33 34 35 36 37 #import <UIKit/UIKit.h> @protocol CustomIOSAlertViewDelegate - ( void )customIOS7dialogButtonTouchUpInside:( id )alertView clickedButtonAtIndex:( NSInteger )buttonIndex; @end @interface CustomIOSAlertView : UIView<CustomIOSAlertViewDelegate> @property ( nonatomic , retain) UIView *parentView; // The parent view this 'dialog' is attached to @property ( nonatomic , retain) UIView *dialogView; // Dialog's container view @property ( nonatomic , retain) UIView *containerView; // Container within the dialog (place your ui elements here) @property ( nonatomic , assign) id <CustomIOSAlertViewDelegate> delegate; @property ( nonatomic , retain) NSArray *buttonTitles; @property ( nonatomic , assign) BOOL useMotionEffects; @property ( copy ) void (^onButtonTouchUpInside)(CustomIOSAlertView *alertView, int buttonIndex) ; - ( id )init; /*! DEPRECATED: Use the [CustomIOSAlertView init] method without passing a parent view. */ - ( id )initWithParentView: (UIView *)_parentView __attribute__ (( deprecated )); - ( void )show; - ( void )close; - ( IBAction )customIOS7dialogButtonTouchUpInside:( id )sender; - ( void )setOnButtonTouchUpInside:( void (^)(CustomIOSAlertView *alertView, int buttonIndex))onButtonTouchUpInside; - ( void )deviceOrientationDidChange: ( NSNotification *)notification; - ( void )dealloc; @end CustomIOSAlertView.m + View Code 调用代码: 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 26 27 28 29 30 31 32 33 34 -( void )customShow{ CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init]; [alertView setContainerView:[ self customView]]; [alertView setButtonTitles:[ NSMutableArray arrayWithObjects:@ "取消" , @ "确定" , nil ]]; [alertView setDelegate: self ]; [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) { NSString *result=alertView.buttonTitles[buttonIndex]; NSLog (@ "点击了%@按钮" ,result); [alertView close]; }]; [alertView setUseMotionEffects: true ]; [alertView show]; } - (UIView *)customView { UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 160)]; UILabel *tip=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 50, 30)]; [tip setText:@ "提示" ]; [customView addSubview:tip]; UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(10, 60, 210, 30)]; [content setText:@ "http://www.cnblogs.com/xiaofeixiang" ]; [content setFont:[UIFont systemFontOfSize:12]]; [customView addSubview:content]; return customView; } 效果如下: 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4453819.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android开发之旅:组件生命周期(一)

引言 应用程序组件有一个生命周期——一开始Android实例化他们响应意图,直到结束实例被销毁。在这期间,他们有时候处于激活状态,有时候处于非激活状态;对于活动,对用户有时候可见,有时候不可见。组件生命周期将讨论活动、服务、广播接收者的生命周期——包括在生命周期中他们可能的状态、通知状态改变的方法、及这些状态的组件寄宿的进程被终结和实例被销毁的可能性。 本文主要讨论活动的生命周期及他们可能的状态、通知状态改变的方法。分为以下三部分: 1、活动生命周期 2、保存活动状态 3、协调活动 1、活动生命周期 一个活动有三个基本状态: 激活状态或运行状态,这时它运行在屏幕的前台(处于当前任务活动栈的最上面)。这个活动有用户的操作的焦点。 暂停状态,这时活动失去焦点但是它对用户仍可见。也就是说,另一个活动在它的上面且那个活动是透明的或者没有覆盖整个屏幕,因此通过它可以看见暂停状态的活动。一个暂停的活动完全是活着的(它维护着所有的状态和成员信息,且仍然依附在窗口管理器),但是当内存极小时可以被系统杀掉。 停止状态,这时活动完全被其他活动掩盖。它仍然保留所有状态和成员信息,但是对用户它不可见,因此它的窗口时隐藏的且当其他地方需要内存时它往往被系统杀掉。 如果一个活动被暂停或停止,系统可以将它从内存移除,通过要求它结束(通过调用它的finish()方法),或简单地杀掉它的进程。当它再次显示给用户时,必须要完全重新启动和恢复到之前的状态。随着活动从一个状态转为另一个状态,通过调用下面的受保护的方法通知该改变: void onCreate(BundlesaveInstanceState) void onStart() void onRestart() void onResume() void onPause() void onStop() void onDestroy() 所有这些方法都是钩子,你可以重写当状态改变时做适当的工作。所有的活动必须要实现onCreate()去做一些初始化的设置,当对象第一次实例化的时候。很多活动也会实现onPause()去提交数据修改或准备停止与用户交互。 将他们合并在一起,这七个方法定义了活动的整个生命周期。有三个嵌套的循环,你可以通过这七个方法监视: 活动的整个生命时间,从第一次调用onCreate()开始直到调用onDestroy()结束。一个活动在onCreate()中做所有的“全局”状态的初始设置,在onDestroy()中释放所有保留的资源。举例来说,有一个线程运行在后台从网络上下载数据,它可能会在onCreate()中创建线程,在onDestroy()中结束线程。 活动的可视生命时间,从调用onStart()到相应的调用onStop()。在这期间,用户可以在屏幕上看见活动,虽然它可能不是运行在前台且与用户交互。在这两个方法之间,你可以保持显示活动所需要的资源。举例来说,你可以在onStart()中注册一个广播接收者监视影响你的UI的改变,在onStop()中注销。因为活动在可视和隐藏之间来回切换,onStart()和onStop()可以调用多次。 活动的前台生命时间,从调用onResume()到相应的调用onPause()。在这期间,频繁地在重用和暂停状态转换——例如,当设备进入睡眠状态或一个新的活动启动时调用onPause(),当一个活动返回或一个新的意图被传输时调用onResume()。因此,这两个方法的代码应当是相当轻量级的。 下面这个图解释了这三个循环和状态之间状态的可能路径。着色的椭圆表示活动的主要状态,矩形表示当活动在状态之间转换时你可以执行的回调方法。 图1、活动生命周期(来源:Android SDK) 下面的表格对每个方法更详细的描述和在活动的整个生命周期中的定位。 注意上面表格的Killable列,它表示当方法返回时没有执行活动的其它代码,系统是否能杀死活动寄宿的进程。三个方法(onPause()、onStop()、onDestroy())标记为Yes。因为onPause()是唯一一个保证在进程被杀之前会调用的,因此你应该使用onPause()来写任何持久化存储数据。 被标记为No的方法保护活动寄宿的进程在他们调用的时候不会被杀掉。因此活动是可杀掉状态,例如onPause()返回到onResume()调用期间。直到onPause()再次返回,活动是不可杀掉的。其实,没有标记为Killable的活动也是可以系统被杀掉的,不过这仅仅发生在极端困难的情况下,没有有任何其他资源可用。 2、保存活动状态 当系统而不是用户关闭一个活动来节省内存时,用户可能希望返回到活动且是它之前的状态。为了获得活动被杀之前的状态,你可以执行活动的onSaveInstanceState()方法。Android在活动容易被销毁前调用这个方法,也就是调用onPause()之前。该方法的参数是一个Bundle对象,在这个对象你可以以名值对记录活动的动态状态。当活动再次启动时,Bundle同时被传递到onCreate()和调用onCreate()之后的方法,onRestoreInstanceState(),因此使用一个或两个可以重新创建捕获的状态。 因为onSaveInstanceState()方法不总是被调用,你应该仅使用onSaveInstanceState()它来记录活动的临时状态,而不是持久的数据。应该使用onPause()来存储持久数据。 3、协调活动 当一个活动启动另一个活动,他们都经历生命周期转换。一个暂停或许是停止,然而另一个启动。有时,你可能需要协调这些活动。生命周期的回调顺序是明确界定的,特别是当这两个活动在同一个进程中: 当前活动的onPause()方法被调用。 接下来,启动活动的onCreate()、onStart()、onResume()方法按序被调用。 然后,如果获得不再在屏幕上可见,它的onStop()方法被调用。 修正:第一节——活动生命周期中的表格第一、二行中的第二列(description)中的onRestart()应该为onStart()。 1000多的点击,居然没有人发现这个错误,看了大家根本就没认真看或者说根本没有静心看园子里的任何博文,心痛!还是水帖比较受欢迎。 本文转自Saylor87 51CTO博客,原文链接:http://blog.51cto.com/skynet/365375,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册