右弹出无动画特效和有特效2种
效果图:
无动画: 瞬间弹出 有动画:时长:500ms
1 无动画
思路:
主布局中有水平排列2个布局,用权重表示,weight=1,width=0dp,其中右侧的默认隐藏,
点击“显示侧拉”,右侧显示,在点击,右侧消失
布局:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#33666666"
android:text="显示侧拉"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:dividerHeight="5dp" >
</ListView>
<LinearLayout
android:id="@+id/layout_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0084ff"
android:visibility="gone" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(new LeftAdapter(this));
layout_right = (LinearLayout) findViewById(R.id.layout_right);
tv_title = (TextView) findViewById(R.id.tv_title);
//没有动画效果
tv_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isOpen) {
layout_right.setVisibility(View.GONE);
isOpen = false;
} else {
layout_right.setVisibility(View.VISIBLE);
isOpen = true;
}
}
});
}
源码;http://download.csdn.net/detail/ss1168805219/9492492
有动画
1 导入nineoldandroids-2.4.0.jar,
2 listView和LinearLayout都要设置valueAnimator
布局
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#33666666"
android:gravity="center"
android:text="显示侧拉" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="5dp" >
</ListView>
<LinearLayout
android:id="@+id/layout_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0084ff" >
<TextView
android:id="@+id/tv_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="textView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
主要代码:
screenWidth = getWindowManager().getDefaultDisplay().getWidth();//获取屏幕的宽
tv_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isOpen) {//设置ValueAnimator值的变化范围
animator =ValueAnimator.ofInt(screenWidth/2,0);
animator2 =ValueAnimator.ofInt(screenWidth/2,screenWidth);
isOpen = false;
} else {
animator =ValueAnimator.ofInt(0,screenWidth/2);
animator2 =ValueAnimator.ofInt(screenWidth,screenWidth/2);
isOpen = true;
}
//listView的动画
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int width = (Integer) valueAnimator.getAnimatedValue();//获取变化中的值
layout_right.getLayoutParams().width = width;
layout_right.requestLayout();//让width重新生效
}
});
animator.setDuration(500).start();//开启动画
//有侧拉栏的动画
animator2.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int width = (Integer) valueAnimator.getAnimatedValue();
listView.getLayoutParams().width = width;
listView.requestLayout();
}
});
animator2.setDuration(500).start();
}
});
源码:http://download.csdn.net/detail/ss1168805219/9492511
其它
ViewHelper.setScaleX(holder.itemView, 0.3f);
ViewHelper.setScaleY(holder.itemView, 0.3f);
ViewPropertyAnimator.animate(holder.itemView).scaleXBy(0.7f)//by:比上一步的值增加了几倍 ,原来是0.5,增加0.5,还是1
.scaleYBy(0.7f)
// .scaleX(1.0f)//还原正常大小
// .scaleY(1.0f)//
.setInterpolator(new OvershootInterpolator())//默认也是这个
.setDuration(500).start();