Display screen size resolution density dimension Android
Get display, screen resolution - dimension Android phone development example source code.
Example:
HTC Desire HD
11 (4.3) Diagonal cm (in)
480×800 Resolution
85 (217) ppcm (PPI - Pixels per inch)
480 / 217 = 2.21 inch
800 / 217 = 3.69 inch
Math.sqrt( 2.21*2.21 + 3.69 * 3.69) = 4.3 inch - diagonal
//en.wikipedia.org/wiki/Pixels_per_inch
//en.wikipedia.org/wiki/Dots_per_inch
Typical screen widths:
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
Emulator skins in the Android SDK
//developer.android.com/guide/practices/screens_support.html
Display display = getWindowManager().getDefaultDisplay();
int nWidth = display.getWidth();
int nHeight = display.getHeight();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//get density per inch for example: 120 , 160 , 240
mXDpi = metrics.xdpi; // 160 The exact physical pixels per inch of the screen in the X dimension.
mYDpi = metrics.ydpi;
// density
int nDensity = metrics.densityDpi; // 160 screen density expressed as dots-per-inch
float mMetersToPixelsX = mXDpi / 0.0254f; // 1 inch == 0.0254 metre
float mMetersToPixelsY = mYDpi / 0.0254f;
// Resolution
// The total number of physical pixels on a screen.
int wPix = metrics.widthPixels; // 320 The absolute width of the display in pixels.
int hPix = metrics.heightPixels; // 480 The absolute height of the display in pixels.
int nWidthDisplay = (wPix < hPix)? wPix : hPix;
float nWidthScreenInInch = wPix / mXDpi; //320 / 160 == 2.0 in inch.
float nHeightScreenInInch = hPix / mYDpi; //480 / 160 == 3.0 in inch.
// for example:
// density 120 per inch
//width 320 pix / 120 dpi == width 2.66 inch
//height 480 pix / 120 dpi == height 4.0 inch
// density 240 per inch
//width 320 pix / 240 dpi == width 1.33 inch
//height 480 pix / 240 dpi == height 2.0 inch
Example:
HTC Desire HD
11 (4.3) Diagonal cm (in)
480×800 Resolution
85 (217) ppcm (PPI - Pixels per inch)
480 / 217 = 2.21 inch
800 / 217 = 3.69 inch
Math.sqrt( 2.21*2.21 + 3.69 * 3.69) = 4.3 inch - diagonal
//en.wikipedia.org/wiki/Pixels_per_inch
//en.wikipedia.org/wiki/Dots_per_inch
Typical screen widths:
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
Emulator skins in the Android SDK
//developer.android.com/guide/practices/screens_support.html
|
|
|
|
|
---|---|---|---|---|
Small screen |
QVGA (240x320) | 480x640 | ||
Normal screen |
WQVGA400 (240x400)
WQVGA432 (240x432) |
HVGA (320x480) | WVGA800 (480x800)
WVGA854 (480x854) 600x1024 |
640x960 |
Large screen |
WVGA800** (480x800)
WVGA854** (480x854) |
WVGA800* (480x800)
WVGA854* (480x854) 600x1024 |
||
Extra Large screen |
1024x600 | WXGA (1280x800)† 1024x768 1280x768 |
1536x1152 1920x1152 1920x1200 |
2048x1536 2560x1536 2560x1600 |
* To emulate this configuration, specify a
custom density of 160 when creating an AVD that uses a WVGA800 or WVGA854 skin. ** To emulate this configuration, specify a custom density of 120 when creating an AVD that uses a WVGA800 or WVGA854 skin. † This skin is available with the Android 3.0 platform |
396LW NO topic_id
AD
Další témata ....(Topics)
1.) Open DDMS via Menu Tools > Android > Android Device Monitor
2.) Select Device
3.) In DDMS click Menu Window > Show View > File Exlporer
4.) Device > Storage folder
5.) Emulator > data > data folder
2.) Select Device
3.) In DDMS click Menu Window > Show View > File Exlporer
4.) Device > Storage folder
5.) Emulator > data > data folder
Example have error code:
//developer.android.com/training/basics/fragments/creating.html
Try to change ArticleFragment.java
//developer.android.com/training/basics/fragments/creating.html
Try to change ArticleFragment.java
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.fragments;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
TextView articleText;
@Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
//
// // If activity recreated (such as from screen rotate), restore
// // the previous article selection set by onSaveInstanceState().
// // This is primarily necessary when in the two-pane layout.
// if (savedInstanceState != null) {
// mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
// }
//
// // Inflate the layout for this fragment
// return inflater.inflate(R.layout.article_view, container, false);
// }
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.article_view, container, false);
articleText = (TextView) rootView.findViewById(R.id.article);
return rootView;
}
@Override
public void onStart() {
super.onStart();
// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already been
// applied to the fragment at this point so we can safely call the method
// below that sets the article text.
Bundle args = getArguments();
if (args != null) {
// Set article based on argument passed in
updateArticleView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
// Set article based on saved instance state defined during onCreateView
updateArticleView(mCurrentPosition);
}
}
public void updateArticleView(int position) {
//TextView article = (TextView) getActivity().findViewById(R.id.article); //Error: article=null.
if (articleText != null)
articleText.setText(Ipsum.Articles[position]);
mCurrentPosition = position;
}
/* ERROR public void updateArticleView(int position) {
TextView article = (TextView) getActivity().findViewById(R.id.article);
article.setText(Ipsum.Articles[position]);
mCurrentPosition = position;
}*/
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current article selection in case we need to recreate the fragment
outState.putInt(ARG_POSITION, mCurrentPosition);
}
}
Landscape - portrait orientation change:
boolean mbOrientationLandscape = true;
if(mbOrientationLandscape ){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mbOrientationLandscape =false;
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mbOrientationLandscape =true;
}
Dil 4. ArticleFragment.java
V 1. dílu jsme se něco dozvěděli od XML souborech a typu procesoru pro správný běh Android Studia a emulátoru různých typů zařizení s Androidem.
V 2. dílu jsme rozebrali MainActivity.java
V 3. dílu jsme se zabývali HeadlinesFragment.java
V tomto dílu se podíváme na ArticleFragment.java soubor.
Používáme příklad i zip porojekt z https://developer.android.com/training/basics/fragments/creating.html Pozorně si jej nastudujte.
V 1. dílu jsme se něco dozvěděli od XML souborech a typu procesoru pro správný běh Android Studia a emulátoru různých typů zařizení s Androidem.
V 2. dílu jsme rozebrali MainActivity.java
V 3. dílu jsme se zabývali HeadlinesFragment.java
V tomto dílu se podíváme na ArticleFragment.java soubor.
Používáme příklad i zip porojekt z https://developer.android.com/training/basics/fragments/creating.html Pozorně si jej nastudujte.
package com.example.android.fragments;
// knihovna pro nižší verze Androidu
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
// extends Fragment - už nebude obsahovat funkci onCreate jako v Activity
// ale onCreateView
public class ArticleFragment extends Fragment {
// důležité pro uložení argumentu - argumentů (hodnot)
// pro obnovení předchozího stavu obsahu obrazovky
// např. při rotaci zařízení atd.
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
TextView article; // uložen do globální proměnné, v originale
// odchycen v updateArticleView() ale tam vracel NULL
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Když je activity znovu vytvořena (např. při rotaci zařízení),
// obnoví, v našem případě, text článku, jehož pozice
// byla uložena pomocí
// public void onSaveInstanceState(Bundle outState) viz níže
// důležité zejména pro dual-panel (dva panely vedle sebe)
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
// umístíme, aktivujeme příslušný layout
// zde je zajímavé, že layout můžete měnit.
// Např. při kliknutí na pložku 1 v HeadlinesFragment
// zde můžete ochytit pozici a dle toho zvolit
// příslušný layout, který chcete zobrazit ve fragmentu
// ALE pak si musíte pohlídat ID prvků, které bude ten JINÝ
// layout obsahovat
// Oproti originalu odchytíme TextView již zde, v originalu to vyhazovalo chybu
View rootView = inflater.inflate(R.layout.vnitrek, container, false);
article = (TextView) rootView.findViewById(R.id.article);
return rootView;
}
@Override
public void onStart() {
super.onStart();
// Při startu fragmentu, zkontrolujte, zda existují nějaké argumenty
// předané do fragmentu.
// OnStart() je právě to správné místo, kde to udělat,
// protože layout s jednotlivými elementy byl již
// naloděn - aktivován, a můžeme bezpečně použít metody,
// které potřebují, aby jednotlivá ID elementů layoutu byla již
// aktivní, použitelná a nevracela NULL, což by mělo za následek
// pád aplikace
Bundle args = getArguments();
if (args != null) {
// vypsaní obsahu článku pomocí předaného argumentu (pozice) z HeadlinesFragment.java
updateArticleView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
// vypsání článku dle pozice uložené např. při rotaci zařízení
// mCurrentPosition je definována (odchycena) v onCreateView
updateArticleView(mCurrentPosition);
}
}
/**
funkce která vypíše obsah článku do TextView.
Jako parametr int position je pozice položky,
na kterou bylo kliknuto v ListView v HeadlinesFragment.java
*/
public void updateArticleView(int position) {
// na rozdíl od Activity se ve Fragment používá k
// získání id ne jen findViewById()
// ALE getActivity().findViewById()
//Tento kod vracel article == NULL , PROTO bylo nutno odchytit TextView
// v onCreateView()
//TextView article = (TextView) getActivity().findViewById(R.id.article);
// vložení textu článku do TextView z Ipsum.java
// je to pole stringů, kde position je pozice stringu v poli
// static String[] Articles = {"","",""};
if (article != null)
article.setText(Ipsum.Articles[position]);
mCurrentPosition = position;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Uložení pozice článku - elementu, či jiných argumentů důležitých
// pro obnovení stavu v onCreateView() např. při rotaci zařízení
outState.putInt(ARG_POSITION, mCurrentPosition);
// TIP: zde můžeme vždy při rotaci zařízení podstrčit náhodnou pozici
// článku pomocí
// randomNum = minimum + (int)(Math.random() * maximum);
// a vytvořit tak zábavnou hru, například pro náhodné
// vypsání přísloví, či nějakého fyzikálního zákona atd.
// Stačí pak aby uživatel jen pootočil zařízení od 90° a zpět,
// k vypsání nové položky
}
}
On Android device is path to *.apk like this example:
How get package *.apk path on device dynamically Android example
/data/app/cz.okhelp.my_package.apk
How get package *.apk path on device dynamically Android example
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String sPackagePath = getPackageResourcePath();
}
Editace: 2012-07-04 15:25:45
Počet článků v kategorii: 396
Url:display-screen-dimension