poupupWindow扩展
今天做一个从底部弹出的侧滑菜单栏,但是用了谷歌的DrawerLayout和第三方的slidingMenu居然都不可以,想了一下,既然这样,那么我们就只有用popupwindow了,但是poupupwindow又没有动画效果,我们也就只有自定义动画效果了。
先看动画的xml文件:
上面的是弹出poupuwindow的动画:popupwindow_enter_anim,文件放置在res文件下的anim文件中,如果没有该文件价,那就新建一个。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromYDelta="0"
android:toYDelta="100%p"
android:duration="2000"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0"
android:duration="2000"/>
</set>
上面的这个是退出poupupwindow时的动画:popupwindow_exit_anim.
将这两个动画文件放入res目录下的style文件中:
<!-- popupwindow 动画 -->
<style name="Animation">
<item name="android:windowEnterAnimation">@anim/popupwindow_enter_anim</item>
<item name="android:windowExitAnimation">@anim/popupwindow_exit_anim</item>
</style>
接下来就是如何去定义popupwindow:
由于我的poupupwindow是在listView中的条目当中的控件监听弹出的,所以我就不贴出完整代码了,贴出相关代码:
下面时popupwindow的布局文件:
图片长这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_margin="30dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/singer"
android:paddingLeft="10dp"
android:paddingRight="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌手:"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
<TextView
android:id="@+id/tv_songs_details_ratist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/album"
android:paddingLeft="6dp"
android:paddingRight="7dp"
android:layout_marginTop="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专辑:"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
<TextView
android:id="@+id/tv_songs_details_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/timing"
android:paddingLeft="10dp"
android:paddingRight="14dp"
android:layout_marginTop="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时长:"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
<TextView
android:id="@+id/tv_songs_details_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/love"
android:paddingLeft="10dp"
android:paddingRight="9dp"
android:layout_marginTop="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大小:"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
<TextView
android:id="@+id/tv_songs_details_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:padding="8dp"
/>
</LinearLayout>
</LinearLayout>
下面就是java逻辑代码:
监听事件点击之后弹出popupwindow:
iv_detals_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((TextView)popupwindow.findViewById(R.id.tv_songs_details_ratist)).setText(song.getArtist());
((TextView)popupwindow.findViewById(R.id.tv_songs_details_album)).setText(song.getAlbum());
((TextView)popupwindow.findViewById(R.id.tv_songs_details_time)).setText(song.getDuration());
((TextView)popupwindow.findViewById(R.id.tv_songs_details_size)).setText(song.getSize());
showPopupWindow();//显示popupwindow
notifyDataSetChanged();
}
});
popupwindow = inflater.inflate(R.layout.slidingmenu_songs_details,null);//这是上面的View
这是显示poupuwindow的代码:
private void showPopupWindow() {
WindowManager wm1 = activity.getWindowManager();
int width = wm1.getDefaultDisplay().getWidth();//屏幕宽度
PopupWindow popupWindow = new PopupWindow(popupwindow, width, LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.Animation);
// 设置点击外部区域, 自动隐藏
popupWindow.setOutsideTouchable(true); // 外部可触摸
popupWindow.setBackgroundDrawable(inflate.getResources().getDrawable(R.drawable.bg_popupwindow_details)); // 设置空的背景, 响应点击事件
popupWindow.setFocusable(true); //设置可获取焦点
// 显示在指定控件下
popupWindow.showAtLocation(iv_detals_menu,Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
}