android开发 交换方向,Android - 互换控件位置:基于LayoutParams的瞬间交换与基于ObjectAnimator动画效果交换...

Android - 交换控件位置:基于LayoutParams的瞬间交换与基于ObjectAnimator动画效果交换

现需要交换两个控件(本文中是两个RelativeLayout),找到了两个方法:

1、使用LayoutParams改变两个layout的属性,即其相对关系(below等),实现位置的交换,但是并没有交换的动画效果,是“瞬间”交换。

2、使用animation交换控件位置,实现了我需要的动画效果。

如下图,交换layoutOne 与layoutTwo 。

054802790.jpg

一、首先介绍使用LayoutParams的方法。

package com.exchange;

import com.exchange.R;

import android.app.Activity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.Toast;

/*

* Exchange layout with LayoutParams

* Author : likun@stu.zzu.edu.cn

* Date: 2015/7/15

*/

public class ParamsExchangeActivity extends Activity {

private Button btnEx;

private LayoutInflater inflater;

private RelativeLayout myFirst,mySecond,layoutOne,layoutTwo;

//set controls' id , the id is random as you like , do NOT use zero

private int btnExId = 11;

private int layoutOneId = 12;

private int layoutTwoId = 13;

//exchange flag , in order to swap back and forth

private boolean TAG_firstLayoutTop;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.layout_main);

btnEx=(Button)findViewById(R.id.button_exchange);

btnEx.setOnClickListener(new BtnExOnClickListener());

inflater=getLayoutInflater();

TAG_firstLayoutTop = true;

//init layoutOne

myFirst = (RelativeLayout) inflater.inflate(

R.layout.layout_first, null).findViewById(R.id.myFirst);

layoutOne = (RelativeLayout)findViewById(R.id.LayoutOne);

layoutOne.removeAllViews();

layoutOne.addView(myFirst);

//init layoutTwo

mySecond = (RelativeLayout) inflater.inflate(

R.layout.layout_second, null).findViewById(R.id.mySecond);

layoutTwo = (RelativeLayout)findViewById(R.id.LayoutTwo);

layoutTwo.removeAllViews();

layoutTwo.addView(mySecond);

}

public class BtnExOnClickListener implements OnClickListener

{

@Override

public void onClick(View v){

Toast.makeText(getBaseContext(), "exchange!", Toast.LENGTH_SHORT).show();

//set id for controls in order to change their Params

btnEx.setId(btnExId);

layoutOne.setId(layoutOneId);

layoutTwo.setId(layoutTwoId);

RelativeLayout.LayoutParams params;

if(TAG_firstLayoutTop){

params = (RelativeLayout.LayoutParams)layoutTwo.getLayoutParams();

params.removeRule(RelativeLayout.BELOW);//remove the exist 'BELOW' rule

params.addRule(RelativeLayout.BELOW,11);//add a new one 'BELOW' rule,below control NO. 11

layoutTwo.setLayoutParams(params);

params = (RelativeLayout.LayoutParams)layoutOne.getLayoutParams();

params.removeRule(RelativeLayout.BELOW);

params.addRule(RelativeLayout.BELOW,13);//below control NO. 13

layoutOne.setLayoutParams(params);

TAG_firstLayoutTop=false;// change the flag

}else{

//vice versa

params = (RelativeLayout.LayoutParams)layoutOne.getLayoutParams();

params.removeRule(RelativeLayout.BELOW);

params.addRule(RelativeLayout.BELOW,11);

layoutOne.setLayoutParams(params);

params = (RelativeLayout.LayoutParams)layoutTwo.getLayoutParams();

params.removeRule(RelativeLayout.BELOW);

params.addRule(RelativeLayout.BELOW,12);

layoutTwo.setLayoutParams(params);

TAG_firstLayoutTop=true;

}

}

}

}

二、使用animation交换控件

使用animation交换的方法非常简单:

ObjectAnimator.ofFloat(layoutTwo, "TranslationY", -300).setDuration(1000).start();

ObjectAnimator.ofFloat(layoutOne, "TranslationY", 300).setDuration(1000).start();

全部代码如下:

package com.exchange;

import android.animation.ObjectAnimator;

import android.app.Activity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.Toast;

public class AnimExchangeActivity extends Activity {

private Button btnEx;

private LayoutInflater inflater;

private RelativeLayout myFirst,mySecond,layoutOne,layoutTwo;

private boolean TAG_firstLayoutTop;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.layout_main);

btnEx=(Button)findViewById(R.id.button_exchange);

btnEx.setOnClickListener(new BtnExOnClickListener());

inflater=getLayoutInflater();

TAG_firstLayoutTop = true;

//init layoutOne

myFirst = (RelativeLayout) inflater.inflate(

R.layout.layout_first, null).findViewById(R.id.myFirst);

layoutOne = (RelativeLayout)findViewById(R.id.LayoutOne);

layoutOne.removeAllViews();

layoutOne.addView(myFirst);

//init layoutTwo

mySecond = (RelativeLayout) inflater.inflate(

R.layout.layout_second, null).findViewById(R.id.mySecond);

layoutTwo = (RelativeLayout)findViewById(R.id.LayoutTwo);

layoutTwo.removeAllViews();

layoutTwo.addView(mySecond);

}

public class BtnExOnClickListener implements OnClickListener

{

@Override

public void onClick(View v){

Toast.makeText(getBaseContext(), "exchange!", Toast.LENGTH_SHORT).show();

if(TAG_firstLayoutTop){

//move upward and downward 300

ObjectAnimator.ofFloat(layoutTwo, "TranslationY", -300).setDuration(1000).start();

ObjectAnimator.ofFloat(layoutOne, "TranslationY", 300).setDuration(1000).start();

TAG_firstLayoutTop = false;

}else{

//back to normal position

ObjectAnimator.ofFloat(layoutOne, "TranslationY", 0).setDuration(1000).start();

ObjectAnimator.ofFloat(layoutTwo, "TranslationY", 0).setDuration(1000).start();

TAG_firstLayoutTop = true;

}

}

}

}

源代码下载传送门:稍后补链。

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值