首先,要实现的效果图如下,点击注册按钮后,进行登录表单0~90度旋转,然后注册表单-90度旋转至0度,最终实现两者的切换,但是遇到了一点问题,记录下来,以后忘记了做个参考。
使用LinearLayout 包裹着EditText,发现很多奇怪的现象
1.LinearLayout 对象 afterView.setVisibility(View.GONE);
2.然后切换回第二块LinearLayout 布局
发现原来的1步的LinearLayout 的子View EditText控件,依然会占用地方,焦点点击软键盘可以弹出,可是输入的内容,是显示在已经隐藏了的1步
LinearLayout 中的EditText输入框中的
经过一番思考和查找资料,发现,这个问题得这么解决
---------------/**切换显示**/
afterView.setVisibility(View.VISIBLE);
for ( int i=0 ; i<afterView.getChildCount() ; i++ ) {
/**让viewgroup隐藏还不行,子view还是占用了地方,
* 点击焦点还是会选中,得一口气把所以子view
* 包括 viewgroup 都隐藏掉**/
afterView.getChildAt(i).setVisibility(View.VISIBLE);
}
currentView.setVisibility(View.GONE);
for ( int i=0 ; i<currentView.getChildCount() ; i++ ) {
/**让viewgroup隐藏还不行,子view还是占用了地方,
* 点击焦点还是会选中,得一口气把所以子view
* 包括 viewgroup 都隐藏掉**/
currentView.getChildAt(i).setVisibility(View.GONE);
}
参考login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white_pure"
android:orientation="vertical">
<!-- padding 内边距 layout_margin 外边距
android:layout_alignParentTop 布局的位置是否处于顶部 -->
<include layout="@layout/top_banner_bar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@color/main_more_black"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/icon_person"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/form_dividing_line"/>
<LinearLayout
android:id="@+id/main_info_container"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/form_item_height"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minEms="6"
android:text="账号"
android:textColor="@color/form_tip_font_color"
android:textSize="@dimen/form_tip_font_size"/>
<EditText
android:id="@+id/login_account"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="start|center_vertical"
android:hint="请输入登录账户"
android:textColorHint="@color/form_tip_input"
android:textSize="@dimen/form_tip_font_size"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/form_dividing_line"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/form_item_height"
android:layout_marginLeft="10dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minEms="6"
android:text="密码"
android:textColor="@color/form_tip_font_color"
android:textSize="@dimen/form_tip_font_size"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="start|center_vertical"
android:hint="请输入登录密码"
android:inputType="textPassword"
android:textColorHint="@color/form_tip_input"
android:textSize="@dimen/form_tip_font_size"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/form_dividing_line"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/form_item_height"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<Button
android:id="@+id/login_bt"
android:layout_width="fill_parent"
android:layout_height="@dimen/form_item_height"
android:layout_weight="1"
android:background="@drawable/radius_shape_submit_bt"
android:gravity="center_vertical|center_horizontal"
android:text="登录"
android:textColor="#ffffff"
android:textSize="@dimen/form_tip_font_size"/>
</LinearLayout>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:layout_height="50dp"
>
<TextView
android:id="@+id/register_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/login_register_link"
android:textColor="#888"
android:textColorLink="#FF0066CC"
/>
<ImageView
android:id="@+id/miniTwitter_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="25dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="25dp"
android:src="@null"
/>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@android:color/transparent"/>
</LinearLayout>
<LinearLayout
android:visibility="gone"
android:id="@+id/main_info_container2"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/form_item_height"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minEms="6"
android:text="账号"
android:textColor="@color/form_tip_font_color"
android:textSize="@dimen/form_tip_font_size"/>
<EditText
android:id="@+id/register_accout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="start|center_vertical"
android:hint="请输入登录账户"
android:textColorHint="@color/form_tip_input"
android:textSize="@dimen/form_tip_font_size"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/form_dividing_line"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/form_item_height"
android:layout_marginLeft="10dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minEms="6"
android:text="密码"
android:textColor="@color/form_tip_font_color"
android:textSize="@dimen/form_tip_font_size"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="start|center_vertical"
android:hint="请输入登录密码"
android:inputType="textPassword"
android:textColorHint="@color/form_tip_input"
android:textSize="@dimen/form_tip_font_size"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/form_dividing_line"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/form_item_height"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<Button
android:id="@+id/register_bt"
android:layout_width="fill_parent"
android:layout_height="@dimen/form_item_height"
android:layout_weight="1"
android:background="@drawable/radius_shape_submit_bt"
android:gravity="center_vertical|center_horizontal"
android:text="注册"
android:textColor="#ffffff"
android:textSize="@dimen/form_tip_font_size"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@android:color/transparent"/>
</LinearLayout>
</LinearLayout>
参考的登录Activity
package com.lvche.lvchedingdang.activity.account;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.TextView;
import com.lvche.lvcheappframelib.frame.utils.TranslatingUtil;
import com.lvche.lvchedingdang.R;
import com.lvche.lvchedingdang.activity.BaseActivity;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.ViewById;
@Fullscreen
@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity {
/**登录面板**/
@ViewById
ViewGroup main_info_container;
/**注册面板**/
@ViewById
ViewGroup main_info_container2;
@ViewById
TextView translate;
@ViewById
TextView title;
@ViewById
EditText login_account;
@ViewById
EditText register_accout;
int width_,height_;
private static final int durationTime = 300;
private static final String[] titleText = new String[]{"登录","注册"};
@Click
void translate(){
if (main_info_container.getVisibility() == View.VISIBLE)
showToggerViewAnimation(main_info_container,main_info_container2);
else
showToggerViewAnimation(main_info_container2,main_info_container);
}
/**切换显示**/
void showToggerViewAnimation(final ViewGroup currentView, final ViewGroup afterView) {
int width =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
currentView.measure(width,height);
int height_ = currentView.getMeasuredHeight();
height_ = TranslatingUtil.Dp2Px(this, height_);
width_ = currentView.getMeasuredWidth();
//width_ = TranslatingUtil.Dp2Px(this, width_ / 2.0f);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width_ = TranslatingUtil.Px2Dp(this,dm.widthPixels);
Rotate3dAnimation anim1 = new Rotate3dAnimation(0,90,width_,0,0,false);
anim1.setDuration(durationTime);
anim1.setFillAfter(true);
anim1.setInterpolator(new AccelerateInterpolator());
// 设置监听
//anim.setAnimationListener(new DisplayNextView());
anim1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (main_info_container.getVisibility() == View.VISIBLE) {
//当前登录可见,替换为注册可见
title.setText(titleText[1]);
translate.setText(titleText[0]);
/*register_accout.requestFocus();
register_accout.setFocusable(true);*/
} else {
title.setText(titleText[0]);
translate.setText(titleText[1]);
/*login_account.requestFocus();
login_account.setFocusable(true);*/
}
/**切换显示**/
afterView.setVisibility(View.VISIBLE);
for ( int i=0 ; i<afterView.getChildCount() ; i++ ) {
/**让viewgroup隐藏还不行,子view还是占用了地方,
* 点击焦点还是会选中,得一口气把所以子view
* 包括 viewgroup 都隐藏掉**/
afterView.getChildAt(i).setVisibility(View.VISIBLE);
}
currentView.setVisibility(View.GONE);
for ( int i=0 ; i<currentView.getChildCount() ; i++ ) {
/**让viewgroup隐藏还不行,子view还是占用了地方,
* 点击焦点还是会选中,得一口气把所以子view
* 包括 viewgroup 都隐藏掉**/
currentView.getChildAt(i).setVisibility(View.GONE);
}
Rotate3dAnimation anim2 = new Rotate3dAnimation(-90, 0, width_, 0, 0, false);
anim2.setDuration(durationTime);
anim2.setFillAfter(true);
anim2.setInterpolator(new LinearInterpolator());
//main_info_container2.setAnimation(anim2);
afterView.startAnimation(anim2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
currentView.startAnimation(anim1);
//main_info_container.setAnimation(anim1);
}
@Click
void longin() {
}
@Click
void register_link() {
//RegisterActivity_.intent(this).start();
this.translate();
}
public void toGoBack(View v) {
super.finish();
}
}
工具3D动画旋转类 -- Rotate3dAnimation.java
package com.lvche.lvchedingdang.activity.account;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
class Rotate3dAnimation extends Animation {
// 开始角度
private final float mFromDegrees;
// 结束角度
private final float mToDegrees;
// 中心点
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
// 是否需要扭曲
private final boolean mReverse;
// 摄像头
private Camera mCamera;
public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX,
float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
// 生成Transformation
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
// 生成中间角度
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
// 取得变换后的矩阵
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}