Android的 - 借鉴不同设备 [英] Android - Drawing on the different devices

查看:123
本文介绍了Android的 - 借鉴不同设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序创建一个画布,并添加一些关于它的位图。问题是,这些对象添加,为什么触摸屏幕。所以在一个屏幕上它们出现在不同的相同的中间,但它们在像素位置是不同的。

In my app I create a canvas and add some bitmaps on it. The problem is that the objects are adding why touch the screen. So on one screens they appear on the middle on different the same, but their position in pixels are different.

我的意思是,我得到了平板电脑和智能手机。当我触摸一个对象出现两个设备(多人),但它不能在同一个地方,因为它的x和y传递的位置。

I mean that I got tablet and smartphone. When I touch one the object appear on both devices (multiplayer) but its not in the same place, because its passing the position by x and y.

如果有人明白我的意思,你能帮帮我吗?
也许它必须有共同之处计数的比率。

If someone understand what I mean, can you help me? Probably it must have something common with counting the ratio.

推荐答案

我猜你遇到的问题是,屏幕是不同的分辨率和你逝去的像素数据。您将需要使用DP值,并在发送之前转换DP到像素值。在接收设备上,您将需要转换被送回的像素​​值DP在指定设备上。使用下面的方法来转换。

I am guessing the problem you are having is that the screens are different resolutions and you are passing pixel data. You will need to use dp values and before sending them convert the dp to pixel values. On the receiving device you will need to convert the pixel values being sent back to dp on the given device. Use the methods below for the conversion.

要转换DP到像素:

    final float scale = getResources().getDisplayMetrics().density;
    int pixelValue = (int) (DESIRED_DP_VALUE * scale + 0.5f);

要转换像素DP:

    final float scale = getResources().getDisplayMetrics().density;
    int dpValue = (int) ((DESIRED_PIXEL_VALUE) - 0.5f / scale);

要getDisplayMetrics()的调用。密度是会给你根据当前设备上的刻度值。在DP值,就是要独立的密度

The call to getDisplayMetrics().density is what will give you a scale value based on the current device. The dp value is meant to be density independent.

这篇关于Android的 - 借鉴不同设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