gpt4 book ai didi

android - 自定义 View 屏幕截图以编程方式在 android 中不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:05:13 25 4
gpt4 key购买 nike

我有这样的自定义 View ,

public class RoundedCornerLayout extends FrameLayout
{
private final static float CORNER_RADIUS = 100.0f;

private Bitmap maskBitmap;
private Paint paint, maskPaint;
private float cornerRadius;

public RoundedCornerLayout(Context context)
{
super(context);
init(context, null, 0);
}

public RoundedCornerLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context, attrs, 0);
}

public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}

private void init(Context context, AttributeSet attrs, int defStyle)
{
p = new Paint();
p.setFlags(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.WHITE);
p.setStyle(android.graphics.Paint.Style.STROKE);
p.setStrokeWidth(6);

pp = new Paint();
pp.setColor(Color.parseColor("#686868"));

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);

paint = new Paint(Paint.ANTI_ALIAS_FLAG);

maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

setWillNotDraw(false);
}

Paint p;
Paint pp;

@Override
public void draw(Canvas canvas)
{

android.util.Log.e("MAYUR", "draw");

canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getWidth() / 2, pp);

Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas offscreenCanvas = new Canvas(offscreenBitmap);

super.draw(offscreenCanvas);

if (maskBitmap == null)
{
maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
}

offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);

canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, (canvas.getWidth() - 6) / 2, p);
}

private Bitmap createMask(int width, int height)
{
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(mask);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);

canvas.drawRect(0, 0, width, height, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);

return mask;
}
}

我正在截屏,

View v1 = ll_contain;
v1.setDrawingCacheEnabled(true);
v1.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

并将位图保存到 sdcard,但保存结果与屏幕上显示的不同。为什么?我哪里错了?

请给我解决这个问题的方向,我试图搜索问题,但我没有找到任何人遇到此类问题。

屏幕上的 View 显示,

enter image description here

截屏时,保存为

enter image description here

最佳答案

我遇到了类似的问题(对于 Drawable),问题本质上在于使用 canvas.getHeight()canvas.getWidth() 位置和大小的方法,同时进行 drawCircle 调用。在实际绘制 View 时,这些方法返回与 View 尺寸相同的值,一切顺利。在绘制屏幕截图时,这些将返回整个屏幕截图的高度和宽度,而不是 View 的尺寸。

解决方案是计算变量 canvasHeightcanvasWidth 作为如下:

  • 对于可绘制对象:canvasWidth = getBounds().width();
  • 对于 View :canvasWidth = getRight() - getLeft();

关于android - 自定义 View 屏幕截图以编程方式在 android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32921931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com