【闲来无事,腾讯T3大佬亲自教你

实现

我们只需要对 iv_above 进行裁剪操作即可

首先获得iv_above的bitmap


bmAbove = ((BitmapDrawable)ivOrange.getDrawable()).getBitmap();



对滚动进行监听:

(实际上知乎的功能是对滑动方向进行判断后,直接开始进行动画,扇形半径并不跟滑动距离有关联,当滑动方向与上次的相反时,停止上次的动画,再reverse,这样做的好处就是在滑动时进行了最少的运算,避免了潜在的滑动卡顿,我还是直接关联扇形半径和滑动距离)


      private float screenWidth;

    private float threshold = 500;

        scrollView.setListener(new MyScrollView.onScrollListener() {

            @Override

            public void onScroll(int l, int t, int oldl, int oldt) {

                float curY = (float)t;

                if(t > oldt){

                    //scroll down

                    isScrollUp = false;

                    float r = curY / threshold ;

                    ivOrange.setImageBitmap(circleBitmap(bmOrange, r * screenWidth));

                }else{

                    isScrollUp = true;

                    float r = curY / threshold ;

                    ivOrange.setImageBitmap(circleBitmap(bmOrange, r * screenWidth));

                }

            }

        });

裁剪



    private Bitmap circleBitmap(Bitmap o,float radius){

        Bitmap outputBm = Bitmap.createBitmap(o.getWidth(),o.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(outputBm);

        final Paint paint = new Paint();

        paint.setAntiAlias(true);

        paint.setFilterBitmap(true);

        paint.setDither(true);



        final Rect rect = new Rect(0,0,o.getWidth(),o.getHeight());

        canvas.drawARGB(0,0,0,0);

        canvas.drawCircle(0,0,radius,paint );

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        canvas.drawBitmap(o,rect,rect,paint);

        return outputBm;

    }



裁剪

 private Bitmap circleBitmap(Bitmap o,float radius){

        Bitmap outputBm = Bitmap.createBitmap(o.getWidth(),o.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(outputBm);

        final Paint paint = new Paint();

        paint.setAntiAlias(true);

        paint.setFilterBitmap(true);

        paint.setDither(true);



        final Rect rect = new Rect(0,0,o.getWidth(),o.getHeight());

        canvas.drawARGB(0,0,0,0);

        canvas.drawCircle(0,0,radius,paint );

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        canvas.drawBitmap(o,rect,rect,paint);

        return outputBm;

    }



用到paint.setXfermode,传入new PorterDuffXfermode 可以对2d图像的合成模式进行设置。

(.setXfermode这个方法功能很复杂也很强大,有兴趣的可以去百度一下)

总计有18个合成模式:


CLEAR/

SRC/

DST/

SRC_OVER/

DST_OVER

> **由于篇幅原因,这份面试宝典已经被整理成了PDF文档,有需要Android面试宝典全套完整文档的麻烦点赞!**

**[CodeChina开源项目地址:https://codechina.csdn.net/m0_60958482/android_p7](https://codechina.csdn.net/m0_60958482/android_p7)**

> ![](https://www.icode9.com/i/ll/?i=img_convert/8056e43b21baa10b3f0c4fe49dee868b.png)


本文在开源项目:[CodeChina中已收录](https://codechina.csdn.net/m0_60958482/android_p7),里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中...

oid_p7)**

> [外链图片转存中...(img-nUI8uDJ1-1631080502948)]


本文在开源项目:[CodeChina中已收录](https://codechina.csdn.net/m0_60958482/android_p7),里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中...

上一篇:CopyOnWriteArrayList 你了解多少?,腾讯T3大牛手把手教你


下一篇:9.6 历城二中练习题