最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)仿QQ空間根據(jù)位置彈出PopupWindow顯示更多操作效果

 更新時(shí)間:2016年12月09日 09:04:46   作者:海豚灣  
我們打開QQ空間的時(shí)候有個(gè)箭頭按鈕點(diǎn)擊之后彈出PopupWindow會(huì)根據(jù)位置的變化顯示在箭頭的上方還是下方,比普通的PopupWindow彈在屏幕中間顯示好看的多,今天就給大家分享下實(shí)現(xiàn)代碼,需要的朋友參考下吧

我們打開QQ空間的時(shí)候有個(gè)箭頭按鈕點(diǎn)擊之后彈出PopupWindow會(huì)根據(jù)位置的變化顯示在箭頭的上方還是下方,比普通的PopupWindow彈在屏幕中間顯示好看的多。

先看QQ空間效果圖:

這個(gè)要實(shí)現(xiàn)這個(gè)效果可以分幾步進(jìn)行

1.第一步自定義PopupWindow,實(shí)現(xiàn)如圖的樣式,這個(gè)繼承PopupWindow自定義布局很容易實(shí)現(xiàn)

2.得到點(diǎn)擊按鈕的位置,根據(jù)位置是否在屏幕的中間的上方還是下方,將PopupWindow顯示在控件的上方或者下方

3.適配問題,因?yàn)镻opupWindow上面的操作列表是動(dòng)態(tài)的所以要自定義listView

4.動(dòng)畫效果+背景變暗

通過步驟分析,我們就很清晰的了解我們要做什么,話不多說,從第一步開始吧

下面自定義PopupWindow實(shí)現(xiàn)效果

1.重寫listView,重新計(jì)算高度(一般也應(yīng)用于解決ScrollView嵌套listView只顯示一行的問題)

public class MyListView extends ListView {
  public MyListView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public MyListView(Context context) {
    super(context);
  }
  public MyListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  @Override
  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
        MeasureSpec.AT_MOST));
  }
}

2.自定義PopupWindow的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="right">
  <ImageView
    android:id="@+id/arrow_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="28dp"
    android:src="@drawable/arrow_up_white"
    android:visibility="visible"/>
  <com.widget.MyListView
    android:id="@+id/lv_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/normal_margin8"
    android:layout_marginTop="-1dp"
    android:layout_marginBottom="-1dp"
    android:dividerHeight="0dp"
    android:layout_marginLeft="@dimen/normal_margin8"
    android:layout_marginRight="@dimen/normal_margin8"
    android:scrollbars="none"
    android:background="@drawable/custom_white"
    android:divider="@null"></com.widget.MyListView>
  <ImageView
    android:id="@+id/arrow_down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="28dp"
    android:src="@drawable/arrow_down_white"
    android:visibility="visible"/>
</LinearLayout>

2.PopupWindow彈出動(dòng)畫以及消失動(dòng)畫

popshow_operation_anim_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="90%"
    android:pivotY="0%"
    android:fillAfter="false"
    android:duration="300" >
  </scale>
</set>

popshow_operation_anim_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="90%"
    android:pivotY="100%"
    android:fillAfter="false"
    android:duration="250" >
  </scale>
</set>

消失動(dòng)畫是漸隱動(dòng)畫可以自己定義,同理。

3.重寫PopupWindow了

