Android 动态壁纸缩放

发布于 2024-10-20 15:38:26 字数 779 浏览 3 评论 0原文

我正在学习如何制作动态壁纸,但我有一个困境,我相信所有刚开始的人也有这样的困境。

有这么多分辨率的屏幕尺寸,我怎样才能使一组艺术品在代码中为所有版本重新缩放?我知道它已经完成了,因为我看到了很多应用程序中的图像,并且它们被重新缩放。

如果只是一张不需要任何定位的图像,那就很容易了,但我的问题是我必须重新调整背景图像以适合所有设备,我还有适合该背景上某个 x 和 y 位置的动画图像适合到位,这样看起来整个背景都在动画化,但只有一部分是动画化的(我的方式是远离 300 张逐帧动态壁纸图像)。

因此,背景图像需要重新缩放,动画也需要重新缩放到与背景图像相同的百分比,并且它们需要位于特定的 x 和 y 位置。

任何帮助将不胜感激,以便我能够继续下去。


我厌倦了一些事情,想我会为所有事情制作一个缩放器,例如:int scaler;然后在onSurfaceChanged中scaler = width /1024; //如果更大的图像是1024。这将为我提供一个可以在任何地方使用的比率。然后使用scaleBitmap进行相应缩放,方法是将缩放器乘以图像高度和宽度,并使用相同的缩放器来定位示例图像x,假设图像x为50,使用相同的东西缩放它x =缩放器* 50;应该负责缩放和定位,下一课就是如何将所有这些转换为 java,因为我是 java 新手,我曾经为 flash 和 php 编程,但这有很大不同,需要一些时间来适应。接下来的事情是如何平移宽度,当您将屏幕从一侧移动到另一侧时,如何使图像显示是我想出的下一个难题。现在,无论如何,它都只显示相同的宽度,即使宽度是表面显示的两倍。如果您得到答案或在某个地方我可以找到有关此问题的信息,我将不胜感激。

I am learning how to make live wallpapers, but I have a dilemma I'm sure all who start off have as well.

There is so many resolution screen sizes, how can I just make one set of artwork to be rescaled in code for all versions? I know it's been done as I seen the images in the apk's on a lot of them and they get rescaled.

If it was just one image that did not need any positioning that would be easy, but my problem is I have to get the background image rescaled to fit all devices, I also have animations that fit in a certain x and y position on that background image to fit in place so it looks like the whole background is being animated but only parts of it is (my way of staying away from 300 images of frame by frame live wallpapers).

So the background image needs to be rescaled and the animations need to be rescaled as well to the exact percentage as the background image and they need to sit in a specific x and y position.

Any help would be appreciated so I can get this going.


I tired a few things, figured I would make a scaler for everything example: int scaler; then in onSurfaceChanged scaler = width /1024; //if the bigger image is 1024. that will give me a ratio to work with everywhere. then scale accordingly using scaleBitmap by multiplying the scaler by the image height and width, and also use the same scaler for positioning example image x lets say is at 50, scale it using the same thing x = scaler * 50; that should take care of scaling and positioning, just how to translate all this into java is the next lesson, since I'm new to java, I used to program for flash and php but this is a lot different, take some getting used to. Next thing is how to pan the width, when you move your screen from side to side how to make the image show is the next puzzle I have figure out. Right now it just shows the same width no matter what even though the width is double what the surface shows. If you got an answer or somewhere I can find out the info on this one that would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离不开的别离 2024-10-27 15:38:26

嗯,嗯,我只能说“欢迎来到现实世界”。您可以通过 onSurfaceChanged 将屏幕尺寸传递给您,是的,您的工作就是弄清楚如何根据此数据缩放所有内容。这就是为什么他们付给我们大笔钱。 :-)
您需要确保您的资源足够大,可以容纳您打算支持的最大显示器,因此您将始终缩小事物(这比扩展事物造成的扭曲要小得多)。

建议从此处的“屏幕独立最佳实践”开始:http://developer.android.com /guide/practices/screens_support.html

关于您寻求更多帮助的请求的其他评论...

  1. 您不能(必然)仅使用宽度来缩放您的图稿,因为您需要支持多种宽高比。如果屏幕比例与您的作品不匹配,您必须决定是否要扭曲您的作品、留下空白等。
  2. 我不知道如何解释您在屏幕尺寸上遇到的麻烦。我们大多数人将所有活动代码放在一个引擎类中,因此我们的方法可以通过私有变量共享数据。例如,在SDK中的Cube壁纸中,onSurfaceChanged()设置mCenterX以便稍后在drawCube()中使用。我建议从类似的简单方法开始。
  3. 处理滚动需要一些“智能”并仔细评估通过 onOffsetsChanged() 收到的数据。 xStep 指示您的启动器支持的屏幕数量。通常xStep为0.25,表示5个屏幕(即xOffset = 0、0.25、0.5、0.75或1),但它可以是0到1之间的任何值; 0.5 表示 3 个屏幕。 xPixels 可让您了解启动器“希望”您根据您所在的屏幕移动图像的程度;通常你应该尊重这一点。在我的手机上,启动器“需要”具有两倍于物理屏幕像素的虚拟壁纸,因此每次滚动应该仅移动屏幕像素的四分之一。所有这些以及更多内容都记录在 http://developer.android.com/参考/android/app/WallpaperManager.html
    这不是“简单”的编码——应用程序比壁纸更容易。 :-)

