如何仅在Android中保存剪辑路径

如何仅在Android中保存剪辑路径,android,Android,这就是我所拥有的: Bitmap result = Bitmap.create(600, 600, Config.ARG_8888); Canvas canvas = new Canvas(result); canvas.clipPath(path) // custom path is a rotated rectangle that has an offset x and y within the 600x600 rectangle canvas.drawBitmap(sourceBitmap

这就是我所拥有的:

Bitmap result = Bitmap.create(600, 600, Config.ARG_8888);
Canvas canvas = new Canvas(result);
canvas.clipPath(path) // custom path is a rotated rectangle that has an offset x and y within the 600x600 rectangle
canvas.drawBitmap(sourceBitmap, 0, 0, new Paint());
canvas.save();
当我检查结果位图时,我只看到我想要的剪辑路径,但我希望结果图像的大小与剪辑路径相同,而不是仍然为600x600


谢谢

正如我在上面的评论中所说的,您可以使用
drawBitmap(位图位图、Rect src、Rect dst、Paint)
位图结果
裁剪为裁剪后的结果,也可以直接执行:

// get the Bitmap
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

// setup sample Path
Path path = new Path();
path.moveTo(10, 10);
path.lineTo(30, 10);
path.lineTo(30, 20);
path.close();

RectF bounds = new RectF();
path.computeBounds(bounds, true);
Rect pathBounds = new Rect();
bounds.roundOut(pathBounds);

Bitmap outBitmap = Bitmap.createBitmap(pathBounds.width(), pathBounds.height(), Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Canvas c = new Canvas(outBitmap);
Paint paint = new Paint();
paint.setShader(shader);
c.translate(-pathBounds.left, -pathBounds.top);
c.drawPath(path, paint);

正如我在上面的评论中所说,您可以使用
drawBitmap(位图位图、Rect src、Rect dst、Paint Paint)
位图结果
裁剪为裁剪后的结果,也可以直接进行:

// get the Bitmap
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

// setup sample Path
Path path = new Path();
path.moveTo(10, 10);
path.lineTo(30, 10);
path.lineTo(30, 20);
path.close();

RectF bounds = new RectF();
path.computeBounds(bounds, true);
Rect pathBounds = new Rect();
bounds.roundOut(pathBounds);

Bitmap outBitmap = Bitmap.createBitmap(pathBounds.width(), pathBounds.height(), Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Canvas c = new Canvas(outBitmap);
Paint paint = new Paint();
paint.setShader(shader);
c.translate(-pathBounds.left, -pathBounds.top);
c.drawPath(path, paint);

正如我在上面的评论中所说,您可以使用
drawBitmap(位图位图、Rect src、Rect dst、Paint Paint)
位图结果
裁剪为裁剪后的结果,也可以直接进行:

// get the Bitmap
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

// setup sample Path
Path path = new Path();
path.moveTo(10, 10);
path.lineTo(30, 10);
path.lineTo(30, 20);
path.close();

RectF bounds = new RectF();
path.computeBounds(bounds, true);
Rect pathBounds = new Rect();
bounds.roundOut(pathBounds);

Bitmap outBitmap = Bitmap.createBitmap(pathBounds.width(), pathBounds.height(), Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Canvas c = new Canvas(outBitmap);
Paint paint = new Paint();
paint.setShader(shader);
c.translate(-pathBounds.left, -pathBounds.top);
c.drawPath(path, paint);

正如我在上面的评论中所说,您可以使用
drawBitmap(位图位图、Rect src、Rect dst、Paint Paint)
位图结果
裁剪为裁剪后的结果,也可以直接进行:

// get the Bitmap
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

// setup sample Path
Path path = new Path();
path.moveTo(10, 10);
path.lineTo(30, 10);
path.lineTo(30, 20);
path.close();

RectF bounds = new RectF();
path.computeBounds(bounds, true);
Rect pathBounds = new Rect();
bounds.roundOut(pathBounds);

Bitmap outBitmap = Bitmap.createBitmap(pathBounds.width(), pathBounds.height(), Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Canvas c = new Canvas(outBitmap);
Paint paint = new Paint();
paint.setShader(shader);
c.translate(-pathBounds.left, -pathBounds.top);
c.drawPath(path, paint);

然后按路径的边界裁剪它。你的意思是我可以做Bitmap.createBitmap(src,left,top,width,height,matrix,true)?矩阵将包含矩形旋转的角度?角度?什么角度?不,使用不起作用的drawBitmap(位图位图,Rect src,Rect dst,Paint),它只将我的整个图像缩放到边界,它不会将原始图像裁剪到自定义路径,我将其裁剪为路径边界。你的意思是我可以做位图。创建位图(src,left,top,width,height,matrix,true)?矩阵将包含矩形旋转的角度?角度?什么角度?不,使用不起作用的drawBitmap(位图位图,Rect src,Rect dst,Paint),它只将我的整个图像缩放到边界,它不会将原始图像裁剪到自定义路径,我将其裁剪为路径边界。你的意思是我可以做位图。创建位图(src,left,top,width,height,matrix,true)?矩阵将包含矩形旋转的角度?角度?什么角度?不,使用不起作用的drawBitmap(位图位图,Rect src,Rect dst,Paint),它只将我的整个图像缩放到边界,它不会将原始图像裁剪到自定义路径,我将其裁剪为路径边界。你的意思是我可以做位图。创建位图(src,left,top,width,height,matrix,true)?矩阵将包含矩形旋转的角度?角度?什么角度?不,使用drawBitmap(位图位图、Rect src、Rect dst、Paint Paint)不起作用,它只会将整个图像缩放到边界,而不会将原始图像裁剪到我剪裁的自定义路径