如何吸引画布?

问题描述 投票:0回答:1
例如,例如,为了在位图上显示任何类型的视图(当然没有父母),我可以调用下一个功能:

public void drawViewToBitmap(Bitmap b, View v, Rect rect) { Canvas c = new Canvas(b); // <= use rect to let the view to draw only into this boundary inside the bitmap view.draw(c); }

有可能吗?也许这甚至是在幕后工作的方式?

我应该在图纸和画布的创建之间写什么?

Edit:我已经尝试了下一个代码,但它没有起作用:
public void drawFromViewToCanvas(final View view, final Rect rect, final Canvas canvas) {
    final int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY);
    final int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);
    // Lay the view out with the known dimensions
    view.layout(0, 0, rect.width(), rect.height());
    // Translate the canvas so the view is drawn at the proper coordinates
    canvas.save();
    canvas.translate(rect.left, rect.top);
    // Draw the View and clear the translation
    view.draw(canvas);
    canvas.restore();
}

用法示例:

final int imageSize = 50; rect = new Rect(35, 344 , 35 + imageSize, 344 + imageSize); final ImageView imageView = new ImageView(mContext); imageView.setImageBitmap(bitmap); imageView.setScaleType(ScaleType.CENTER_CROP); drawFromViewToCanvas(imageView, getRect(), canvas);


Edit:

Sony的网站上有一个样本:
int measureWidth = View.MeasureSpec.makeMeasureSpec(bitmapWidth, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(bitmapHeight, View.MeasureSpec.EXACTLY);
view.measure(measureWidth, measuredHeight);
view.layout(0, 0, bitmapWidth, bitmapHeight);
view.draw(canvas);

wonger是否有效。

Yeah,您可以做到这一点。请记住,由于您没有将其附加到布局上,因此您需要在绘制它之前手动布置它:

view.layout(0, 0, viewWidth, viewHeight); 除非您只知道对这些宽度和高度参数的确切知道什么,否则您可能还想先测量它:

int widthSpec = MeasureSpec.makeMeasureSpec (ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED;
int heightSpec = MeasureSpec.makeMeasureSpec (400, MeasureSpec.UNSPECIFIED;
view.measure(widthSpec, heightSpec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

Edit:

android bitmap draw android-canvas mutable
1个回答
71
投票

//Lay the view out with the known dimensions view.layout (0, 0, rect.width(), rect.height()); //Translate the canvas so the view is drawn at the proper coordinates canvas.save(); canvas.translate(rect.left, rect.top); //Draw the View and clear the translation view.draw(canvas); canvas.restore();

又来了:
YYES,测试。您可以自己尝试:

public class DrawingActivity extends Activity { public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Set a Rect for the 200 x 200 px center of a 400 x 400 px area Rect rect = new Rect(); rect.set(100, 100, 300, 300); //Allocate a new Bitmap at 400 x 400 px Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); //Make a new view and lay it out at the desired Rect dimensions TextView view = new TextView(this); view.setText("This is a custom drawn textview"); view.setBackgroundColor(Color.RED); view.setGravity(Gravity.CENTER); //Measure the view at the exact dimensions (otherwise the text won't center correctly) int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY); int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY); view.measure(widthSpec, heightSpec); //Lay the view out at the rect width and height view.layout(0, 0, rect.width(), rect.height()); //Translate the Canvas into position and draw it canvas.save(); canvas.translate(rect.left, rect.top); view.draw(canvas); canvas.restore(); //To make sure it works, set the bitmap to an ImageView ImageView imageView = new ImageView(this); imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); setContentView(imageView); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setImageBitmap(bitmap); } }
    

工程代码在2025年:

private fun drawViewAtPosition(view: View, x: Int, y: Int, width: Int, height: Int, canvas: Canvas) { view.measure( MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY) ) view.layout(x, y, x + width, y + height) view.draw(canvas) }
    

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.