祝你好运...乔治

P.S.我还要再补充一件事:在某个地方,您可能想要检索启动器所需的壁纸的“所需最小宽度”,这样您就可以明确地理解 xPixels 中隐含的虚拟化。例如,在我的引擎构造函数中,我

mContext = getApplicationContext();
mWM = WallpaperManager.getInstance(mContext);
mDW = mWM.getDesiredMinimumWidth();

的设备具有 320 像素宽度;我得到 mDW = 640;当我从一个屏幕滚动到另一个屏幕时,xPixels 每次都会改变 80...因为四个滚动(跨五个屏幕)应该使显示的艺术品数量加倍(这种效果称为“视差滚动”)。最右边的部分 xPixels 等于 0;中心(五个)部分的 xPixels = -160 等。

Well, um, all I can say is "Welcome to the real world." You get your screen dimensions passed to you via onSurfaceChanged, and yes, it is your job to figure out how to scale everything based on this data. That's why they pay us the big bucks. :-)
You will want to make sure your resources are large enough to fit the biggest display you intend to support, so you will always be shrinking things (which distorts much less than expanding things).

Suggest starting with "best practices for screen independence" here: http://developer.android.com/guide/practices/screens_support.html

Additional comments in re your request for more help...

  1. You cannot (necessarily) scale your artwork just using the width, because you need to support multiple aspect ratios. If the screen proportions do not match your artwork, you must decide if you want to distort your artwork, leave blank spaces, etc.
  2. I'm not sure how to interpret your trouble passing around the screen dimensions. Most of us put all of our active code within a single engine class, so our methods can share data via private variables. For example, in the Cube wallpaper in the SDK, onSurfaceChanged() sets mCenterX for later use in drawCube(). I suggest beginning with a similar, simple approach.
  3. Handling scrolling takes some "intelligence" and a careful assessment of the data you receive via onOffsetsChanged(). xStep indicates how many screens your launcher supports. Normally xStep will be 0.25, indicating 5 screens (i.e. xOffset = 0, 0.25, 0.5, 0.75, or 1) but it can be any value from 0 to 1; 0.5 would indicate 3 screens. xPixels gives you an indication of how much the launcher "wants" you to shift your imagery based on the screen you're on; normally you should respect this. On my phone, the launcher "desires" a virtual wallpaper with twice the pixels of the physical screen, so each scroll is supposed to shift things only one quarter of one screen's pixels. All this, and more, is documented in http://developer.android.com/reference/android/app/WallpaperManager.html
    This is not "easy" coding--apps are easier than wallpaper. :-)

Good luck...George

P.S. I'll throw in one more thing: somewhere along the line you might want to retrieve the "desired minimum width" of the wallpaper desired by the launcher, so you can explicitly understand the virtualization implicit in xPixels. For example, in my engine constructor, I have

mContext = getApplicationContext();
mWM = WallpaperManager.getInstance(mContext);
mDW = mWM.getDesiredMinimumWidth();

My device has 320 pixel width; I get mDW = 640; as I scroll from screen to screen, xPixels changes by 80 each time...because four scrolls (across five screens) is supposed to double the amount of revealed artwork (this effect is called "parallax scrolling"). The rightmost section has xPixels equals 0; the center (of five) sections has xPixels = -160, etc.

找个人就嫁了吧 2024-10-27 15:38:26

我使用此代码片段来缩放一张图像以适应不同的屏幕尺寸。

Bitmap image1, pic1;
image1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
float xScale = (float) canvas.getWidth() / image1.getWidth();
                float yScale = (float) canvas.getHeight() / image1.getHeight();
                float scale = Math.max(xScale, yScale); //selects the larger size to grow the images by

                //scale = (float) (scale*1.1); //this allows for ensuring the image covers the whole screen.

                scaledWidth = scale * image1.getWidth();
                scaledHeight = scale * image1.getHeight();

                    pic1 = Bitmap.createScaledBitmap(image1, (int)scaledWidth, (int)scaledHeight, true);

确保边缘不包含重要信息,因为在某些屏幕比例下,边缘会从图片中缩小。

I've used this code snippet to scale one image to fit on different screen sizes.

Bitmap image1, pic1;
image1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
float xScale = (float) canvas.getWidth() / image1.getWidth();
                float yScale = (float) canvas.getHeight() / image1.getHeight();
                float scale = Math.max(xScale, yScale); //selects the larger size to grow the images by

                //scale = (float) (scale*1.1); //this allows for ensuring the image covers the whole screen.

                scaledWidth = scale * image1.getWidth();
                scaledHeight = scale * image1.getHeight();

                    pic1 = Bitmap.createScaledBitmap(image1, (int)scaledWidth, (int)scaledHeight, true);

Make sure that the edges don't contain vital information as it will be scaled out of the picture on some screen ratios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文