android 简单模仿IOS的3D Touch功能

博主对iOS的3D Touch功能情有独钟,决定在Android平台上进行模仿实现。在搜索并尝试多种方法无果后,从GitHub上找到了一个开源项目,该项目能实现基本的3D Touch效果。本文主要记录了这一过程,并分享了实现的核心代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        个人对IOS端3D Touch功能很感兴趣,想来自己实现,上网一搜,这类文章并不是很多,于是想自己实现一个。在自己绞尽脑汁的时候在github上搜索到一个开源项目可以实现简单3D Touch功能,在此坐一下笔记,以供后续参考。

话不多说,贴出核心代码:

//设置PeekView选项
PeekViewOptions options = new PeekViewOptions();
options.setBackgroundDim(1f);           // range: 0  - 1  (default is .6)   背景模糊度
options.setHapticFeedback(false);       // default is true

// it may be a good idea to set set these through resources so that you can use different options based on screen size and orientation
options.setWidthPercent(.4f);           // range: .1 - .9 (default is .6)
options.setHeightPercent(.4f);          // range: .1 - .9 (default is .5)

// you can also set the size of the PeekView using absolute values, instead of percentages.
// Setting these will override the corresponding percentage value.
// You should use this instead of setting the size of the view from the layout resources, as those get overridden.
options.setAbsoluteWidth(200);          // 200 DP
options.setAbsoluteHeight(200);         // 200 DP

// default is false. If you change this to true, it will ignore the width and height percentages you set.
options.setFullScreenPeek(true);
// default is true. Unless you are going to animate things yourself, i recommend leaving this as true.
//options.setFadeAnimation(false);
options.setUseFadeAnimation(true);

// PeekView has the ability to blur the background behind it, instead of just using a simple dark dim.
// If you set a blurred view, then it will invalidate whatever you set as your background dim.
// If you do this, please look at the installation steps for the blur effect, or the app will crash.
options.setBlurBackground(true);                            // default is true
// options.setBlurOverlayColor(Color.parse("#99000000"));      // #99000000 default
options.setBlurOverlayColor(Color.parseColor("#99000000"));    //背景颜色

//Peek.into(...).with(options).applyTo(...);


Peek.into(R.layout.image_peek, new SimpleOnPeek() {
    @Override
    public void onInflated(View rootView) {
        WebView mWebView = (WebView) rootView.findViewById(R.id.mWebView);
        final ProgressBar mProgressBar = (ProgressBar) rootView.findViewById(R.id.mProgressBar);
        mWebView.loadUrl("http://www.baidu.com");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress==100) {
                    mProgressBar.setVisibility(View.GONE);

                } else {
                    mProgressBar.setVisibility(View.VISIBLE);
                }
                mProgressBar.setProgress(newProgress);

                super.onProgressChanged(view, newProgress);
            }


        });


    }
}).with(options).applyTo(this, baseView);

一个模仿iOS3D Touch效果的库,因为安卓本身不支持3D Touch,所以事件的触发是用长按点击来替代。项目地址:https://github.com/shalskar/PeekAndPop demo地址:https://github.com/shalskar/PeekAndPopDemo 效果图:使用说明:开始这个库托管在 Jitpack.io,所以在根 build.gradle文件中添加:allprojects {     repositories {        ...         maven { url "https://jitpack.io" }     } }然后在application的 build.gradle文件中添加如下依赖:dependencies {     compile 'com.github.shalskar:PeekAndPop:v0.1.1' }基本的使用很简单,只需一个activity实例,一个为 peek and pop准备的布局文件,一个或者多个在长按之后显示的 peek and pop视图。PeekAndPop peekAndPop = new PeekAndPop.Builder(this)                 .peekLayout(R.layout.peek_view)                 .longClickViews(view)                 .build();你可以调用PeekAndPop对象的getPeekView()来得到 peek view ,并使用 findViewById() 来得到 peek layout中的任意视图。View peekView = peekAndPop.getPeekView(); ImageView imageView = peekView.findViewById(R.id.image_view); TextView textView = peekView.findViewById(R.id.text_view);通常你可能还会想在列表中的某个item被点击时显示peek and pop ,为了让peek and pop正常工作,你需要添加这行代码:                .parentViewGroupToDisallowTouchEvents(viewGroup)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值