How to make the Virtual Machine display the same as the real machine
Original article, if reproduced, please indicate the source: http://blog.csdn.net/yihui823/article/details/6734341
Basic Concepts
1. PX (pixels): pixel
Is the basic element of the drawing. The screen resolution is pixel. If the resolution of a mobile phone is 480*800, the mobile phone is 480 pixels wide and 800 pixels long. This pixel is fixed. If you draw a straight line from 0 to 240,400 in pixels, It is a straight line from the upper left corner to the middle of the screen.
2. DPI (dots per inch): pixel density
. Number of points or lines printed per inch.
DPI is the unit of recording on printing, meaning the number of dots per inch that can be printed on each inch ). However, with the rapid development of digital input and output devices, most people use DPI to describe the resolution of digital images, but more rigorous people may note that the dot calculated during printing) pixel is not the same as that of a computer monitor. Therefore, more professional people use pixel per inch to indicate the resolution of digital images to distinguish them.
It is easy to understand how many pixels each fixed size has. For example, the smaller the pixel size, the higher the pixel density. High density means delicate pictures. The same diagonal line is displayed on devices with high density, so it is not easy to see sertices. In devices with low density, sertices are easily displayed.
Let's look at the two figures below:
PIX-001
PIX-002
For the same image size, the pixel density of the first image is large (50*50 pixels), and the pixel density of the second image is small (5*5 pixels). Does it look completely different.
When creating a virtual machine in Android, there is a parameter: density. This and DPI should be a concept.
3. Dip (device independent pixels): Device Independent pixel.
A pixel in an abstract sense. A program uses it to define interface elements. As a unit unrelated to the actual density, it helps programmers build a layout scheme (width, height, and position of the interface element ).
DIP is an abstract concept of Android. What is his main purpose? Why is dip recommended by Google for screen layout?
Lab 1
Same dip Display Effect
We use a dip button to see the effect.
To facilitate comparison, we place a custom view under the button.
XML file of layout:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" ><com.study.about.dip.ViewForShow android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><Button android:id="@+id/btn" android:layout_width="240dip" android:layout_height="320dip" android:text="Button"/></FrameLayout>
Custom view code:
package com.study.about.dip; import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.util.TypedValue;import android.view.View; public class ViewForShow extends View { private Paint mPaint = new Paint(); public ViewForShow(Context context) { super(context); } public ViewForShow(Context context, AttributeSet attrs) { super(context, attrs); } public ViewForShow(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void draw(Canvas canvas) { mPaint.setColor(Color.RED); canvas.drawLine(0, 0, 120, 160, mPaint); mPaint.setColor(Color.GREEN); canvas.drawLine(0, 320, 120, 160, mPaint); mPaint.setColor(Color.YELLOW); canvas.drawLine(240, 0, 120, 160, mPaint); mPaint.setColor(Color.BLUE); canvas.drawLine(240, 320, 120, 160, mPaint); }}
The function of the code is to draw four straight lines and compare them with absolute pixels. Then, place a 240*320 DPI button on the upper line. Let's look at the relationship between DPI and pixels.
We start different virtual machines to compare the display results.
Comparison 1:
We will first compare the situations where the ratio of the screen length to width is the same and the resolution is different.
Virtual Machine 1:
PIX-003
The result is as follows:
PIX-004
Virtual Machine 2:
PIX-005
The result is as follows:
PIX-006
We can see that the two display buttons are displayed in the same proportion on the screen. The four colored straight lines are drawn by absolute pixels, And the buttons are drawn by dip. When density = 120, 240dip = 180px; when density = 160, 240dip = 240px.
Basically, we can draw a conclusion:
For the same dip, the relative width of the screen is the same on the screen with different resolutions. The relative width can be understood as the percentage of a widget's screen.
When density = 160, 1dip = 1px
We can get this formula:
Density: 160 = px: dip
However, formulas are only used for calculation. The actual intention should be that if the screen control is set with DIP pixels, the relative size of the control will remain unchanged regardless of the screen size. This is what Google thinks, "It has nothing to do with devices.
To be continued...