public class CustomOperationPopWindow extends PopupWindow {
  private Context context;
  private View conentView;
  private View backgroundView;
  private Animation anim_backgroundView;
  private MyListView listView;
  private TypeSelectPopuAdapter selectAdapter;
  ImageView arrow_up, arrow_down;
  List<TypeSelect> typeSelectlist = new ArrayList<>();
  int[] location = new int[2];
  private OnItemListener onItemListener;
  private AdapterView.OnItemClickListener onItemClickListener;
  public interface OnItemListener {
    public void OnItemListener(int position, TypeSelect typeSelect);
  }
  ;
  public void setOnItemMyListener(OnItemListener onItemListener) {
    this.onItemListener = onItemListener;
  }
  public CustomOperationPopWindow(Context context) {
    this.context = context;
    initView();
  }
  public CustomOperationPopWindow(Context context, List<TypeSelect> typeSelectlist) {
    this.context = context;
    this.typeSelectlist = typeSelectlist;
    initView();
  }
  private void initView() {
    this.anim_backgroundView = AnimationUtils.loadAnimation(context, R.anim.alpha_show_anim);
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.conentView = inflater.inflate(R.layout.view_operation_popupwindow, null);
    // 設(shè)置SelectPicPopupWindow的View
    this.setContentView(conentView);
    // 設(shè)置SelectPicPopupWindow彈出窗體的寬
    this.setWidth(LayoutParams.MATCH_PARENT);
    // 設(shè)置SelectPicPopupWindow彈出窗體的高
    this.setHeight(LayoutParams.WRAP_CONTENT);
    // 設(shè)置SelectPicPopupWindow彈出窗體可點(diǎn)擊
    this.setFocusable(true);
    this.setOutsideTouchable(true);
    // 刷新狀態(tài)
    this.update();
    // 實(shí)例化一個(gè)ColorDrawable顏色為半透明
    ColorDrawable dw = new ColorDrawable(0000000000);
    // 點(diǎn)back鍵和其他地方使其消失,設(shè)置了這個(gè)才能觸發(fā)OnDismisslistener ,設(shè)置其他控件變化等操作
    this.setBackgroundDrawable(dw);
    // 設(shè)置SelectPicPopupWindow彈出窗體動(dòng)畫效果
    this.setAnimationStyle(R.style.operation_popwindow_anim_style_up);
    this.listView = (MyListView) conentView.findViewById(R.id.lv_list);
    this.arrow_up = (ImageView) conentView.findViewById(R.id.arrow_up);
    this.arrow_down = (ImageView) conentView.findViewById(R.id.arrow_down);
    //設(shè)置適配器
    this.selectAdapter = new TypeSelectPopuAdapter(context, typeSelectlist,
        R.layout.item_operation_popu);
    this.listView.setAdapter(selectAdapter);
    this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (isShowing()) {
          dismiss();
        }
        onItemListener.OnItemListener(position, typeSelectlist.get(position));
      }
    });
    this.setOnDismissListener(new OnDismissListener() {
      @Override
      public void onDismiss() {
        if (backgroundView != null) {
          backgroundView.setVisibility(View.GONE);
        }
      }
    });
  }
  //設(shè)置數(shù)據(jù)
  public void setDataSource(List<TypeSelect> typeSelectlist) {
    this.typeSelectlist = typeSelectlist;
    this.selectAdapter.notifyDataSetChanged();
  }
  /**
   * 沒有半透明背景 顯示popupWindow
   *
   * @param
   */
  public void showPopupWindow(View v) {
    v.getLocationOnScreen(location); //獲取控件的位置坐標(biāo)
    //獲取自身的長(zhǎng)寬高
    conentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    if (location[1] > MainApplication.SCREEN_H / 2 + 100) { //MainApplication.SCREEN_H 為屏幕的高度,方法可以自己寫
      this.setAnimationStyle(R.style.operation_popwindow_anim_style_up);
      arrow_up.setVisibility(View.GONE);
      arrow_down.setVisibility(View.VISIBLE);
      this.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]), location[1] - conentView.getMeasuredHeight());
    } else {
      this.setAnimationStyle(R.style.operation_popwindow_anim_style_down);
      arrow_up.setVisibility(View.VISIBLE);
      arrow_down.setVisibility(View.GONE);
      this.showAsDropDown(v, 0, 0);
    }
  }
  /**
   * 攜帶半透明背景 顯示popupWindow
   *
   * @param
   */
  public void showPopupWindow(View v, View backgroundView) {
    this.backgroundView = backgroundView;
    v.getLocationOnScreen(location); //獲取控件的位置坐標(biāo)
    //獲取自身的長(zhǎng)寬高
    conentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    backgroundView.setVisibility(View.VISIBLE);
    //對(duì)view執(zhí)行動(dòng)畫
    backgroundView.startAnimation(anim_backgroundView);
    if (location[1] > MainApplication.SCREEN_H / 2 + 100) { //若是控件的y軸位置大于屏幕高度的一半,向上彈出
      this.setAnimationStyle(R.style.operation_popwindow_anim_style_up);
      arrow_up.setVisibility(View.GONE);
      arrow_down.setVisibility(View.VISIBLE);
      this.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]), location[1] - conentView.getMeasuredHeight()); //顯示指定控件的上方
    } else {
      this.setAnimationStyle(R.style.operation_popwindow_anim_style_down); //反之向下彈出
      arrow_up.setVisibility(View.VISIBLE);
      arrow_down.setVisibility(View.GONE);
      this.showAsDropDown(v, 0, 0);  //顯示指定控件的下方
    }
  }
  /**
   * 顯示popupWindow 根據(jù)特殊要求高度顯示位置 
   *
   * @param
   */
  public void showPopupWindow(View v, View backgroundView,int hight) {
    this.backgroundView = backgroundView;
    v.getLocationOnScreen(location);
    //獲取自身的長(zhǎng)寬高
    conentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    backgroundView.setVisibility(View.VISIBLE);
    //對(duì)view執(zhí)行動(dòng)畫
    backgroundView.startAnimation(anim_backgroundView);
    if (location[1] > MainApplication.SCREEN_H / 2 + 100) {
      this.setAnimationStyle(R.style.operation_popwindow_anim_style_up);
      arrow_up.setVisibility(View.GONE);
      arrow_down.setVisibility(View.VISIBLE);
      this.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]), location[1] - conentView.getMeasuredHeight()-hight);
    } else {
      this.setAnimationStyle(R.style.operation_popwindow_anim_style_down);
      arrow_up.setVisibility(View.VISIBLE);
      arrow_down.setVisibility(View.GONE);
      this.showAsDropDown(v, 0, 0);
    }
  }
}

 4.代碼中的用法

    1.

