移动一个View
1.想要移动一个view,我们可以在这个view上(或者它的子控件上)设置一个touchListener,然后在onTouch()方法中添加移动的响应处理。
(pipView.findViewById(R.id.pip_move)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX =event.getRawX();
startY =event.getRawY();
Log.i(TAG,"PIP_ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
int movingX= (int) event.getRawX();
int movingY = (int)event.getRawY();
int left = pipWholeView.getLeft();
int right = pipWholeView.getRight();
int top = pipWholeView.getTop();
int bottom = pipWholeView.getBottom();
pipWholeView.layout(left + movingX - (int) startX, top + movingY - (int) startY,
right + movingX - (int) startX, bottom+ movingY - (int)startY);
/*pipWholeView.setLeft(left + movingX - (int) startX);
pipWholeView.setTop(top + movingY - (int) startY);
pipWholeView.setRight(right + movingX - (int) startX);
pipWholeView.setBottom(bottom + movingY - (int)startY);*/
startX = (int) event.getRawX();
startY = (int) event.getRawY();
Log.i(TAG,"PIP_MoveAction");
break;
case MotionEvent.ACTION_UP:
//sourceViewReset();
break;
}
return true;
}
});
这里的ACTION_MOVE的时候,layou()函数可以改变view的布局,但是view的实际的marigin参数并未改变,在设置pipWholeView的一个子控件的可见与不可见时,整个pipWholeView又回到了最初的位置(左上角),尽管移动时整个view是随手指移动的,后面的view.SetLeft(),setTop(),setRight(),setBottom(),和layout()函数的效果一样。
那么既能改变view的视觉效果,又能改变view的布局属性的是哪个方法呢?其实就是下面的layoutParams.setMargins(int left,int top,int right,int bottom)和view.setLayoutParams()方法。该方法设置了view的左上右下相对于父控件的距离,其实只需设置前面两个参数即可,左边距和上边距足够确定view的位置(后面的两个参数设置为0)。
首先声明一个view布局的布局参数变量,注意ViewGroup(是一个抽象类,子类有RelativeLayout,LinearLayout,GridLayout,FrameLayout等)的LayoutParams只能设置宽度和高度,它的子类的LayoutParams才可以设置子控件相对于父控件的距离。
private RelativeLayout.LayoutParams pipLayoutParams;
然后在view的构造函数中初始化该变量。
public PipWindow(View v, Context context) { super(context); this.context = context; pipWholeView = (RelativeLayout) v; pipLayoutParams = (RelativeLayout.LayoutParams) pipWholeView.getLayoutParams();
最后在手指移动的时候,添加处理代码。
(pipView.findViewById(R.id.pip_move)).setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getRawX(); startY = event.getRawY(); Log.i(TAG,"PIP_ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: int movingX = (int) event.getRawX(); int movingY = (int) event.getRawY(); int left = pipWholeView.getLeft(); int right = pipWholeView.getRight(); int top = pipWholeView.getTop(); int bottom = pipWholeView.getBottom(); int marginLeft = left + movingX - (int) startX; int marginTop = top + movingY - (int) startY; pipLayoutParams.setMargins(left + movingX - (int) startX,top + movingY - (int) startY, 0,0); pipWholeView.setLayoutParams(pipLayoutParams); startX = (int) event.getRawX(); startY = (int) event.getRawY(); Log.i(TAG,"PIP_MoveAction"); break; case MotionEvent.ACTION_UP: //sourceViewReset(); break; } return true; } });
View的缩放
缩放一个view的时候,最方便的方法是设置view的scaleX()和scaleY(),这样不仅达到视觉上的缩放,又能改变view的属性,更重要的一点是在缩放时,还可以设置view缩放的锚点(即以哪个点为中心进行缩放)具体代码如下。
public boolean onTouch(View view, MotionEvent motionEvent) { switch(motionEvent.getAction()){ case MotionEvent.ACTION_DOWN: fingerDownX = motionEvent.getRawX(); fingerDownY = motionEvent.getRawY(); break; case MotionEvent.ACTION_MOVE: fingerMoveX = motionEvent.getRawX(); fingerMoveY = motionEvent.getRawY(); moveX = fingerMoveX - fingerDownX; moveY = fingerMoveY - fingerDownY; Log.i("PipWindow","moveX = " + moveX); Log.i("PipWindow","moveY = " + moveY); Log.i("PipWindow","window width = "+pipView.getWidth()); Log.i("PipWindow","window height = "+pipView.getHeight()); break; case MotionEvent.ACTION_UP: acumulateX+=moveX; acumulateY+=moveY; if(Math.sqrt(acumulateX*acumulateX+acumulateY*acumulateY)>3.0){ pipWholeView.setPivotX(pipView.getX()); pipWholeView.setPivotY(pipView.getY()); scaleX = (acumulateX + origiScaleX*pipView.getWidth())/(origiScaleX*pipView.getWidth()); scaleY = (acumulateY + origiScaleY*pipView.getHeight())/(origiScaleY*pipView.getHeight()); origiWindowSize.x = (float)pipView.getWidth(); origiWindowSize.y = (float)pipView.getHeight(); Log.i("PipWidnow","end window width = "+origiWindowSize.x); Log.i("PipWidnow","end window height = "+origiWindowSize.y); pipWholeView.setScaleX(scaleX); pipWholeView.setScaleY(scaleY); } Log.i("Scale","scaled!"); //sourceViewReset(); break; } return true; }