android菜鸡提升之路---实现一个积木拼图游戏
废话不多说,先上图:
这个是开始时候的模样(讲道理有点幼稚,因为这个是 苹果软件商店的一款游戏,懒得反编译,所以图片都是自己扣的)
这个是拼好以后的模样,同时有语音提示(“太棒啦”)哈哈哈,很搞笑
当然 我只是模仿一下,同时弥补一下自己还不清晰的知识,按照我的做法是很简单的。下面就是我的整体思路:
1. 当然是去截一波图了,没办法,不会画
2.就是实现把 中间整体的白色的“人”放在中间 同时“人”的其它部位放在四周 这里就用 了imageview
3.就是让控件随着你的手指移动
4.移动到一定位置的时候需要自动对齐一下,比如说你在移动手的时候,不需要很精确到达白色“人”手的位置,到达附近的时候就可以自己对齐了,这样整个游戏玩起来流畅一些
5.最后就是当你全部移动好了 ,出现三颗星星 说一声“你真棒”
开始实现思路:
1.截图反正就是PS抠图,这个不是重点。
2.这里用了 layoutparams,在代码中实现对控件位置的动态赋值(对每一个imageview都做一下)
layoutParams = new RelativeLayout.LayoutParams(161,176);//设置图片大小 layoutParams.leftMargin=200;//与画布左边距离 layoutParams.topMargin = 200;//与画布顶部距离 _lian.setLayoutParams(layoutParams);//绑定到对应LayoutParams _lian.setOnTouchListener(this);//设置监听事件
3.这个就是重写一下onTouch函数 具体代码下面给出
4.判断一下 你当前的位置,距离目标位置绝对值相差50px(x坐标,y坐标都要)以内的时候,直接把图片放到目标位置
5.给个flag,每个部位成功了,flag++;直到全部成功,输出声音。
下面是代码,很简单,而且代码哟注释有,有不懂可以评论。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root" tools:context=".move_activity" ><ImageView android:id="@+id/id_shen_ti" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/shen_ti" android:layout_centerInParent="true" /><ImageView android:id="@+id/id_lian" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/lian" android:layout_centerInParent="true" /><ImageView android:id="@+id/id_mao_zi" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/mao_zi" android:layout_centerInParent="true"/><ImageView android:id="@+id/id_you_shou" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/you_shou" android:layout_centerInParent="true" /><ImageView android:id="@+id/id_you_tui" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/you_tui" android:layout_centerInParent="true" /><ImageView android:id="@+id/id_zuo_shou" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/zuo_shou" android:layout_centerInParent="true" /><ImageView android:id="@+id/id_zuo_tui" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/zuo_tui" android:layout_centerInParent="true" /><ImageView android:id="@+id/id_yi_fu" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/yi_fu" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="get" android:text="get" android:layout_alignParentBottom="true"/> </RelativeLayout>
package com.example.admin.yi_mi_yang_guang;import android.content.Intent; import android.media.Image; import android.os.Build; import android.support.constraint.ConstraintLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout;public class move_activity extends AppCompatActivity implements View.OnTouchListener{ImageView _lian,_shen_ti,_mao_zi,_yi_fu,_you_tui,_you_shou,_zuo_tui,_zuo_shou;ViewGroup _root;private int lastX, lastY;// final static int IMAGE_SIZE = 72; RelativeLayout.LayoutParams layoutParams;RelativeLayout.LayoutParams layoutParams2;RelativeLayout.LayoutParams layoutParams3;RelativeLayout.LayoutParams layoutParams4;RelativeLayout.LayoutParams layoutParams5;RelativeLayout.LayoutParams layoutParams6;RelativeLayout.LayoutParams layoutParams7;RelativeLayout.LayoutParams layoutParams8;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_move_activity);getWindow().getDecorView().setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {@Override public void onSystemUiVisibilityChange(int visibility) {if (visibility == View.SYSTEM_UI_FLAG_VISIBLE) {onWindowFocusChanged(true);}}});_root = (ViewGroup) findViewById(R.id.root); //绑定图片 _lian = (ImageView) findViewById(R.id.id_lian);_shen_ti = (ImageView) findViewById(R.id.id_shen_ti);_mao_zi = (ImageView) findViewById(R.id.id_mao_zi);_yi_fu = (ImageView) findViewById(R.id.id_yi_fu);_you_tui = (ImageView) findViewById(R.id.id_you_tui);_you_shou = (ImageView) findViewById(R.id.id_you_shou);_zuo_tui = (ImageView) findViewById(R.id.id_zuo_tui);_zuo_shou = (ImageView) findViewById(R.id.id_zuo_shou);layoutParams = new RelativeLayout.LayoutParams(161,176);//设置图片大小 layoutParams.leftMargin=200;//与画布左边距离 layoutParams.topMargin = 200;//与画布顶部距离 _lian.setLayoutParams(layoutParams);//绑定到对应LayoutParams _lian.setOnTouchListener(this);//设置监听事件 layoutParams2 = new RelativeLayout.LayoutParams(360, 619);layoutParams2.leftMargin = 400;layoutParams2.topMargin = 400;_shen_ti.setLayoutParams(layoutParams2);_shen_ti.setOnTouchListener(this);layoutParams3 = new RelativeLayout.LayoutParams(185, 106);layoutParams3.leftMargin = 200;layoutParams3.topMargin = 600;_mao_zi.setLayoutParams(layoutParams3);_mao_zi.setOnTouchListener(this);layoutParams4 = new RelativeLayout.LayoutParams(207, 216);layoutParams4.leftMargin = 400;layoutParams4.topMargin = 200;_yi_fu.setLayoutParams(layoutParams4);_yi_fu.setOnTouchListener(this);layoutParams5 = new RelativeLayout.LayoutParams(121, 204);layoutParams5.leftMargin = 400;layoutParams5.topMargin = 600;_you_tui.setLayoutParams(layoutParams5);_you_tui.setOnTouchListener(this);layoutParams6 = new RelativeLayout.LayoutParams(122, 228);layoutParams6.leftMargin = 600;layoutParams6.topMargin = 200;_you_shou.setLayoutParams(layoutParams6);_you_shou.setOnTouchListener(this);layoutParams7 = new RelativeLayout.LayoutParams(115, 215);layoutParams7.leftMargin = 600;layoutParams7.topMargin = 400;_zuo_tui.setLayoutParams(layoutParams7);_zuo_tui.setOnTouchListener(this);layoutParams8 = new RelativeLayout.LayoutParams(119, 230);layoutParams8.leftMargin = 800;layoutParams8.topMargin = 400;_zuo_shou.setLayoutParams(layoutParams8);_zuo_shou.setOnTouchListener(this);}public boolean onTouch(View view, MotionEvent event) {final int X = (int) event.getRawX();final int Y = (int) event.getRawY();switch (event.getAction() & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:lastX = (int) event.getRawX();lastY = (int) event.getRawY();/* RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view .getLayoutParams();*/ break;case MotionEvent.ACTION_UP:break;case MotionEvent.ACTION_POINTER_DOWN:break;case MotionEvent.ACTION_POINTER_UP:break;case MotionEvent.ACTION_MOVE:int dx = (int) event.getRawX() - lastX;int dy = (int) event.getRawY() - lastY;int left = view.getLeft() + dx;int top = view.getTop() + dy;RelativeLayout.LayoutParams layoutParams_ = (RelativeLayout.LayoutParams) view.getLayoutParams();if(layoutParams_==layoutParams) {layoutParams_.leftMargin = left;layoutParams_.topMargin = top;if (left >= 447 && left <= 547 && top >= 387 && top <= 487) {layoutParams_.leftMargin = 497;layoutParams_.topMargin = 437;}view.setLayoutParams(layoutParams_);lastX = (int) event.getRawX();lastY = (int) event.getRawY();} //自动对齐 lian if(layoutParams_==layoutParams3) {layoutParams_.leftMargin = left;layoutParams_.topMargin = top;if(left>=484-50&&left<=484+50&&top>=400-50&&top<=400+50) {layoutParams_.leftMargin = 484;layoutParams_.topMargin = 400;}view.setLayoutParams(layoutParams_);lastX = (int) event.getRawX();lastY = (int) event.getRawY();}//自动对齐 mao_zi if(layoutParams_==layoutParams4) {layoutParams_.leftMargin = left;layoutParams_.topMargin = top;if(left>=478-50&&left<=478+50&&top>=603-50&&top<=603+50) {layoutParams_.leftMargin = 478;layoutParams_.topMargin = 603;}view.setLayoutParams(layoutParams_);lastX = (int) event.getRawX();lastY = (int) event.getRawY();}//自动对齐 yi_fu if(layoutParams_==layoutParams5) {layoutParams_.leftMargin = left;layoutParams_.topMargin = top;if(left>=571-50&&left<=571+50&&top>=820-50&&top<=820+50) {layoutParams_.leftMargin = 571;layoutParams_.topMargin = 820;}view.setLayoutParams(layoutParams_);lastX = (int) event.getRawX();lastY = (int) event.getRawY();}//自动对齐 you_tui if(layoutParams_==layoutParams6) {layoutParams_.leftMargin = left;layoutParams_.topMargin = top;if(left>=636-50&&left<=636+50&&top>=623-50&&top<=623+50) {layoutParams_.leftMargin = 636;layoutParams_.topMargin = 623;}view.setLayoutParams(layoutParams_);lastX = (int) event.getRawX();lastY = (int) event.getRawY();}//自动对齐 you_shou if(layoutParams_==layoutParams7) {layoutParams_.leftMargin = left;layoutParams_.topMargin = top;if(left>=472-50&&left<=472+50&&top>=820-50&&top<=820+50) {layoutParams_.leftMargin = 472;layoutParams_.topMargin = 820;}view.setLayoutParams(layoutParams_);lastX = (int) event.getRawX();lastY = (int) event.getRawY();}//自动对齐 zuo_tui if(layoutParams_==layoutParams8) {layoutParams_.leftMargin = left;layoutParams_.topMargin = top;if(left>=400-50&&left<=400+50&&top>=616-50&&top<=616+50) {layoutParams_.leftMargin = 400;layoutParams_.topMargin = 616;}view.setLayoutParams(layoutParams_);lastX = (int) event.getRawX();lastY = (int) event.getRawY();}//自动对齐 zuo_tui break;}_root.invalidate();return true;}public void get(View v){Intent intent = new Intent(move_activity.this,shou_ye_activity.class);startActivity(intent);}@Override //沉浸式,去掉状态栏,去掉 public void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);if (hasFocus && Build.VERSION.SDK_INT >= 19) {View decorView = getWindow().getDecorView();decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}} }
东西很简单,没什么技术含量,就是一个小东西,记录一下!
android菜鸡提升之路---实现一个积木拼图游戏相关推荐
- Android菜鸡的提升之路---自闭症儿童游戏的实现
最近花了几天时间写一个帮助自闭症儿童的小项目,项目主要包括4个小游戏,实现的逻辑不难,没太多技术含量,但是有一些小知识点,相当于汇总一下. 写在最上面:当你的项目需要加载很多图片的时候,千万不要在xm ...
- Android菜鸡的苦逼求职路
转载请注明出处. 本文主要包含以下几部分内容: 写在前面 准备工作 面试进行时 总结 写在最后 ##1.写在前面 本人2014年10月底以菜鸡的水平进入薯片公司手机团队做Android 应用开发.20 ...
- Android菜鸡脉脉求助:4年开发经验,深圳不到15K,我该怎么办?
近日逛脉脉,看到这样一则帖子,引起了我的关注.楼主4年开发经验,月薪不到15K,满满的无力感! 有网友吐槽: 有后选后,无后选前,无后无前,算法也甜,条件允许,无脑后端,前途无量,预定高管,其次前端, ...
- 裸奔的Android菜鸡之找工作见闻杂想
15年7月份毕业到现在将近两年,毕业之后去了一家半国企半私企的外包单位做Android开发,当初大四找工作也没想那么多,只要觉得公司还行就行了,于是乎便在这家单位呆了1年多的时间. 在这一年多的时间里 ...
- Android案例手册 - 实现一个华容道拼图游戏
往期文章分享 点击跳转=><导航贴>- Unity手册,系统实战学习 点击跳转=><导航贴>- Android手册,重温移动开发
- 一个菜鸡的ACM之路
花絮 做为一个大三计软狗,那么菜,打完19年的区域赛,也没时间打20年的区域赛了吧.这个学期,是我的第一次,也是最后一次,打CCPC和ICPC,忙前忙后,共打了3场:打完后,又去赶各种死亡DDL:DD ...
- Android面试:一个三非渣本的Android校招秋招之路,附答案
前不久听我一个字节的朋友说了一个神转折的故事. 一名大专生,异常执着地向他们公司投简历,屡战屡败,屡败屡战,前前后后向字节跳动投了九次简历. 你猜后面怎么着?还真让他成功了,第九次居然拿到了offer ...
- 【实习之路】投了109份简历,幸运的大二菜鸡终于拿到欢聚时代的offer了!!
文章目录 前言 正文 面试过程 笔试情况 一轮面试: 二轮面试: 三轮面试: 发放offer 总结 前言 我是一名普通3本(本地叫2本,外地叫3本)的大二(准大三)学生,学校是一个民办独立院校,软件工 ...
- 一个一年工作经验的菜鸡程序员的一年总结
坎坷并且顺利的一年 前言 一.为什么会干这一行? 二.入职 1.被大佬照顾的小白 2.被浪潮拍死的弄潮儿 3.重新出世的大菜鸡 回顾以前展望未来 前言 刚开始准备投入这个行业是2020年8月的时候,当 ...
最新文章
- Error creating bean with name 'messageSource' defi
- Java 逆序存放4位数
- 计算机教育杂志社投稿送样刊,山东教育杂志社投稿期刊论文征稿发表-陶润杂志网...
- linux convert 添加文字,Linux convert命令有什么用
- java Ant build.xml
- [ZJOI2010]网络扩容[网络流24题]
- Java 8 Friday:让我们弃用那些旧版库
- tc溜溜865手机投屏卡_溜溜 TC Games 官网_专题
- 前端系统化学习【JS篇】:(一)浏览器简述
- java was datasource_mybatis默认的数据源连接池(PooledDataSource和UnPooledDataSource)
- WWW2022 | 知识提示的预训练微调
- linux下tpcc测试mysql_tpcc_mysql性能测试
- 黑客帝国代码雨以及其灵活使用——道家真言!!(附带相关知识点)
- mysql数据库修复工具 innodb表数据恢复 ibd文件恢复工具
- 王珞丹用手写传递情感 字体入库笔尖上的中国
- 图的表示(Adjacency List + Adjacency Matrix)
- 企业大数据价值最大化的关键因素——内存多维数据库
- 送你给你最爱的人,表白神器!!!
- 计算机论文致谢词范文500字,论文致谢词范文500字(精选6篇)
- GPS跟踪载波环matlab代码,GPS接收机载波跟踪环路解决方案