首页 文章 精选 留言 我的

精选列表

搜索[android things],共10000篇文章
优秀的个人博客,低调大师

Android:postDelayed(模拟音乐播放进度模块)

postDelayed的用法: 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 public class MainActivity extends Activity implements OnClickListener { /** * 音乐播放模块 */ private ImageButton imageButton; private ProgressBar progressBar; private TextView textView; private boolean isPlay = false ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageButton = (ImageButton) findViewById(R.id.imageButton1); progressBar = (ProgressBar) findViewById(R.id.progressBar1); textView = (TextView) findViewById(R.id.textView1); imageButton.setOnClickListener( this ); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true ; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageButton1: imgClick(); break ; default : break ; } } private void imgClick() { isPlay = !isPlay; if (isPlay) //更换播放按钮图片 { imageButton.setImageResource(R.drawable.ic_pause); play(); //调用播放方法 } else { imageButton.setImageResource(R.drawable.ic_play); } } private int playTime; private Runnable action = null ; private void play() { new Thread() { public void run() { //使用postDelayed定时发送消息 action = new Runnable() // 创建action时并不执行run()方法 { @Override public void run() { if (isPlay) { playTime++; textView.setText(playTime + "" ); imageButton.postDelayed(action, 1000 ); // 回调action progressBar.setProgress(playTime); } } }; imageButton.postDelayed(action, 0 ); // 创建action后执行此方法,回调Runnable()里的run方法 }; }.start(); } } 模拟音乐播放计时和进度条: 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener { private ImageButton imageButton; private SeekBar seekBar; private TextView textView_playtime; private boolean isPlay = false ; private Chronometer mTimer; private TextView textView_lasttime; private long firstClickTime = 0 ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageButton = (ImageButton) findViewById(R.id.imageButton1); seekBar = (SeekBar) findViewById(R.id.seekBar1); textView_playtime = (TextView) findViewById(R.id.textView_playtime); textView_lasttime = (TextView) findViewById(R.id.textView_lasttime); mTimer = (Chronometer) findViewById(R.id.chronometer1); imageButton.setOnClickListener( this ); seekBar.setOnSeekBarChangeListener( this ); seekBar.setMax(lastTime()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true ; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageButton1: imgClick(); break ; default : break ; } } private void imgClick() { //用户点击间隔在1秒之内,就不响应(不再启动新线程,避免连续点击的加速问题) long secondClickTime = System.currentTimeMillis(); if (secondClickTime - firstClickTime < 1000 ) { return ; } firstClickTime = System.currentTimeMillis(); isPlay = !isPlay; if (isPlay) // 更换播放按钮图片 { imageButton.setImageResource(R.drawable.ic_play); play(); // 调用播放方法 } else { imageButton.setImageResource(R.drawable.ic_pause); } } private int playTime; private Runnable action = null ; private void play() // 播放音乐 { new Thread() { public void run() { action = new Runnable() { @Override public void run() { if (isPlay) { if (playTime > lastTime()) { prostop(); } else { textView_playtime.setText(progresstime(playTime)); seekBar.setProgress(playTime); imageButton.postDelayed(action, 1000 ); playTime += 1000 ; } } } }; imageButton.postDelayed(action, 0 ); }; }.start(); } private void prostop() //播放结束 { new Thread() { @Override public void run() { Runnable actionStop = new Runnable() { @Override public void run() { imageButton.setImageResource(R.drawable.ic_pause); isPlay = false ; playTime = 0 ; seekBar.setProgress(playTime); } }; imageButton.post(actionStop); } }.start(); } // 得到结束时间的毫秒数 private int lastTime() { String[] str = textView_lasttime.getText().toString().split( ":" ); return (Integer.parseInt(str[ 1 ]) + Integer.parseInt(str[ 0 ]) * 60 ) * 1000 ; } // 监听进度条方法 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { playTime = progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } // 将毫秒数转换为时间格式 private String progresstime( int progress) { Date date = new Date(progress); SimpleDateFormat format = new SimpleDateFormat( "mm:ss" ); return format.format(date); } } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1206266,如需转载请自行联系原作者

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

3天打造一个 android 小游戏

十月二日日~十月四日,三天(对前两天没兴趣的,可以直接下拉到第三天) 第一天:小游戏的简单准备 竟然是小游戏…当然越简单越好了… 游戏策划--- 游戏玩法: 玩家只需要把屏幕里面见到的人物全部消灭! 游戏需要的技术: 1,surfaceview 的用法. 2,游戏的线程循环. 3,坐标的居中. 素材: 人物: http://www.famitsu.com/freegame/tool/chibi/index2.html 这个网站可以帮我们生成我们需要的十二宫格人物 第二天:技术代码的实现 1,surfaceView publicclassGameViewSurfaceextendsSurfaceView{ //1,声明我们的Bitmap对象 privateBitmapbitmap; //2,声明一个holder对象 privateSurfaceHolderholder; //3,声明用于线程循环的对象 privateGameThreadgameThread; //4,声明我们的精灵,下一章节用的 privateList<Sprite>sprites=newArrayList<Sprite>(); //5,初始我们的变量 publicGameViewSurface(Contextcontext){ super(context); gameThread=newGameThread(this); holder=getHolder(); //用于调用运行的线程 holder.addCallback(newCallback(){ @Override publicvoidsurfaceDestroyed(SurfaceHolderholder){ //1退出时终止我们的run方法 booleanretry=true;gameThread.setRunning(false); //2等待线程的终止 while(retry){ try{ gameThread.join(); retry=false; }catch(InterruptedExceptione){ Log.d("sur","guale"); }} } //添加我们的绘图线程 @Override publicvoidsurfaceCreated(SurfaceHolderholder){ //1,创建精灵 createSprites(); //2,开始我们的线程 gameThread.setRunning(true);gameThread.start(); } @Override publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth, intheight){ }});} //创建精灵方法 privatevoidcreateSprites(){ sprites.add(createSprite(R.drawable.bad1));}privateSpritecreateSprite(intresouce){bitmap=BitmapFactory.decodeResource(getResources(),resouce); returnnewSprite(this,bitmap);} //绘图方法protectedvoidonDraw(Canvascanvas){ //把背景画成黑色 canvas.drawColor(Color.BLACK); //画出所有精灵 for(Spritesprite:sprites){ sprite.onDraw(canvas);}} //点了精灵消失的方法 //1,控制最后点击的时间 longlastClick; @Override publicbooleanonTouchEvent(MotionEventevent){ //当时间间隔太少不执行以下方法 if(System.currentTimeMillis()-lastClick>500){ lastClick=System.currentTimeMillis();synchronized(getHolder()){ floattX=event.getX(); floattY=event.getY(); for(inti=sprites.size()-1;i>=0;i--){ Spritesprite=sprites.get(i); if(sprite.isCollison(tX,tY)){ //精灵消失 sprites.remove(i);break; }} }} returntrue;}} 2,接着就要写我们的game循环的线程 publicclassGameThreadextendsThread{privateGameViewview; privatebooleanrunning=false;publicGameLoopThread(GameViewview){this.view=view;} publicvoidsetRunning(booleanrun){running=run;}@Override publicvoidrun(){ while(running){ Canvasc=null; try{ c=view.getHolder().lockCanvas(); synchronized(view.getHolder()){ view.onDraw(c);}}finally{ if(c!=null){view.getHolder().unlockCanvasAndPost(c);}}}}} 3,创建我们的精灵类 publicclassSprite{ //图片有四行 privatestaticfinalintBMP_ROWS=4; //图片有三列 privatestaticfinalintBMP_COLUMNS=3;privateintx=0; privateinty=0; privateintxSpeed=5; privateGameSurfaceViewgameView; privateBitmapbmp; //当前片断 privateintcurrentFrame=0; privateintwidth; privateintheight; publicSprite(GameSurfaceViewgameView,Bitmapbmp){ this.gameView=gameView; this.bmp=bmp; this.width=bmp.getWidth()/BMP_COLUMNS; this.height=bmp.getHeight()/BMP_ROWS;} privatevoidupdate(){ if(x>gameView.getWidth()-width-xSpeed){ xSpeed=-5;} if(x+xSpeed<0){ xSpeed=5;} x=x+xSpeed; currentFrame=++currentFrame%BMP_COLUMNS;} publicvoidonDraw(Canvascanvas){ update(); //处理,人物的行走 intsrcX=currentFrame*width; //对于人物的动作先不处理 intsrcY=1*height; //1,画出我们要矩形 Rectsrc=newRect(srcX,srcY,srcX+width,srcY+height); //2,画到的具体目的 Rectdst=newRect(x,y,x+width,y+height); //3,画出.. canvas.drawBitmap(bmp,src,dst,null);}publicbooleanisCollison(floatx2,floaty2){ //判断要消失的精灵位置 returnx2>x&&x2<x+width&&y2>y&&y2<y+height;} 这三个类写好以后,应该就有这样的效果 4,剩下就是让多个精灵随机出现,这个我相信就不多说了,只需要在sprite的构造方法中x,y的坐标随机化就好了,人物的动作就是改变操纵srcY的坐标. //坐标随机参考代码 x=rnd.nextInt(gameView.getWidth()-width); y=rnd.nextInt(gameView.getHeight()-height); //人物动作参考代码 intsrcY=getAnimationRow()*height; privateintgetAnimationRow(){ doubledirDouble=(Math.atan2(xSpeed,ySpeed))/(Math.PI/2)+2; intdirection=(int)Math.round(dirDouble)%BMP_ROWS; returndirection;} 这也能叫做游戏?? 完成的源代码下载 http://115.com/file/aq7kzhff# kill_them_all_by_tom.rar 第三天:思考与总结 还记得前两天做的游戏吗?那个也能配得叫做做游戏吗?没有华丽的画面.没有动听的音乐,没有动人的剧情,更没有好玩的操作,还… 当然,前两天只是做一个例子,算不上真正的游戏,那今天,我们想想什么才算得上一个游戏呢? 1,好的画面,音乐,特效 技术的发展,也同时带动了游戏画面的发展,我们现在做一款FC年代的游戏画面的游戏的话,估计,会被喷子们碰死,”这都啥年代了还做这种游戏!”(当然,也有例外的情况),这年头,大家真正愿意掏钱购买的游戏,如果,没有在第一眼给人以震撼,估计很少人会去购买,这种作品就是我们俗称的大作,例如,将要发售的战地3,而那些小游戏,只是作为无聊的时候打发时间用的,如果,有标价的话,他们宁愿玩另外一款免费的,因为,在他们看来,小游戏就应该是免费的,无论这个游戏做得多好.小游戏的画面,依然达不到值得他们购买的水平.当然,做得非常好的也会心甘情愿的掏钱购买,例如,植物大战僵尸,水果忍者,愤怒的小鸟等等,不过,要做到这种程度的话…不是可以复制的.(当然…在中国,你只能看到这些游戏是免费的…) 2,足以支持第一点的技术 一个优秀的想法,也要有足以实现的技术…不然,就像达芬奇那样…有着天才的想法,却没有相应实现的技术… 3,优秀的剧本 一个优秀的游戏,必然有一个好的剧本支撑.你说,俄罗斯方块这些没有剧本也很好玩…如果,我告诉你如果加上剧本的话就更好玩了(NDS上有一款马里奥俄罗斯方块),优秀的剧本,可以说得上游戏的灵魂. 4,人性化的操作 如果,我们做的游戏想模拟飞行类那样的操作,估计就只有那些很少一部分人能够享受其中的乐趣了.所以,我们在设计我们的游戏的时候,要针对我们面对的人群,设计出一套最人性化的操作,这点我觉得很重要. 5,能够提供给人以好玩的感受 一个游戏,最关键的是什么?对于,一般玩家来说,应该就是好玩.虽然,他们玩玩说不出为什么好玩,但是,他们就感觉好玩,然后,他们就会购买你的游戏,这点,以我目前的水平还不足以阐述清楚. 综合以上五点:我们能不能改进一下我们前两天做的游戏呢? 1,游戏的玩法,剧情, 1,玩法不变,但是,我们可以加上一点游戏背景 2,在一个晚上,你在睡觉的时候,有一只蚊子在里耳旁飞行,你忍无可忍,于是,打开灯,与蚊子进行一番搏斗. 2,设计与相应技术. 1,环境的视角设计, 1,用人的身体作为背景(有猎奇的性质) 2,用房间作为背景(普通) 2,追逐蚊子的方式 1,利用手机的重力感应, 2,利用手势 3,蚊子的死亡与攻击 1,拍的蚊子以后可以出现一滩鲜血 2,被蚊子咬到的时候,手机震动,或者屏幕变红 4,蚊子AI的设计 1,蚊子躲避, 2,蚊子咬人的位置 3,图片,音乐,特效的设计 如果,加上以上几点,那么前两天做的游戏是不是变成焕然一新呢? PS:以上提到的只是建议...没有实现... 本文转自 liam2199 博客,原文链接: http://blog.51cto.com/youxilua/772684如需转载请自行联系原作者

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

[转]Android笔记:Fragment与Activity之间的交互

在Activity中有一个FragmentRight. step1.首先,为了在Activity中能够使用FragmentRight,写了一条: 1 fragmentRight = (FragmentRight) getFragmentManager().findFragmentById(R.id.fragmentright); 当然,fragmentright.xml就是FragmentRight的布局文件了。 写了这条以后,就可以在Activity中使用FragmentRight实例的方法了 step2.然后,在FragmentRight中我有个按钮,当按它的时候,要触发一些代码。这时,要在FragmentRight中定义一个接口,和一个方法: 1 2 3 4 // Container Activity must implement this interface public interface OnMyButtonClickListener { public void onMyButtonClick(); //接口中定义一个方法 } 当然,之后要在需要的地方调用这个方法。我是在按FragmentRight上那个按钮时调用它。我的按钮就叫testButton. 慢着,接口的方法要被调用前,当然要先声明接口类型的成员变量: 1 2 3 4 5 6 7 private OnMyButtonClickListener mListener; testButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mListener.onMyButtonClick(); } }); step3.在FragmentRight的onAttach()方法中给mListener这个成员变量赋个值,不然会出讨厌的nullpointerexception哦。 1 2 3 4 5 6 7 8 9 10 11 12 13 @Override public void onAttach(Activity activity) { super .onAttach(activity); try { mListener = (OnbtnSendClickListener) activity; //这句就是赋初值了。 } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + "must implement OnbtnSendClickListener" ); //这条表示,你不在Activity里实现这个接口的话,我就要抛出异常咯。知道下一步该干嘛了吧? } } step4.在Activity中实现这个接口: ...... extends Activity implements OnMyButtonClickListener{...... 并且,重写该接口中定义的方法吧: 1 2 3 4 5 @Override public void onMyButtonClick() { ...... //在这里该干嘛干嘛呗。 } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1307721,如需转载请自行联系原作者

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

android中setClickable,setEnabled,setFocusable的含义及区别

setClickable设置为true时,表明控件可以点击,如果为false,就不能点击;“点击”适用于鼠标、键盘按键、遥控器等; 注意,setOnClickListener方法会默认把控件的setClickable设置为true。setEnabled使能控件,如果设置为false,该控件永远不会活动,不管设置为什么属性,都无效;设置为true,表明激活该控件,控件处于活动状态,处于活动状态,就能响应事件了,比如触摸、点击、按键事件等;setEnabled就相当于总开关一样,只有总开关打开了,才能使用其他事件。setFocusable使能控件获得焦点,设置为true时,并不是说立刻获得焦点,要想立刻获得焦点,得用requestFocus; 使能获得焦点,就是说具备获得焦点的机会、能力,当有焦点在控件之间移动时,控件就有这个机会、能力得到焦点。 本文转自 一点点征服 博客园博客,原文链接:http://www.cnblogs.com/ldq2016/p/6875225.html,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Nacos

Nacos

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

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

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

用户登录
用户注册