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。
[color=red]paramTest.leftMargin = -width +1;[/color]:这句很关键,不加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);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值