Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Pixel to density independent pixel to pixel conversion

Physical pixel (px)
Density independent pixel (dp)
Dots per inch (dpi) .. physical pixels per inch
px = dp * (dpi / 160)

public static int dpToPx(int dp)
{
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

public static int pxToDp(int px)
{
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

396LW NO topic_id




AD

Další témata ....(Topics)


364

layout-sw600dp values-sw600dp Android example of use | layout-sw600dp-values-sw600dp-android-example-of-use


Why the app selects data from basic layout folder if smallest width is higher then the number in folder name?
Example 1
layout-sw600dp values-sw600dp (smallest width sw for data usage from this folder is 600dp density independent pixel!!!!!)
Device screen resolution is 1200 x 900 px (pixel) Wow, app to be select data from sw600dp folder! Realy?
DPI of device screen - dot per inch (pixel per inch) is 480 pixel it is wery important number!


  1. App selects smallest dimension of screen. In our case 900 px
    Medium screen have 160 dpi (The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen.).


  2. App calculate ratio 480 / 160 = 3 (The conversion of dp units to screen pixels: px = dp * (dpi / 160))


  3. App calculate smallest dimesnion of screen in dp 900 / 3 = 300 dip or dp (density independed pixel).


  4. App selects data from basic values and layout folder because sw600dp is greater than 300dp.





In our case smallest dimension of screen must be at least 1800 real - physical pixels (1800 px / 3 ratio(dpi/160) = 600 dp (dip density independend pixels) to be used data from folders values-sw600dp and layout-sw600dp.


Example 2 see Example 1 abouve
Device: Nexus 7 (2012) selected from Android Studio tool layout editor
Resolution: 800x1280 px
DPI: tvdpi (approximately 213dpi)
Ratio: 1.33 (213 / 160)
Smallest width in px: 800
Convert px to dp: 601.5 (800 / 1.33)
Result:Smallest width is 601.5dp The App to be used data from folders values-sw600dp and layout-sw600dp.
144

Compressing a bitmap to JPG format Android example | compressing-a-bitmap-to-jpg-format-android-example


Codec, compress(), CompressFormat, quality, decodeByteArray(), Android example source code with image.

public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
			int quality) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		src.compress(format, quality, os);

		byte[] array = os.toByteArray();
		return BitmapFactory.decodeByteArray(array, 0, array.length);
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			
			canvas.drawColor(Color.GRAY);
            
                        //  you need to insert some image flower_blue into res/drawable folder
			Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
                        // Best of quality is 80 and more, 3 is very low quality of image 
			Bitmap bJPGcompress = codec(b, Bitmap.CompressFormat.JPEG, 3);
                   // get dimension of bitmap getHeight()  getWidth()
		   int h = b.getHeight();
           
           canvas.drawBitmap(b, 10,10, paint);
           canvas.drawBitmap(bJPGcompress, 10,10 + h + 10, paint);
           
		}

	}
}



android/jpg-compression-android.png
270

How to add or remove widgets home screen android 4 | how-to-add-or-remove-widgets-home-screen-android-4


Video tutorial
How to add or remove widgets home screen Android 4.