Android绘制相关注意问题

  • 画笔粗细导致的Rect和line不精准绘制问题

画笔粗细在画line的时候应该注意,画笔以中心向两边扩展,目前用rect代替line去画,然后rect四周往内偏移画笔一半粗细。

  • path.op问题
    路径取交集的时候如果往path添加的是Rect集合,那么确保Rect不会绘制错误,这样不同path取交集出问题就不难排查

  • 绘制的时候bitmap获取非透明像素
    bitmap.extractAlpha();

  • bitmap使用放大不能用createBitmap要用Martix

  Matrix matrix = new Matrix();
           matrix.postScale(sx, sy);
           result = Bitmap.createBitmap(width, height, bitmap.getConfig());
           Canvas c = new Canvas(result);
           c.drawBitmap(bitmap, matrix, null);
           bitmap.recycle();
           bitmap = result;
  • 取值
    getX(),getLeft(),0和getMeasureWidth();
    在当前View中绘制(0,0,getMeasureWidth(),getMeasureHeight());
    在父容器中绘制View(getX(),getY(),getX()+getMeasureWidth(),getY()+getMeasureHeight();
  • 获取截图
    public static Bitmap getBitmapCache(View view){
        //刷新
        view.setDrawingCacheEnabled(false);
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();

        //不全
        /*view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();*/

        //只有黑线
       /* view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        Canvas canvas=new Canvas();
        Bitmap bitmap=Bitmap.createBitmap(view.getMeasuredWidth(),
                view.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
        view.draw(canvas);*/

        return bitmap;
    }
    //Webview截图获取不可见区域
     Bitmap getViewBitmap(WebView webView){
        int height = (int) (webView.getContentHeight() * webView.getScale());
        int width = webView.getWidth();
        int pH = webView.getHeight();
        Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bm);
        int top = height;
        while (top > 0) {
            if (top < pH) {
                top = 0;
            } else {
                top -= pH;
            }
            canvas.save();
            canvas.clipRect(0, top, width, top + pH);
            webView.scrollTo(0, top);
            webView.draw(canvas);
            canvas.restore();
        }
        return bm;
    }

  • 限制当前区域大小,用于父容器使用了adjustclipbounds=true的情况
    Rect clipBounds;
    private void clip() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setClipToOutline(true);
        }else{
            if (clipBounds==null)
             clipBounds = new Rect(getLeft(), getTop(), getRight(), getBottom());
            ViewCompat.setClipBounds(this, clipBounds);
        }
    }
    
  • 将bitmap绘制时限制在指定的宽高,rect是View的矩形区域,nullPaint是空对象
canvas.drawBitmap(ramdomBitmap, height, width), null, rect, nullPaint);

  • 保证添加shadower的时候视图大小一致
    因为绘制shadower的时候是按照px绘制的,所以我们把px当做dp转成px之后再计算缩放比之后用矩阵放大就能保证视图大小一致

addViewInLayout

批量添加View如果是用addView的方式,内部会调用requestLayout就会产生性能上的问题,可以使用addViewInLayout代替,不过这个方法是protected的,只能子类访问,所以子类改成public可供外部使用,跟对象的clone方法是一样的。
这里写图片描述
该方法本来是用来给你在onLayout里面addView的,不过这样你可以在全部加完之后一次requestLayout

removeInLayout

这个方法是public所以可以直接用。

ps:这点算我孤陋寡闻了,到今天才发现,起因是因为iOS是默认手动刷新的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值