欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義popupwindow實(shí)例代碼

 更新時(shí)間:2016年11月25日 11:33:44   作者:森林森  
這篇文章主要為大家詳細(xì)介紹了Android自定義popupwindow實(shí)例代碼,popupwindow彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先來(lái)看看效果圖:

一、布局 

<?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="wrap_content"
  android:orientation="vertical"
  android:background="#ffffff"
  android:padding="20dp" >

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:gravity="center"
    android:textColor="@android:color/holo_orange_dark"
    android:text="確定" />

  <TextView
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:clickable="true"
    android:gravity="center"
    android:text="取消" />

</LinearLayout>

2、自定義MypopupWindow繼承PopupWindow

public class MyPopupWindow extends PopupWindow {  

3、重寫構(gòu)造方法與動(dòng)畫樣式
在styles.xml自定義樣式,動(dòng)畫

<style name="MyPopupWindow">

    <item name="android:windowEnterAnimation">@anim/pop_in</item>
    <item name="android:windowExitAnimation">@anim/pop_out</item>
  </style>

 pop_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

  <!-- 平移
  <translate
     android:duration="5000"
     android:fromXDelta="100%"

     android:toXDelta="0"/>
     -->

  <scale
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.8"
    android:toYScale="0.5"
    android:duration="200"/>

  <!--
fromXScale
fromYScale
起始時(shí)X,Y座標(biāo),

pivotX
pivotY

動(dòng)畫起始位置,相對(duì)于屏幕的百分比,兩個(gè)都為50%表示動(dòng)畫從屏幕中間開始

toXScale
toYScale
動(dòng)畫最終縮放的倍數(shù), 1.0為正常大小,大于1.0放大

duration
動(dòng)畫持續(xù)時(shí)間


 -->

  <!--透明度-->
  <alpha
    android:duration="200"
    android:fromAlpha="0.0"

    android:toAlpha="1.0"/>


</set>

pop_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

  <!-- <translate
    android:duration="5000"
    android:fromXDelta="0"

    android:toXDelta="100%"/>-->

  <scale
    android:fromXScale="0.8"
    android:fromYScale="0.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0"
    android:toYScale="0"
    android:duration="200"/>



  <alpha
    android:duration="200"
    android:fromAlpha="1.0"

    android:toAlpha="0.0"/>

</set>

4、重寫構(gòu)造方法并設(shè)置點(diǎn)擊外部可以消失監(jiān)聽

 super(context);

    this.mContext=context;
    //打氣筒
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    //打氣

    mContentView = mInflater.inflate(R.layout.layout_dialog,null);

    //設(shè)置View
    setContentView(mContentView);


    //設(shè)置寬與高
    setWidth(WindowManager.LayoutParams.MATCH_PARENT);

    setHeight(WindowManager.LayoutParams.WRAP_CONTENT);


    /**
     * 設(shè)置進(jìn)出動(dòng)畫
     */
    setAnimationStyle(R.style.MyPopupWindow);


    /**
     * 設(shè)置背景只有設(shè)置了這個(gè)才可以點(diǎn)擊外邊和BACK消失
     */
    setBackgroundDrawable(new ColorDrawable());


    /**
     * 設(shè)置可以獲取集點(diǎn)
     */
    setFocusable(true);

    /**
     * 設(shè)置點(diǎn)擊外邊可以消失
     */
    setOutsideTouchable(true);

    /**
     *設(shè)置可以觸摸
     */
    setTouchable(true);


    /**
     * 設(shè)置點(diǎn)擊外部可以消失
     */

    setTouchInterceptor(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

        /**
         * 判斷是不是點(diǎn)擊了外部
         */
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
          return true;
        }
        //不是點(diǎn)擊外部
        return false;
      }
    });

5、顯示及設(shè)置窗口變暗與變亮

public void displayDialog(View view){

    MyPopupWindow myPopupWindow = new MyPopupWindow(this);

    myPopupWindow.showAsDropDown(mBtnDispaly,0,0);
    lightOff();


    /**
     * 消失時(shí)屏幕變亮
     */
    myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();

        layoutParams.alpha=1.0f;

        getWindow().setAttributes(layoutParams);
      }
    });
  }

  /**
   * 顯示時(shí)屏幕變暗
   */
  private void lightOff() {

    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();

    layoutParams.alpha=0.3f;

    getWindow().setAttributes(layoutParams);

  }

6、完整

package liu.basedemo.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import liu.basedemo.R;


/**
 * 學(xué)習(xí)PopupWindow
 * Created by 劉楠 on 2016/8/1 0001.17:42
 */
