天天看點

AndroidUI開源元件庫BottomView 第三方自定義UI控件

實作功能:

可以在底部彈出的View裡自定義布局;

可以自定義是否可以觸摸外部消失;

可以自定義事件;

可以自定義外圍背景是否透明;

可以自定義動畫;

如果需要的話,可以強制為頂部View顯示

BottomView.jar庫檔案下載下傳位址:http://download.csdn.net/detail/jay100500/7547055

BottomView的Demo下載下傳位址:http://download.csdn.net/detail/jay100500/7547049

其實原理就是WindowManager Dialog 和Animation的組合

這裡分享一個Android的非常經典實用而且簡單友善的第三方UI控件庫:BottomView(小米的米UI也用到了這個)

用法:

1、下載下傳BottomView.jar庫檔案,放到Android項目工程裡的libs裡

2、設定BottomView的Theme:

這2個Theme複制粘貼到你的項目的res/values/styles.xml裡即可

  1. <!--半透明背景Theme--> 
  2.    <style name="BottomViewTheme_Defalut"> 
  3.         <item name="android:windowFrame">@null</item> 
  4.         <item name="android:windowContentOverlay">@null</item> 
  5.         <item name="android:windowIsFloating">true</item> 
  6.         <item name="android:windowIsTranslucent">false</item> 
  7.         <item name="android:windowNoTitle">true</item> 
  8.         <item name="android:windowBackground">@color/white</item> 
  9.         <item name="android:backgroundDimEnabled">true</item> 
  10.         <item name="android:windowFullscreen">true</item> 
  11.     </style> 
  12. <!--透明背景Theme--> 
  13.     <style name="BottomViewTheme_Transparent"> 
  14.         <!-- Transparent --> 
  15.         <item name="android:backgroundDimEnabled">false</item> 
  16.     </style>

複制代碼

另外如果提示

  1. <item name="android:windowBackground">@color/white</item>

這裡的white找不到的話,說明你項目res/values/color.xml沒有建立或者沒有white顔色這個值,隻需在res/values/color.xml裡添加

  1.      <color name="white">#ffffff</color>

這個白色值即可。

另外View的動畫Theme可選,建議也複制進去,效果好一些,代碼如下:

  1. <font color="#333333"><font face="Arial"> <style name="BottomToTopAnim" parent="android:Animation"> 
  2.         <item name="@android:windowEnterAnimation">@anim/bottomview_anim_enter</item> 
  3.         <item name="@android:windowExitAnimation">@anim/bottomview_anim_exit</item> 
  4.     </style></font></font>

res/anim/bottomview_anim_enter.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" > 
  3.     <translate 
  4.         android:duration="500" 
  5.         android:fromYDelta="100%p" /> 
  6. </set>

res/anim/bottomview_anim_exit.xml

  1.         android:toYDelta="100%p" /> 

整體為:

AndroidUI開源元件庫BottomView 第三方自定義UI控件

2、部分核心使用代碼:

  1. BottomView bottomView = new BottomView(this, 
  2.                                         R.style.BottomViewTheme_Defalut, R.layout.bottom_view); 
  3.                         bottomView.setAnimation(R.style.BottomToTopAnim);//設定動畫,可選 
  4.                         bottomView.showBottomView(false);

如果想擷取這個View的話,調用.getView()方法即可。

效果圖之一:(可随意發揮)

AndroidUI開源元件庫BottomView 第三方自定義UI控件

以下是源代碼“:

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