1. 程式人生 > >安卓 高斯背景模糊popupwindow,彈簧彈出 rebounds

安卓 高斯背景模糊popupwindow,彈簧彈出 rebounds

最終類似效果圖,沒有截動畫,gridelayout彈上來的時候是波紋的。

直接上程式碼。

public class MyBlurPopWin extends BlurPopupWindow
{
    Context mContext;
    GridLayout gridLayout;
    SpringChain springChain;
    Button closeBtn;
    public MyBlurPopWin(Context context) {
        super(context);
        mContext=context;
    }


    @Override//父類回撥,父類建立一個根FrameLayout,這裡用來添加里面的內容。
    public void onCreatView(FrameLayout container) {
        //初始化很多東西,父類。
        super.onCreatView(container);
        //這裡必須用三個引數,並且設定成false才能獲得到layoutparams!!!否則獲得到空的。
        //當然也可以自己設定layoutparams。
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.layout,container,false);
        FrameLayout.LayoutParams frameLayout= (FrameLayout.LayoutParams) rootView.getLayoutParams();
        frameLayout.gravity=Gravity.BOTTOM;
        //rootView.findViewById(R.id.btn).setOnClickListener(this);
        //FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //layoutParams.gravity=Gravity.BOTTOM;
        //rootView.setLayoutParams(layoutParams);
        //找到id,view
        gridLayout = (GridLayout) rootView.findViewById(R.id.group);
        closeBtn= (Button) rootView.findViewById(R.id.btn);
        //設定監聽,用來關閉popwin,用到了父類的backpress。
        setClickableItems(closeBtn);
        //新增到父容器。
        container.addView(rootView);
    }

    @Override//show的時候父類動畫的時候呼叫此函式,在這裡重寫,實現自己需要的函式
    public void onShowAnimStart() {
        Log.e("xxx","onShowAnimStart");
        super.onShowAnimStart();
        springChain =SpringChain.create(50,6,40,7);             //faceBook的rebounds
        for (int i=0;i<gridLayout.getChildCount();i++){
            final View child=gridLayout.getChildAt(i);
            springChain.addSpring(new SimpleSpringListener(){
                @Override
                public void onSpringUpdate(Spring spring) {
                    super.onSpringUpdate(spring);
                    double value=spring.getCurrentValue();
                    child.setTranslationY((float) (value*gridLayout.getMeasuredHeight()));
                }
            });
        }
        for(Spring s:springChain.getAllSprings()){
            s.setCurrentValue(1);
        }

        springChain.setControlSpringIndex(1).getControlSpring().setEndValue(0);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn:
                onBackpressed();
                break;
        }
    }

    @Override
    public boolean onBackpressed() {
        boolean isDismiss=super.onBackpressed();
        if (!isDismiss){
            springChain.setControlSpringIndex(1).getControlSpring().setEndValue(1);
        }
        return isDismiss;
    }
}


用到的高斯模糊popwin

public class BlurPopupWindow {
    PopupWindow mPopupWindow;
    View mRootView;
    FrameLayout mContentView;
    ImageView mBlurImageView;
    Activity mActivity;
    private int mDelayAnim=150;

