Android scaling

I am running into a great deal of trouble trying to get my app to scale properly with android devices with devices that do not have standard aspect ratios. I am creating a “virtual viewport” as width and height. I designate the height to be 768, and then the width will equal actualScreenWidth / Height * 768 which keeps the size everything being drawn pretty uniform across devices and it seems like that should work with any viewport and keep the image the same size. I draw everything to the virtual viewport and then stretch the viewport to a render target that is the same size as the actual screen resolution. Depending on the aspect ratio of the device, it stretches it sometimes where it shouldn’t and it just doesn’t come out correctly. Since what I’m attempting isn’t working properly, what would be the best way to keep the internal vertical resolution 768 and have the width be dynamic so that it doesn’t stretch, but keeps the same size for any resolution?

You have to have both width and height fixed internally.

I do it like this.
On initialization game creates the render target of the required size.
Render loop uses this render target for all the drawing, and as a last step it draws this render target to the screen.
This way there is no worrying about scaling during the game.

There are multiple ways to deal with aspect ratios. It depends on what you want to do with the blank space.
I calculate the scaling ratio like this:
float widthScale = ActualScreenWidth / (float)RequiredWidth;
float heightScale = ActualScreenHeight / (float)RequiredHeight;
Now only one of these values can be used for scaling if you want uniform aspect ratio.

If you want all content to be on the screen:
float scale = Math.Min(widthScale, heightScale);

From here:
int scaledWidth = (int)(scale * RequiredWidth);
int scaledHeight = (int)(scale * RequiredHeight);