public class Main4Activity extends Activity { private TextView tv1;
private Button button1;
private RelativeLayout rlMain;
private int mHeight = 0;
int topHide;
int topShow; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4); rlMain = (RelativeLayout) findViewById(R.id.rlMain);
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setVisibility(View.GONE);
button1 = (Button) findViewById(R.id.button1); final LayoutTransition transitioner = new LayoutTransition();
transitioner.setDuration(150);
rlMain.setLayoutTransition(transitioner); mHeight = getResources().getDisplayMetrics().heightPixels
- getStatusBarHeight(this); getWindow().getDecorView().addOnLayoutChangeListener(
new View.OnLayoutChangeListener() { @Override
public void onLayoutChange(View arg0, int arg1, int arg2,
int arg3, int arg4, int arg5, int arg6, int arg7,
int arg8) { //计算显示时y坐标
topShow = mHeight - dip2px(Main4Activity.this, 70)
- getActionBar().getHeight();
//计算隐藏时y坐标
topHide = mHeight - getActionBar().getHeight(); // VISIBLE
ObjectAnimator customAppearingAnim = ObjectAnimator
.ofFloat(null, "y", topHide, topShow);
customAppearingAnim
.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim)
.getTarget();
view.setY(topShow);
}
}); // GONE
ObjectAnimator customDisappearingAnim = ObjectAnimator
.ofFloat(null, "y", topShow, topHide);
customDisappearingAnim
.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim)
.getTarget();
view.setY(topHide);
}
}); transitioner.setAnimator(LayoutTransition.APPEARING,
customAppearingAnim); transitioner.setAnimator(LayoutTransition.DISAPPEARING,
customDisappearingAnim); }
}); button1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) { if (tv1.getVisibility() == View.GONE) { tv1.setVisibility(View.VISIBLE); } else {
tv1.setVisibility(View.GONE);
} }
}); } /**
* dip转px
* @param context
* @param dipValue
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
} /**
* 获取系统状态栏高度
* @param context
* @return
*/
public int getStatusBarHeight(Context context) {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
Log.v("@@@@@@", "the status bar height is : " + statusBarHeight);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
} }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="@string/hello_world"
android:textColor="@android:color/white" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="44dp"
android:text="Button" /> <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:maxLines="10"
android:scrollbars="vertical"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" /> </RelativeLayout>