    public BlurPopupWindow(Context context) {
        mActivity = (Activity) context;
        if (mActivity.getParent() != null) {
            ViewGroup contentView= (ViewGroup)  mActivity.getParent().getWindow().findViewById(Window.ID_ANDROID_CONTENT);
            mRootView = (ViewGroup) contentView.getChildAt(0);
            mActivity = mActivity.getParent();
        }
        if(mRootView==null){
            ViewGroup contentView= (ViewGroup) mActivity.getWindow().findViewById(Window.ID_ANDROID_CONTENT);
            mRootView = (ViewGroup) contentView.getChildAt(0);
        }
        generateContentView();
        BlurKit.init(mActivity.getApplicationContext());

    }
    public boolean isShowing(){
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            return true ;
        }
        return false;
    }
    boolean isAnimRuning ;
    public boolean onBackpressed(){
        if (mPopupWindow == null || !mPopupWindow.isShowing()) {
            return true ;
        }
        if(isAnimRuning){
            return true;
        }
        ValueAnimator valueAnimator = ValueAnimator.ofInt(255,0).setDuration(400);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {


            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                mBlurImageView.setImageAlpha(value);
            }
        });
        valueAnimator.setStartDelay(mDelayAnim);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.setRepeatCount(0);
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
                isAnimRuning = false;

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                isAnimRuning = false;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
        isAnimRuning = true;
        return false;
    }
    public void onShowAnimStart(){
    }
    public void onShowAnimEnd(){

    }
    public final void dismiss(){
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
        isAnimRuning = false;
    }
    private void generateContentView() {
        mContentView = new FrameLayout(mActivity);
        mPopupWindow = new PopupWindow(mContentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mBlurImageView = new ImageView(mActivity);
        mBlurImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.addView(mBlurImageView, params);
        mPopupWindow.setContentView(mContentView);
        mPopupWindow.setAnimationStyle(0);
//        mPopupWindow.setFocusable(true);
//        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(false);
        mContentView.setFocusableInTouchMode(true);
        mContentView.setFocusable(true);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//        mContentView.setForeground(new ColorDrawable(Color.BLACK));
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mContentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {


                    return true;
                }
                return false;
            }
        });

    }

    public void show() {
        if(!isViewCreated){
            isViewCreated =true;
            onCreatView(mContentView);
        }
        Bitmap bitmap = BlurKit.getInstance().fastBlur(mRootView,10,0.12f);
//        Bitmap bitmap = BlurKit.getInstance().fastBlur(mRootView,5,0.5f);
        mBlurImageView.setImageBitmap(bitmap);
        mBlurImageView.setImageAlpha(0);
        mBlurImageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {

                ValueAnimator valueAnimator = ValueAnimator.ofInt(0,255).setDuration(400);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {


                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        int value = (int) valueAnimator.getAnimatedValue();
                        mBlurImageView.setImageAlpha(value);
                    }
                });
                valueAnimator.setInterpolator(new DecelerateInterpolator());
                valueAnimator.setRepeatCount(0);
                valueAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        onShowAnimStart();
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        isAnimRuning= false;
                        onShowAnimEnd();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        isAnimRuning= false;
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                valueAnimator.start();
                mBlurImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                isAnimRuning =true;
                return true;
            }
        });

        mPopupWindow.showAtLocation(mRootView, Gravity.CENTER , 0, 0);
    }
    boolean isViewCreated;
    public void onCreatView(FrameLayout container) {


    }
    @SuppressWarnings("unchecked")
    final public <E extends View> E findView(int id) {
        try {
            return (E)mContentView.findViewById(id);
        } catch (ClassCastException e) {

            throw e;
        }
    }

    /* Click listener that avoid double click event in short time*/
    public NoDoubleClickListener mNoDoubleClickListener = new NoDoubleClickListener(){

        @Override
        public void onClickInternal(View v) {
            BlurPopupWindow.this.onClick(v);
        }
    };

    public interface NoDoubleClickListener{
        public void onClickInternal(View v);

    }
    public void onClick(View v){

    }
    /*Set all widget that need to implements OnClick() here*/
    protected void setClickableItems(View... views) {
        if (views != null && views.length > 0) {
            for (View v : views) {
                if (v != null)
                    v.setOnClickListener((new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BlurPopupWindow.this.onClick(v);
                        }
                    }));
            }
        }
    }
    /*Set all widget that need to implements OnClick() here*/
    protected void setClickableItems(int... residGroup) {
        if (residGroup != null && residGroup.length > 0) {
            for (int resid : residGroup) {
                if (resid != 0) {
                    findView(resid).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BlurPopupWindow.this.onClick(v);
                        }
                    });
                }
            }
        }
    }



}


blurPopupwindow用到的工具

public class BlurKit {

    private static BlurKit instance;

    private RenderScript rs;

    public static void init(Context context) {
        if (instance != null) {
            return;
        }

        instance = new BlurKit();
        instance.rs = RenderScript.create(context);
    }

    public Bitmap blur(Bitmap src, int radius) {
        final Allocation input = Allocation.createFromBitmap(rs, src);
        final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setRadius(radius);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(src);
        return src;
    }

    public Bitmap blur(View src, int radius) {
        Bitmap bitmap = getBitmapForView(src, 1f);
        return blur(bitmap, radius);
    }

