- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已阅读“屏幕支持 API 指南”(http://developer.android.com/guide/practices/screens_support.html) 和更多资源,但我无法理解 dpi 的工作原理。
我正在开发一款游戏,我没有使用任何布局(我将使用 canvas.drawbitmap 等函数自行绘制)。但是当我使用 Canvas.Drawbitmap 函数时,我需要指定要绘制图像的屏幕像素。
所以我现在使用固定分辨率 (1280x800) 并使用 drawable-nodpi 文件夹,如果手机屏幕更宽或更窄,我会稍后调整 Canvas 。问题在于,当分辨率不是原始分辨率 (1280x800) 时,图像看起来很糟糕。
我该怎么做才能解决这个问题?我在 3 天内阅读和阅读,但所有解释和示例都与布局、 9-patch 等相关。
最佳答案
获取正在使用的设备的密度,并将该密度乘以您选择的某个基本尺寸(您实际希望绘制多大或多小?)
例子:
float objectToDrawHeight = 50; //Specified in plain pixels
float objectToDrawWidth = 50; //Specified in plain pixels
float density = getResources().getDisplayMetrics().density;
objectToDrawHeight *= density;
objectToDrawWidth *= density;
//Draw your object using the new (scaled) height and width
//If you are worried about how the object will look on different aspect ratio devices
// you can get the screen dimensions and use that ratio as a multiplier
// just as you did with density
Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
float screenDimensionX = display.getWidth();
float screenDimensionY = display.getHeight();
使用密度和可能的屏幕尺寸应该允许您绘制任何东西并保持正确缩放。使用 Canvas 时,假设一切都以像素为单位,并且您必须进行 dpi 转换。
关于android - 在 canvas.drawBitmap 中使用 dpi 而不是像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063928/
我试图在 ImageView 中显示图像的一部分。我用 canvas.drawBitmap() 试过了,但似乎显示了图像的错误部分。我的代码: 主要 Activity : this.drawC
我使用这样的 onDraw 方法显示图像: public void onDraw(Canvas canvas) { super.onDraw(canvas); Bitmap bac
我有一个 200x200 像素的位图。我想在我的 Canvas 上绘制位图的左上角 50x50px,坐标为 100,100,宽度和高度为 50px,方法是: drawBitmap(Bitmap bit
我正在使用 SurfaceView 编写 Android 游戏。 SurfaceView 获取的 Canvas 绘制了一个大小为 80x80 的 32 位 PNG 图像,带有 alpha 层。 dra
我正在编写一款 Android 游戏,我似乎遇到了在 Canvas 上绘图的性能问题。我的游戏有多个关卡,每个关卡中(显然)有不同数量的对象。 奇怪的是,在一个包含 45 张图像的关卡中,运行完美(几
如果我使用 drawBitmap() 在 View Canvas 上绘制位图,图像将被重新采样,这样图像中的 1 个像素将在屏幕上倾斜 1 个。在我拥有高像素密度的设备上,这意味着每个图像像素分布在
简单的乐趣 fun getCircleBitmap(bitmap: Bitmap, recycle: Boolean): Bitmap { val paint = Paint()
我是 Android 编码方面的新手。作为第一个项目,我决定移植 ZX Spectrum 游戏。我已经准备好了汽车的位图框架和按钮,它们将根据屏幕尺寸进行缩放。我已将图像放在“drawable”和“d
我的应用程序以连续循环的方式绘制到 Canvas 上,并在每个循环中重新评估可绘制对象的位置并循环它们以进行动画处理。我的问题是以下两种方法哪种更优越,为什么?我是一个初学者,所以我不知道如何对方法进
我必须在 map 上画一条线。由于特殊线条效果的要求,我不得不创建一个单独的位图并使用临时 Canvas 在该位图上绘制线条。现在当它完成绘制线条时,我将它渲染到主 Canvas 上。不幸的是,下面的
我正在尝试将位图叠加到另一个位图上,将其放置在用户触摸的位置。这是代码: public static Bitmap mergeImage(Bitmap base, Bitmap overlay, f
我最近遇到了一个问题 Android: Rotation of image around the center这是在Unconn的帮助下解决的。 然而,事实证明,这个解决方案——以及它的工作原理——确
我已经像这样为 DragShadowBuilder 覆盖了 onDragShadow @Override public void onDrawShadow(Canvas canvas) {
我有一个游戏,在其中一个关卡中,绵羊可能会被地雷炸毁。爆炸动画是通过在 512x515 png 文件中包含 4x4 爆炸图像阵列的 png 来控制的……见下文。 然后我使用以下代码为爆炸设置动画:
我一直在尝试在 Android 中制作一些 Sprite 动画(这是一项大学练习),但我遇到了一个奇怪的问题。它没有绘制我定义为源的矩形,而是恰好绘制了该矩形的一半。 这是我的绘图方法的代码 publ
我的程序在动态壁纸 Canvas 上绘制位图。它有效,但一段时间后图像变得非常糟糕(http://img855.imageshack.us/img855/9756/deviceq.png) 有什么想法
我正在扩展 SurfaceView 以创建全屏水平滚动背景。 我的问题是位图的质量。在绘图调用中用作 canvas.drawBitmap(bitmap, 0, 0, paint); 它的质量比原来的差
我正计划在我的游戏中实现一组新图形:普通圆圈。绘制的 Sprite (在本例中为圆圈)的数量从 2-3 开始,并且可以无限增加(可能)。不过,最大值可能在 60 左右。总共必须有 5 种类型的圆圈,每
我想绘制位图的子部分,但大小不同。如果大小大于位图中的源矩形,那么我希望位图的该部分平铺以填充目标区域。然而,它们并没有平铺,而是被拉伸(stretch)了。 我将所有变量设置如下: Bitmap b
我是 Android 新手,我有两张图片,一张是空图片,另一张是代表进度条的完整图片。 如何使用 canvas.drawBitmap 只绘制整个图像的一部分? 我不想每次都调整位图图像的大小。 最佳答
我是一名优秀的程序员,十分优秀!