【最讨厌那些不开源的】小米的米UI的BOTTOMVIEW源代码完美反编译
事实上原理就是WindowManager Dialog 和Animation的组合
这里分享一个Android的很经典有用并且简单方便的第三方UI控件库:BottomView(小米的米UI也用到了这个)
实现功能:
能够在底部弹出的View里自己定义布局。
能够自己定义能否够触摸外部消失。
能够自己定义事件;
能够自己定义外围背景是否透明。
能够自己定义动画;
假设须要的话。能够强制为顶部View显示
使用方法:
1、下载BottomView.jar库文件,放到Android项目project里的libs里
2、设置BottomView的Theme:
这2个Theme复制粘贴到你的项目的res/values/styles.xml里就可以
- <!--半透明背景Theme-->
- <style name="BottomViewTheme_Defalut">
- <item name="android:windowFrame">@null</item>
- <item name="android:windowContentOverlay">@null</item>
- <item name="android:windowIsFloating">true</item>
- <item name="android:windowIsTranslucent">false</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowBackground">@color/white</item>
- <item name="android:backgroundDimEnabled">true</item>
- <item name="android:windowFullscreen">true</item>
- </style>
- <!--透明背景Theme-->
- <style name="BottomViewTheme_Transparent">
- <item name="android:windowFrame">@null</item>
- <item name="android:windowIsFloating">true</item>
- <!-- Transparent -->
- <item name="android:windowIsTranslucent">false</item>
- <item name="android:windowContentOverlay">@null</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowBackground">@color/white</item>
- <item name="android:backgroundDimEnabled">false</item>
- </style>
另外假设提示
- <item name="android:windowBackground">@color/white</item>
这里的white找不到的话。说明你项目res/values/color.xml没有新建或者没有white颜色这个值,仅仅需在res/values/color.xml里加入
- <color name="white">#ffffff</color>
这个白色值就可以。
另外View的动画Theme可选,建议也复制进去。效果好一些,代码例如以下:
- <font color="#333333"><font face="Arial"> <style name="BottomToTopAnim" parent="android:Animation">
- <item name="@android:windowEnterAnimation">@anim/bottomview_anim_enter</item>
- <item name="@android:windowExitAnimation">@anim/bottomview_anim_exit</item>
- </style></font></font>
- <?
xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="500"
- android:fromYDelta="100%p" />
- </set>
res/anim/bottomview_anim_exit.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="500"
- android:toYDelta="100%p" />
- </set>
总体为:
2、部分核心使用代码:
- BottomView bottomView = new BottomView(this,
- R.style.BottomViewTheme_Defalut, R.layout.bottom_view);
- bottomView.setAnimation(R.style.BottomToTopAnim);//设置动画,可选
- bottomView.showBottomView(false);
假设想获取这个View的话。调用.getView()方法就可以。
效果图之中的一个:(可任意发挥)下面是源码“:
package com.tandong.bottomview; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import com.tandong.bottomview.adapter.BVAdapter; import com.tandong.bottomview.view.BottomView; /** * BottomView * * www.aplesson.com * * @author TanDong * */ public class MainActivity extends Activity implements OnClickListener { private Button btn_show; private ListView lv_menu_list; private ArrayList<String> menus; private BottomView bottomView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { menus = new ArrayList<String>(); menus.add(getResources().getString(R.string.menu_search)); menus.add(getResources().getString(R.string.menu_filemanage)); menus.add(getResources().getString(R.string.menu_downloadmanage)); menus.add(getResources().getString(R.string.menu_setting)); menus.add(getResources().getString(R.string.menu_about)); } private void initView() { btn_show = (Button) this.findViewById(R.id.btn_show); btn_show.setOnClickListener(this); } @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.btn_show: bottomView = new BottomView(MainActivity.this, R.style.BottomViewTheme_Defalut, R.layout.bottom_view); bottomView.setAnimation(R.style.BottomToTopAnim); bottomView.showBottomView(false); lv_menu_list = (ListView) bottomView.getView().findViewById( R.id.lv_list); BVAdapter adapter = new BVAdapter(MainActivity.this, menus); lv_menu_list.setAdapter(adapter); lv_menu_list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?
> arg0, View arg1, int arg2, long arg3) { bottomView.dismissBottomView(); } }); break; default: break; } } }
package com.tandong.bottomview.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.tandong.bottomview.R;
/**
* BottomView
*
* @author TanDong
*
*/
public class BVAdapter extends BaseAdapter {
private Context c;
private ArrayList<String> alss;
public BVAdapter(Context context, ArrayList<String> als) {
this.c = context;
this.alss = als;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return alss.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return alss.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
convertView = View.inflate(c, R.layout.item, null);
TextView tv = (TextView) convertView.findViewById(R.id.tv_name);
tv.setText(alss.get(position));
return convertView;
}
}
原理:
package com.tandong.bottomview.view;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
public class BottomView
{
private View convertView;
private Context context;
private int theme;
private Dialog bv;
private int animationStyle;
private boolean isTop = false;
public BottomView(Context c, int theme, View convertView)
{
this.theme = theme;
this.context = c;
this.convertView = convertView;
}
public BottomView(Context c, int theme, int resource) {
this.theme = theme;
this.context = c;
this.convertView = View.inflate(c, resource, null);
}
public void showBottomView(boolean CanceledOnTouchOutside) {
if (this.theme == 0)
this.bv = new Dialog(this.context);
else
this.bv = new Dialog(this.context, this.theme);
this.bv.setCanceledOnTouchOutside(CanceledOnTouchOutside);
this.bv.getWindow().requestFeature(1);
this.bv.setContentView(this.convertView);
Window wm = this.bv.getWindow();
WindowManager m = wm.getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = wm.getAttributes();
p.width = (d.getWidth() * 1);
if (this.isTop)
p.gravity = 48;
else
p.gravity = 80;
if (this.animationStyle != 0)
{
wm.setWindowAnimations(this.animationStyle);
}
wm.setAttributes(p);
this.bv.show();
}
public void setTopIfNecessary() {
this.isTop = true;
}
public void setAnimation(int animationStyle) {
this.animationStyle = animationStyle;
}
public View getView() {
return this.convertView;
}
public void dismissBottomView() {
if (this.bv != null)
this.bv.dismiss();
}
}
posted on 2019-05-14 21:19 xfgnongmin 阅读(323) 评论(0) 编辑 收藏 举报