Android 自定义view的简单应用(2) 毛玻璃效果

先看效果图

 

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:background="#000000"
    tools:context=".MainActivity">

    <myapp.customview.CustomView.CircleImageView
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

 

这里用到了RenderScript

blur.java

public class blur
{
    private static int width = 0;
    private static int height = 0;

    public static Bitmap goBlur(Bitmap bitmap,float radius,Context mContext) {
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(mContext);

        //Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, result);

        //Set the radius of the blur: 0 < radius <= 25
        blurScript.setRadius(radius);

        //Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        //Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(result);

        //After finishing everything, we destroy the Renderscript.
        rs.destroy();

        return result;
    }
}
CircleImageView.java
public class CircleImageView extends View implements View.OnTouchListener {

    boolean MoveCircle = false;
    boolean init = true;
    int CircleCenterX = 0;
    int CircleCenterY = 0;
    int radius = 0;

    int[] TouchMove = new int[2];
    Bitmap newmap;

    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        this.setOnTouchListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Bitmap head = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        if(init) {
            int width = this.getMeasuredWidth();
            int height = this.getMeasuredHeight();

            CircleCenterX = width / 2;
            CircleCenterY = height / 2;
            radius = width / 4;

            init = false;

            Bitmap tmp = head;
            for (int i = 0; i < 10; i++) {
                newmap = blur.goBlur(tmp, 25f, getContext());
                tmp = newmap;
            }
        }

        if (newmap != null)
            canvas.drawBitmap(newmap, 0, 0, new Paint());

        Path path = new Path();
        path.addCircle(CircleCenterX,CircleCenterY,radius, Path.Direction.CCW);
        canvas.clipPath(path);
        canvas.drawBitmap(head,0,0,new Paint());
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
        {
            MoveCircle = false;
            /*
            判断按下是否在圆内 计算按下坐标到当前圆心的距离是否大于圆半径
             */
            int downX = (int) motionEvent.getX();
            int downY = (int) motionEvent.getY();

            TouchMove[0] = downX;
            TouchMove[1] = downY;

            int distance = (int) Math.sqrt(Math.pow((downX - CircleCenterX),2) + Math.pow((downY - CircleCenterY),2));

            if(distance < radius)
            {
                MoveCircle = true;
            }
        }

        if(motionEvent.getAction() == MotionEvent.ACTION_MOVE)
        {
            if(MoveCircle)
            {
                int X = (int) motionEvent.getX();
                int Y = (int) motionEvent.getY();

                CircleCenterX = CircleCenterX + X - TouchMove[0];
                CircleCenterY = CircleCenterY + Y - TouchMove[1];

                TouchMove[0] = CircleCenterX;
                TouchMove[1] = CircleCenterY;

                invalidate();
            }
        }

        if(motionEvent.getAction() == MotionEvent.ACTION_UP)
        {
            MoveCircle = false;
        }

        return false;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值