    public Bitmap fastBlur(View src, int radius, float downscaleFactor) {
        Bitmap bitmap = getBitmapForView(src, downscaleFactor);
        return blur(bitmap, radius);
    }

    private Bitmap getBitmapForView(View src, float downscaleFactor) {
        Bitmap bitmap = Bitmap.createBitmap(
                (int) (src.getWidth() * downscaleFactor),
                (int) (src.getHeight() * downscaleFactor),
                Bitmap.Config.ARGB_4444
        );

        Canvas canvas = new Canvas(bitmap);
        Matrix matrix = new Matrix();
        matrix.preScale(downscaleFactor, downscaleFactor);
        canvas.setMatrix(matrix);
        src.draw(canvas);

        return bitmap;
    }

    public static BlurKit getInstance() {
        if (instance == null) {
            throw new RuntimeException("BlurKit not initialized!");
        }

        return instance;
    }

}


相關推薦

背景模糊popupwindow彈簧 rebounds

最終類似效果圖,沒有截動畫,gridelayout彈上來的時候是波紋的。 直接上程式碼。 public class MyBlurPopWin extends BlurPopupWindow { Context mContext; GridLayout

關於手機訪問一些網站或者Fiori應用安裝證書的提示

有朋友問遇到在安卓手機上安裝Fiori Client,開啟的時候提示需要安裝證書,如下圖所示: 我在自己的Android手機試了試,因為我沒有裝Fiori Client,所以就用手機瀏覽器直接訪問https://go.sap.com: 彈出了類似的對話方塊。點選取消之後,可以手動輸入使用者名稱和密碼,但

使用Socket發送中文C語言服務端接收亂碼問題解決方式

article nbsp ons size ret con pre n+1 utf8 今天用安卓通過Socket發送數據到電腦上使用C語言寫的服務端,發送英文沒有問題,可當把數據改變成中文時,服務端接收到的數據確是亂碼。 突然想到。VS的預處理使用的

朱雀大廳源碼制作惡意軟件Skygofree爆發連你的照片都能監控到

進化 收集 工程 一份 全面 惡意軟件 window skype 一個 昨日,根據卡巴斯基實驗室公布的一份報告朱雀大廳源碼制作(h5.hxforum.com)企鵝2952777280 三公,炸金花、三公源碼出售 房卡出售 後臺出租,研究人員發現了一款相當強悍的惡意軟件,名為

GMM混合背景建模C++結合Opencv實現(內附Matlab實現)

最近在做視訊流檢測方面的工作,一般情況下這種視訊流檢測是較複雜的場景,比如交通監控,或者各種監控攝像頭,場景比較複雜,因此需要構建背景影象,然後去檢測場景中每一幀動態變化的前景部分,GMM高斯模型是建模的一種方法,關於高斯建模的介紹有很多部落格了,大家可以去找一找,本篇部落格主要依賴於上

圓角、背景遮罩。覆蓋實現方式(適用於所有控制元件)

1.工具類直接用(已經改好) package com.etwod.yulin.t4.unit; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap;

背景建模演算法--平均背景建模、單背景建模C實現

1、背景建模   視訊影象中運動目標的檢測的一種基本思想是對影象的背景進行建模,既是將所有畫素分為背景和運動前景兩類。 背景建模方法可以分為兩類 – 顏色背景建模和紋理背景建模。此文只涉及顏色背景建模。紋理背景建模可參考 顏色背景建模的基本原理:對影象中每個畫素的顏色值(灰度或彩色)進

微信小程式手機訪問不到圖片無法顯示圖片

關於微信小程式不顯示圖片 通病可能有以下幾個可能性: 非本地圖片:確定圖片資源存在,copy 圖片url再瀏覽器開啟,確定圖片資源存在且能正常訪問 本地圖片:確定相對路徑或者絕對路徑正確 微信小程式圖片路徑,不可以存在中文,使用英文做路徑和檔名 檔案字尾文,小寫。且保證正確 網路圖片,必須確保域名已經備案

IOS和共用一個二維碼實現掃碼跳轉連結-踩坑記_04

ios和安卓共用一個二維碼,實現掃碼跳轉連結 直接上程式碼了 也是參考了網上的demo,直接小修改了一下。臨時接了一個任務,直接套過來了。因為安卓微信的內建的瀏覽器和手機自帶的不一樣,在ios上面沒有問題,會直接提示跳轉到appstore。要求是實現安卓微信跳轉顯示

實現沉浸式狀態列相容小米、魅族

現階段不是很忙,就總結一下自己都實現過的功能。這一篇總結安卓實現沉浸式狀態列 Android4.4(API 19) - Android 5.0(API 21): 這個階段可以實現沉浸式,但是表現得還不是很好,實現方式為: 通過FLAG_TRANSLUCENT_STATUS設定

RK N 專案預置三方apkout目錄有生成刷機系統中沒有的問題

**RK3288 安卓N專案客戶要求預置兩個APK,要求使用者可以解除安裝,恢復出廠設定時不能恢復;安卓平常的經驗嘗試了幾種方法都不行,讓自己一度開始懷疑人生了,後面經同事指導,在程式碼裡面載入預裝apk後,才搞定,感謝!前面也是無語到崩潰啊; 以下是嘗試方法和修改的過程

Kotlin:實現okhttp3持續登入同步到webview

經常用httpclient請求的的情況下,一般就是用jsoup解析,去爬資料,用okhttp3實現cookie的儲存。 這裡做的是用okhttp3實現登入請求,然後直接將登入後可訪問的頁面Cookie同步載入到webview裡面去。 將賬號與密碼儲存到shareP ···

Python3 + Appium + 模擬器 實現APP自動化測試並生成測試報告

概述 本文主要分為以下幾個部分 安裝Python3 安裝Python3的Appium庫 安裝Android SDK 安裝JDK 安裝Appium 安裝模擬器 編寫測試指令碼並生成測試報告 正文 一、安裝Python3 直接登入Python官網https://

濾波-模糊圖片

#include <iostream> #include <opencv2/opencv.hpp> #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using name

的webView嵌入網頁後傳送圖片至網頁網頁請求檔案操作

public class WebViewDemo extends FragmentActivity {     private static final int FILE_SELECT_CODE = 0;     private WebView webView;    

微信開啟X5除錯使微信頁面可以在谷歌瀏覽器除錯【chrome://inspect/#devices】

1.微信瀏覽器開啟:http://debugx5.qq.com2.開啟後選擇以下選項3.開啟微信公眾號頁面4.開啟chrome://inspect/#devices  然後按移動端除錯方式開啟微信公眾號

基於的四級英語學習軟體基於java畢業設計

**基於安卓的四級英語學習軟體,基於java畢業設計** 基於安卓的四級英語學習軟體mysql資料庫建立語句 基於安卓的四級英語學習軟體oracle資料庫建立語句 基於安卓的四級英語學習軟體sqlserver資料庫建立語句 基於安卓的四級英語學習軟體spring s

iscroll在版本(6.0以上)某些機型上滑動卡頓問題的解決方法

問題:發現公司專案移動端的分類頁面在某些安卓機型上滑動時異常卡頓,而且出現卡頓的手機都是非常新的安卓手機,除錯的時候發現在谷歌瀏覽器的手機模擬滾動時也非常卡頓   在一段糾結異常的除錯和搜尋下找到了解決方法: 使用用fixed版本的iscroll就可以了:https://github.com/

影象模糊演算法及其 C 實現

高斯模糊的基本思路是根據二維 正太分佈 正態分佈 (感謝 xhr 大牛指正錯別字) 公式生成一個高斯矩陣, 求新影象中的每一點時, 將高斯矩陣的中心對準舊影象的這一點, 並將所有點根據高斯矩陣上對應的點加權平均. 二維正態分佈公式如下: u, v 分別為水平、豎直距離. 觀察可得, 當 r>3σ

關於版本訪問網路出現.NetworkOnMainThreadException異常的問題

  一、問題  在安卓2.3版本之前,我們在MainThread裡面進行網路操作時沒有問題的,但是在2.3版本之後(也就是3.0等),就會出現.NetworkOnMainThreadException異常。舉一個例子,比如我們要顯示一張網路圖片,以csdn的logo為例,可以