包含RelativeLayout的安卓移动浏览 [英] android moving View containing RelativeLayout

查看:96
本文介绍了包含RelativeLayout的安卓移动浏览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想创建一个简单的拼字游戏般的棋盘游戏。我已经创建了RelativeLayout的含我局各领域的XML文件。现在我要完成的任务是让这款主板活动 - 让用户可以放大它,缩小和移动在屏幕上。要做到这一点我使用的提示从<一个href="http://stackoverflow.com/questions/9398057/android-move-a-view-on-touch-move-action-move">HERE.我TextView的替代用自己的简单的BoardView:

 公共类BoardView扩展视图{

        公共BoardView(上下文的背景下){
            超(上下文);
            的setContentView(R.layout.board);
        }

    }
 

不幸的是它不工作...我局不动。 请大家帮帮我吧。 也许你有完全不同的方法对这种游戏?我很乐意听到这个消息。

我的BoardActivity code:

 公共类BoardActivity扩展活动实现OnTouchListener {

私人诠释deltaX_;
私人诠释deltaY_;

私人的ViewGroup root_;
私人BoardView view_;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_board);

    root_ =(ViewGroup中)findViewById(R.id.rootBoard);

    view_ =新BoardView(本);

    RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(540,540);
    layoutParams.leftMargin = 50;
    layoutParams.topMargin = 50;
    layoutParams.bottomMargin = -2500;
    layoutParams.rightMargin = -2500;
    view_.setLayoutParams(的LayoutParams);

    view_.setOnTouchListener(本);
    root_.addView(view_);
}

公共布尔onTouch(视图V,MotionEvent事件){

        最终诠释X =(INT)event.getRawX();
        最终诠释Y =(INT)event.getRawY();
        开关(event.getAction()及MotionEvent.ACTION_MASK){
        案例MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
            deltaX_ = X  -  lParams.leftMargin;
            deltaY_ = Y  -  lParams.topMargin;
            打破;
        案例MotionEvent.ACTION_UP:
            打破;
        案例MotionEvent.ACTION_POINTER_DOWN:
            打破;
        案例MotionEvent.ACTION_POINTER_UP:
            打破;
        案例MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams的LayoutParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
            layoutParams.leftMargin = X  -  deltaX_;
            layoutParams.topMargin = Y  -  deltaY_;
            layoutParams.rightMargin = -2500;
            layoutParams.bottomMargin = -2500;
            view_.setLayoutParams(的LayoutParams);
            打破;
        }
        返回true;
    }
}
 

我的board.xml布局类同<一个href="http://staticallytyped.word$p$pss.com/2011/01/26/android-avoiding-custom-views-with-resources/"相对=nofollow>这:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;
  &LT; ImageView的
    机器人:背景=@可绘制/ special_square
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / square_a1/&GT;
  &LT; ImageView的
    机器人:layout_toRightOf =@ ID / square_a1
    机器人:背景=@可绘制/正常
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / square_a2/&GT;
   ...等等...
 

解决方案

 案例MotionEvent.ACTION_MOVE:
        RelativeLayout.LayoutParams的LayoutParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
        layoutParams.leftMargin = X  -  deltaX_;
        layoutParams.topMargin = Y  -  deltaY_;
        layoutParams.rightMargin = -2500;
        layoutParams.bottomMargin = -2500;
        //没有需要重新setLayout的:view_.setLayoutParams(的LayoutParams);

        // requestLayout是必需的
        view_.requestLayout();
 


I want to create a simple scrabble-like board game. I've already created an XML file with RelativeLayout containing all fields of my board. Now what I want to accomplish is to have this board movable - so that user can zoom it in, zoom out and move over the screen. To make this happen I used tips from HERE. I replaced TextView with my own simple BoardView:

public class BoardView extends View {

        public BoardView(Context context) {
            super(context);
            setContentView(R.layout.board);
        }

    }

Unfortunately it doesn't work... My Board doesn't move. Please help me with it. Maybe you have completely different approach to this kind of game? I'd love to hear about it.

My BoardActivity code:

public class BoardActivity extends Activity implements OnTouchListener {

private int deltaX_;
private int deltaY_;

private ViewGroup root_;
private BoardView view_;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_board);

    root_ = (ViewGroup) findViewById(R.id.rootBoard);

    view_ = new BoardView(this);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(540, 540);
    layoutParams.leftMargin = 50;
    layoutParams.topMargin = 50;
    layoutParams.bottomMargin = -2500;
    layoutParams.rightMargin = -2500;
    view_.setLayoutParams(layoutParams);

    view_.setOnTouchListener(this);
    root_.addView(view_);
}

public boolean onTouch(View v, MotionEvent event) {

        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
            deltaX_ = X - lParams.leftMargin;
            deltaY_ = Y - lParams.topMargin;
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
            layoutParams.leftMargin = X - deltaX_;
            layoutParams.topMargin = Y - deltaY_;
            layoutParams.rightMargin = -2500;
            layoutParams.bottomMargin = -2500;
            view_.setLayoutParams(layoutParams);
            break;
        }
        return true;
    }
}

My board.xml layout is similiar to THIS:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
  <ImageView
    android:background="@drawable/special_square"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/square_a1" />
  <ImageView
    android:layout_toRightOf="@id/square_a1"
    android:background="@drawable/normal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/square_a2" />
   ...and so on...

解决方案

    case MotionEvent.ACTION_MOVE:
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
        layoutParams.leftMargin = X - deltaX_;
        layoutParams.topMargin = Y - deltaY_;
        layoutParams.rightMargin = -2500;
        layoutParams.bottomMargin = -2500;
        //no needs to setLayout again: view_.setLayoutParams(layoutParams);

        //requestLayout is required
        view_.requestLayout();

这篇关于包含RelativeLayout的安卓移动浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