CustomOperationPopWindow customOperationPopWindow = new CustomOperationPopWindow(this, operationTypeSelectlist);
customOperationPopWindow.setOnItemMyListener(new CustomOperationPopWindow.OnItemListener() {
  @Override
  public void OnItemListener(int position, TypeSelect typeSelect) {
    //此處實(shí)現(xiàn)列表點(diǎn)擊所要進(jìn)行的操作
  }
});

  2.

textView.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
customOperationPopWindow.showPopupWindow(textView);//可以傳個(gè)半透明view v_background過去根據(jù)業(yè)務(wù)需要顯示隱藏
  }
});

 5.最終實(shí)際效果

以上代碼為幾乎主要全部代碼,主要是PopupWindow的用法,思路清晰一步一步實(shí)現(xiàn)很簡(jiǎn)單。

以上所述是小編給大家介紹的Android開發(fā)仿QQ空間根據(jù)位置彈出PopupWindow顯示更多操作效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android編程防止進(jìn)程被第三方軟件殺死的方法

    Android編程防止進(jìn)程被第三方軟件殺死的方法

    這篇文章主要介紹了Android編程防止進(jìn)程被第三方軟件殺死的方法,涉及Android進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 詳解Android如何實(shí)現(xiàn)不同大小的圓角

    詳解Android如何實(shí)現(xiàn)不同大小的圓角

    在開發(fā)過程中,設(shè)計(jì)常常會(huì)有一些比較炫酷的想法,比如兩邊不一樣大小的圓角啦,甚至四角的radius各不相同,對(duì)于這種情況我們?cè)撛趺磳?shí)現(xiàn)呢,本文小編就和大家來聊聊,需要的朋友可以參考下
    2023-08-08
  • Android開發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法示例

    Android開發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法示例

    這篇文章主要介紹了Android開發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法,結(jié)合實(shí)例形式分析了Android滑動(dòng)數(shù)值選擇器NumberPicker的功能、相關(guān)函數(shù)、事件監(jiān)聽、界面布局等操作技巧,需要的朋友可以參考下
    2019-03-03
  • Android 自定義View實(shí)現(xiàn)計(jì)時(shí)文字詳解

    Android 自定義View實(shí)現(xiàn)計(jì)時(shí)文字詳解

    這篇文章主要為大家介紹了Android 自定義View實(shí)現(xiàn)計(jì)時(shí)文字詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 安裝android開發(fā)環(huán)境原始版(windows版)

    安裝android開發(fā)環(huán)境原始版(windows版)

    安裝android開發(fā)環(huán)境原始版(windows版)的詳細(xì)步驟
    2013-03-03
  • Android開發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法

    Android開發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android開發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • Flutter實(shí)現(xiàn)底部導(dǎo)航欄

    Flutter實(shí)現(xiàn)底部導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)底部導(dǎo)航欄的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • RxJava兩步打造華麗的Android引導(dǎo)頁

    RxJava兩步打造華麗的Android引導(dǎo)頁

    如今,移動(dòng)應(yīng)用對(duì)首次使用的用戶呈現(xiàn)歡迎頁已經(jīng)越來越普遍了。這樣做的目的就是向用戶介紹并展示我們的應(yīng)用。本文給Android開發(fā)的引導(dǎo)頁面提供了很多參考,非常值得一讀。
    2016-07-07
  • android仿Adapter實(shí)現(xiàn)自定義PagerAdapter方法示例

    android仿Adapter實(shí)現(xiàn)自定義PagerAdapter方法示例

    這篇文章主要給大家介紹了關(guān)于android仿Adapter實(shí)現(xiàn)自定義PagerAdapter的相關(guān)資料,文中詳細(xì)介紹了關(guān)于PagerAdapter的用法,對(duì)大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android裁剪圖像實(shí)現(xiàn)方法示例

    Android裁剪圖像實(shí)現(xiàn)方法示例

    這篇文章主要介紹了Android裁剪圖像實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式分析了Android針對(duì)圖片的讀取、調(diào)用、裁剪、保存等操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-08-08

最新評(píng)論

衡山县| 凤城市| 莎车县| 新民市| 乐清市| 张家界市| 横山县| 卓资县| 永年县| 津南区| 甘德县| 腾冲县| 岳普湖县| 望都县| 绥阳县| 河南省| 东明县| 阿荣旗| 宝鸡市| 安国市| 乐昌市| 肇东市| 邮箱| 阳西县| 铜陵市| 黄骅市| 县级市| 长丰县| 三江| 六枝特区| 庆云县| 通河县| 五华县| 融水| 凤台县| 年辖:市辖区| 深州市| 宁化县| 通州区| 海门市| 连平县|