思路:
用1个View盖住(半透明),另外一个目标layout做移动使用
class="xml" name="code">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/rightBackGroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/half_transparent" />
<LinearLayout
android:id="@+id/targetLayout"
android:layout_width="3dp"
android:layout_height="match_parent"
android:layout_marginLeft="-2dp"
android:background="@drawable/background_main"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="helloworld" />
</LinearLayout>
</RelativeLayout>
显示时的代码,其中xxx就是上面的layout。
paramTest.leftMargin = -width +1;:这句很关键,不加1不行啊
DisplayMetrics dm =new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
xxx.setVisibility(View.VISIBLE);
View targetLayout = findViewById(R.id.targetLayout);
int width =(int)(dm.widthPixels *0.8);
RelativeLayout.LayoutParams paramTest = (RelativeLayout.LayoutParams) targetLayout.getLayoutParams();
paramTest.width = width;
paramTest.leftMargin = -width +1;
targetLayout.setLayoutParams(paramTest);
TranslateAnimation anim = new TranslateAnimation(1, width, 0, 0);
anim.setDuration(300);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
findViewById(R.id.rightBackGroundView).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
hideAreaView();
}
});
}
});
targetLayout.startAnimation(anim);
隐藏代码就比较简单了,xxx知道是什么了吧:
private void hideAreaView() {
DisplayMetrics dm =new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
View areaLayout = findViewById(R.id.targetLayout);
int width =(int)(dm.widthPixels *0.8);
TranslateAnimation anim = new TranslateAnimation(width,1,0, 0);
anim.setDuration(300);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
Log.i("start","start" );
}
public void onAnimationRepeat(Animation animation) {
Log.i("onAnimationRepeat","onAnimationRepeat" );
}
public void onAnimationEnd(Animation animation) {
xxx.setVisibility(View.GONE);
findViewById(R.id.rightBackGroundView).setOnClickListener(null);
}
});
areaLayout.startAnimation(anim);
}