如何以像素为单位设置Android画布的宽度和高度?

如何以像素为单位设置Android画布的宽度和高度?,android,android-canvas,Android,Android Canvas,我需要创建一个像素大小为500x500的画布元素。如何在px中设置宽度/高度?就像view.height=100一样,因此此视图在px单位中的高度为100可能不适用于您的情况,但这对我有效 Bitmap animation = BitmapFactory.decodeResource(mContext.getResources(), resourceId, mBitmapOptions); //Get a bitmap from a image file // Create a bit

我需要创建一个像素大小为500x500的画布元素。如何在px中设置宽度/高度?

就像view.height=100一样,因此此视图在px单位中的高度为100

可能不适用于您的情况,但这对我有效

Bitmap animation = BitmapFactory.decodeResource(mContext.getResources(), resourceId, mBitmapOptions); //Get a bitmap from a image file

    // Create a bitmap for the part of the screen that needs updating.
    Bitmap bitmap = Bitmap.createBitmap(animation.getWidth(), animation.getHeight(), BITMAP_CONFIG);
    bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    Canvas canvas = new Canvas(bitmap);
您已经创建了所需大小的位图。然后Canvas Canvas=新的Canvasbitmap;
这将画布设置为位图的大小

您可以在xml中执行此操作,并设置高度和使用px单位。