您的位置:首页 > 移动开发 > Android开发

Android绘制相关注意问题

2018-02-07 00:58 381 查看
画笔粗细导致的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;
}


限制当前区域大小,用于父容器使用了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之后再计算缩放比之后用矩阵放大就能保证视图大小一致
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: