anim实现左边切入效果

思路:

用1个View盖住(半透明),另外一个目标layout做移动使用

注:

查看了一下F4,还有这几个能用:

AlphaAnimation渐变透明

ScaleAnimation尺寸伸缩?(用这个可能更好一点)

TranslateAnimation位置移动

RotateAnimation旋转?

AnimationSet这个厉害哦

<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/backGroundView"
        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>
    	<View
   	    android:id="@+id/rightBackGroundView"
   	    android:layout_width="20dp"
   	    android:layout_height="match_parent"
   	    android:layout_alignParentRight="true"
   	    android:layout_alignParentTop="true"
   	    android:layout_marginRight="0dp"
   	    android:background="@color/transparent" />
</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);
int widthRight =(int)(dm.widthPixels *0.2 -1);
View rightBackGroundView = findViewById(R.id.rightBackGroundView);
RelativeLayout.LayoutParams p1 = (RelativeLayout.LayoutParams)rightBackGroundView.getLayoutParams();
p1.width = widthRight;
rightBackGroundView.setLayoutParams(p1);


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);
	
}

相关推荐