《2048》开发2——自定义布局

新建GameView类使GameView继承自GridLayout,建立构造方法

public class GameView extends GridLayout {
   public GameView(Context context,AttributeSet attrs, int defStyle) {
                   super(context,attrs, defStyle);
                   //TODO Auto-generated constructor stub
                   initGameView();//初始化类的方法,作为入口方法
         }
         publicGameView(Context context, AttributeSet attrs) {
                   super(context,attrs);
                    //TODO Auto-generated constructor stub
                   initGameView();//初始化类的方法
         }
         publicGameView(Context context) {
                   super(context);
                   //TODO Auto-generated constructor stub
                   initGameView();//初始化类的方法
         }

为了保证不出错,将所有带有参数的构造方法都写上,防止出错。在方法中调用initGameView();方法,用来初始化类。

并且,将这个自定义的布局放入到布局文件中。

<com.Lemon.game2048.GameView
             android:layout_width="fill_parent"
             android:layout_height="0dp"
             android:layout_weight="1"
             android:id="@+id/gameView">
</com.Lemon.game2048.GameView>
 

接下来就是定义初始化类方法initGameView();

private void initGameView(){
                   setColumnCount(4);//设定GridLayout是4列的
                   setBackgroundColor(0xffbbada0);//设定背景颜色
                  
                   //新建触摸事件
                   setOnTouchListener(newView.OnTouchListener() {
                            privatefloat startX,startY,offsetX,offsetY;//定义手指刚触碰和离开的坐标
                            @Override
                            publicboolean onTouch(View v, MotionEvent event) {
                                     //TODO Auto-generated method stub
                                     //监听手指的意图
                                     switch(event.getAction()) {
                                     caseMotionEvent.ACTION_DOWN://手指按下时
                                               startX= event.getX();
                                               startY= event.getY();
                                               break;
                                     caseMotionEvent.ACTION_UP://手指离开时
                                               offsetX= event.getX()-startX;//得到的坐标要减去按下时的坐标
                                               offsetY= event.getY()-startY;
                                              
                                               //判断斜方向上的手势
                                               //x的绝对值要是比y的绝对值大的话,说明是在水平方向上
                                               if(Math.abs(offsetX)>Math.abs(offsetY)) {
                                                        //如果offset是负数说明是向左滑动,有误差,设置为-5
                                                        if(offsetX<-5) {
                                                                 swipeLeft();//向左滑动的方法
                                                        }elseif(offsetX>5){
                                                                 swipeRight();
                                                        }
                                               }
                                                        else{
                                                                 if(offsetY<-5) {
                                                                           swipeUp();
                                                                 }
                                                                 elseif (offsetY>5) {
                                                                           swipeDown();
                                                                 }
                                                        }
                                              
                                               break;
                                     }
                                     returntrue;//如果是false的话,无法触发onTouchDown等方法
                            }
                   });
         }

原文地址:https://www.cnblogs.com/lemonhome/p/4492634.html