[Do not hate those that are not open source] Xiaomi's rice UI's BOTTOMVIEW source code is perfectly decompiled

Source: Internet
Author: User

The principle is the combination of WindowManager Dialog and Animation.


Here we will share a very classic, practical and simple Third-Party UI control library for Android: BottomView (this is also used by Mi UI of Xiaomi)

Functions:

You can customize the layout in the View popped up at the bottom;

You can customize whether the image can be touched and the image disappears;

You can customize events;

You can customize whether the peripheral background is transparent;

You can customize animations;

If needed, you can force the display of the Top View



Usage:

1. Download The BottomView. jar library file and put it in libs of the Android project.

2. Set the Theme of BottomView:

Copy and paste the two Theme files to res/values/styles. xml of your project.



Copy code
If you are prompted Copy code
If white cannot be found here, it means that the res/values/color. xml of your project has not been created or the value of white color does not exist. You only need to add it to res/values/color. xml.
Copy code
This white value is enough.

In addition, the animation Theme of the View is optional. We recommend that you copy the animation to it. The Code is as follows:

Copy codeRes/anim/bottomview_anim_enter.xml Copy code
Res/anim/bottomview_anim_exit.xml
Copy code


Overall:



2. Some core code:


Copy code


To obtain this View, call the. getView () method.

One: (available at Will)



The following is the source code ":

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;@Overrideprotected 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);}@Overridepublic 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() {@Overridepublic 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;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn alss.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn alss.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic 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;}}



Principle:


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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.