app开发者平台在数字化时代的重要性与发展趋势解析
669
2022-11-17
Andorid Studio 制作欢乐写数字(Timer启动+帧动画)
做的一个小游戏,让儿童照着屏幕上的根据笔顺来写数字 0-9
学会了一点细节还有实现帧动画效果,将比较好的代码放一放
首先是实现启动应用后出现启动界面2s后向主界面跳转:
实现代码如下:
Timer timer = new Timer(); //用于设置启动界面显示的时间 TimerTask timerTask = new TimerTask() { @Override public void run() { startActivity(new Intent(StartActivity.this,MainActivity.class)); finish(); } }; timer.schedule(timerTask,2000); //设置两秒后跳转到主界面
private void initView() { iv_frame = findViewById(R.id.iv_frame); linearLayout = findViewById(R.id.linearLayout1); LinearLayout write_layout = findViewById(R.id.LinearLayout_number); write_layout.setBackgroundResource(R.drawable.bg1); //获取屏幕的高度和宽度 widthPixels = this.getResources().getDisplayMetrics().widthPixels; heightPixels = this.getResources().getDisplayMetrics().heightPixels; //图片是按照1280*720准备的,要适应于其他的分辨率的屏幕 scaleWidth = ((float)widthPixels/720); scaleHeight = ((float)heightPixels/1280); try{ //通过输入流打开第一张图片 InputStream is = getResources().getAssets().open("on1_1.png"); //通过使用bitmap解析第一张图片 arrdown = BitmapFactory.decodeStream(is); } catch (IOException e) { e.printStackTrace(); } //获取布局的宽高信息 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv_frame.getLayoutParams(); //获取图片缩放后宽度 layoutParams.width = (int)(arrdown.getWidth() * scaleWidth); //获取图片缩放后高度 layoutParams.height = (int)(arrdown.getHeight() * scaleHeight); iv_frame.setLayoutParams(layoutParams); lodimagep(1); } private synchronized void lodimagep(int j) { i = j; if(i < 25){ //当前图片小于25 String name = "on1_" + i; //获取图片资源id int imgid = getResources().getIdentifier(name,"drawable","com.example.msi.writenumber"); iv_frame.setBackgroundResource(imgid); i++; } if(j == 24){ //如果当前图片的位置为24 if(typedialog){ //没有对话框的情况下 dialog(); //调用书写完成对话框方法 } } }
实现代码如下:
其中R.drawable.frame1为放置所有帧的anmianimation-list
duration为帧持续的时间为0.15s即150ms
public void OnYS(View v){ if(mdiaolg == null){ mdiaolg = new mCustomProgressDialog(this,"演示中单击边缘取消",R.drawable.frame1); } mdiaolg.show(); } //继承dialog代码如下: public class mCustomProgressDialog extends ProgressDialog{ private AnimationDrawable mAnimation; //设置对话框的动画资源 private Context mContext; private ImageView mImageView; private String mLoadingTip; //设置对话框文字 private TextView mLoadingTv; //显示对话框文字 private int mResid; //资源id public mCustomProgressDialog(Context context,String content,int id) { super(context); this.mContext = context; this.mLoadingTip = content; this.mResid = id; //设置单击对话框周边是否让dialog消失,设置为True setCanceledOnTouchOutside(true); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progress_dialog); //获取布局文件中的TextView mLoadingTv = findViewById(R.id.loadingTv); mImageView = findViewById(R.id.loadingIv); if(mResid == 0){ mImageView.setBackgroundDrawable(null); }else { mImageView.setBackgroundResource(mResid); } mAnimation = (AnimationDrawable) mImageView.getBackground(); //为了防止在onCreate方法中只显示第一帧的解决方案之一 mImageView.post(new Runnable() { @Override public void run() { mAnimation.start(); } }); mLoadingTv.setText(mLoadingTip); }}
还有操作时候根据鼠标滑动来实现图片切换效果
根据实时滑动屏幕的x2,y2来判断手势位置,画完之后弹出dialog完成或者再来一次
如果在中途手离开屏幕,这是耗时操作(?),则发送message,子线程接收message进行handle将图片慢慢的撤回到第一帧
实现代码如下:
linearLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){ //获取行动方式头部 case MotionEvent.ACTION_DOWN: //手指按下事件 x1 = event.getX(); //获取手指按下的x坐标 y1 = event.getY(); //获取按下的y坐标 igvx = iv_frame.getLeft(); //获取手指按下图片的x坐标 igvy = iv_frame.getTop(); //图片的y坐标 //判断当手指按下的坐标大于图片位置的坐标时,证明手指按住移动,开始书写 if(x1 >= igvx && x1 <= igvx + (int)(arrdown.getWidth() * scaleWidth) && y1 >= igvy && y1 <= igvy + (int)(arrdown.getWidth() * scaleWidth) ){ type = 1; //开启书写 }else { type = 0; //否则关闭书写 } break; case MotionEvent.ACTION_MOVE: //手势移动中判断 igvx = iv_frame.getLeft(); igvy = iv_frame.getTop(); x2 = event.getX(); y2 = event.getY(); //下面是根据笔画以及手势做图片的处理,滑到不同位置,加载不同图片 if(type == 1){ if(x2 >= igvx && x2 <= igvx + (int)(arrdown.getWidth() * scaleWidth)){ if(y2 <= igvy +(int)(arrdown.getHeight() * scaleHeight) / 24 && y2 >= igvy) lodimagep(1); else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 2){ lodimagep(2); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 3){ lodimagep(3); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 4){ lodimagep(4); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 5){ lodimagep(5); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 6){ lodimagep(6); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 7){ lodimagep(7); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 8){ lodimagep(8); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 9){ lodimagep(9); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 10){ lodimagep(10); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 11){ lodimagep(11); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 12){ lodimagep(12); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 13){ lodimagep(13); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 14){ lodimagep(14); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 15){ lodimagep(15); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 16){ lodimagep(16); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 17){ lodimagep(17); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 18){ lodimagep(18); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 19){ lodimagep(19); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 20){ lodimagep(20); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 21){ lodimagep(21); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 22){ lodimagep(22); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 23){ lodimagep(23); } else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 24){ lodimagep(24); } } } break; case MotionEvent.ACTION_UP: //手势抬起判断 type = 0; if(touchTimer != null){ //中断计时器 touchTimer.cancel(); touchTimer = null; } touchTimer = new Timer(); touchTimer.schedule(new TimerTask() { @Override public void run() { Thread thread = new Thread(new Runnable() { //创建子线程 @Override public void run() { //创建Message用于发送消息 Message message = new Message(); message.what = 2; //Message消息为2 //发送消息给handle实现倒退显示图片 mHandler.sendMessage(message); } }); thread.start(); } },300,200); } return true; } }); } public Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 2: //当接收到手势抬起子线程消息时 jlodimage(); //调用资源图片倒退显示方法 break; default: break; } super.handleMessage(msg); } }; private void jlodimage() { if(i == 25){ }else if(i < 25){ if(i > 1){ i--; }else if(i == 1){ i =1; if(touchTimer != null){ touchTimer.cancel(); touchTimer = null; } } String name ="on1_" + i; int imgid = getResources().getIdentifier(name,"drawable", "com.example.msi.writenumber"); iv_frame.setBackgroundResource(imgid); } }
以数字1为例写完代码,因为后面的数字实在是难写…
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~