public class MyPopupWindow extends PopupWindow {

  Context mContext;
  private LayoutInflater mInflater;
  private View mContentView;


  public MyPopupWindow(Context context) {
    super(context);

    this.mContext=context;
    //打氣筒
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    //打氣

    mContentView = mInflater.inflate(R.layout.layout_dialog,null);

    //設(shè)置View
    setContentView(mContentView);


    //設(shè)置寬與高
    setWidth(WindowManager.LayoutParams.MATCH_PARENT);

    setHeight(WindowManager.LayoutParams.WRAP_CONTENT);


    /**
     * 設(shè)置進(jìn)出動(dòng)畫
     */
    setAnimationStyle(R.style.MyPopupWindow);


    /**
     * 設(shè)置背景只有設(shè)置了這個(gè)才可以點(diǎn)擊外邊和BACK消失
     */
    setBackgroundDrawable(new ColorDrawable());


    /**
     * 設(shè)置可以獲取集點(diǎn)
     */
    setFocusable(true);

    /**
     * 設(shè)置點(diǎn)擊外邊可以消失
     */
    setOutsideTouchable(true);

    /**
     *設(shè)置可以觸摸
     */
    setTouchable(true);


    /**
     * 設(shè)置點(diǎn)擊外部可以消失
     */

    setTouchInterceptor(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

        /**
         * 判斷是不是點(diǎn)擊了外部
         */
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
          return true;
        }
        //不是點(diǎn)擊外部
        return false;
      }
    });


    /**
     * 初始化View與監(jiān)聽器
     */
    initView();

    initListener();
  }




  private void initView() {

  }

  private void initListener() {

  }


}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Https證書過(guò)期的兩種解決方案

    Android Https證書過(guò)期的兩種解決方案

    應(yīng)該有很多小伙伴遇到這樣一個(gè)問(wèn)題,在線上已發(fā)布的app里,關(guān)于https的cer證書過(guò)期,從而導(dǎo)致app所有網(wǎng)絡(luò)請(qǐng)求失效無(wú)法使用,這篇文章主要介紹了Android Https證書過(guò)期的解決方案,需要的朋友可以參考下
    2022-12-12
  • Android UI控件之ListView實(shí)現(xiàn)圓角效果

    Android UI控件之ListView實(shí)現(xiàn)圓角效果

    這篇文章主要為大家詳細(xì)介紹了Android UI控件之ListView實(shí)現(xiàn)圓角效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android中獲取設(shè)備的各種信息總結(jié)

    Android中獲取設(shè)備的各種信息總結(jié)

    相信各位Android的開發(fā)者們都知道,現(xiàn)在幾乎所有的app都需要獲得設(shè)備信息,那么下面這篇文章就來(lái)詳細(xì)說(shuō)說(shuō)獲取設(shè)備信息的方法。
    2016-09-09
  • Android中RecyclerView的item寬高問(wèn)題詳解

    Android中RecyclerView的item寬高問(wèn)題詳解

    RecyclerView出現(xiàn)已經(jīng)有一段時(shí)間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關(guān)于Android中RecyclerView的item寬高問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-08-08
  • OpenGL?Shader實(shí)現(xiàn)陰影遮罩效果

    OpenGL?Shader實(shí)現(xiàn)陰影遮罩效果

    這篇文章主要介紹了如何利用OpenGL?Shader實(shí)現(xiàn)陰影遮罩效果,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)OpenGL有一定幫助,需要的可以參考一下
    2022-02-02
  • Android深入分析屬性動(dòng)畫源碼

    Android深入分析屬性動(dòng)畫源碼

    這篇文章主要給大家介紹了關(guān)于Android動(dòng)畫系列教程之屬性動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Android PhotoView使用步驟實(shí)例詳解

    Android PhotoView使用步驟實(shí)例詳解

    這篇文章主要介紹了Android PhotoView使用步驟實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)

    Android中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)

    下面小編就為大家?guī)?lái)一篇Android中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • Android?Gradle模塊依賴替換使用技巧

    Android?Gradle模塊依賴替換使用技巧

    這篇文章主要為大家介紹了Android?Gradle模塊依賴替換使用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • android實(shí)現(xiàn)添加耳機(jī)狀態(tài)圖標(biāo)的方法

    android實(shí)現(xiàn)添加耳機(jī)狀態(tài)圖標(biāo)的方法

    這篇文章主要介紹了android實(shí)現(xiàn)添加耳機(jī)狀態(tài)圖標(biāo)的方法,較為詳細(xì)的分析了Android實(shí)現(xiàn)添加耳機(jī)圖標(biāo)的原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10

最新評(píng)論