android - How to scale imageview / bitmap and pixels versus dips -


i have imageview on linearlayout.

i imageview scale bitmap holds, takes max amount of space in linear layout, still keeps proper image scale.

  public static void sharedutilscaleimage(imageview view)   {       drawable drawing = view.getdrawable();       //--       bitmap bitmap = ((bitmapdrawable)drawing).getbitmap();       int bitmapwidth = bitmap.getwidth();       int bitmapheight = bitmap.getheight();       int widthparent = view.getwidth();             int heightparent = view.getheight();             //--       float density = 1;       if (true) {         density = micapp.getcontext().getresources().getdisplaymetrics().density;       }       //--       float xscale = ((float) widthparent * density) / bitmapwidth;        float yscale = ((float) heightparent * density) / bitmapheight;       float minscale = math.min(xscale, yscale);         //--        matrix matrix = new matrix();       matrix.postscale(minscale, minscale);       //--       bitmap scaledbitmap = bitmap.createbitmap(bitmap, 0, 0, bitmapwidth, bitmapheight, matrix, true);       bitmapdrawable result = new bitmapdrawable(scaledbitmap);       view.setimagedrawable(result);         } 

for reference, found of above code here: htttp://stackoverflow.com/questions/8114085/how-to-create-white-border-around-bitmap

however, find above bit puzzling.

  • i store bitmaps in drawable-hdpi
  • the bitmap .getheight/.getwidth returns actual pixels (and not density altered pixels)
  • however view .getheight / .getwidth returns pixels smaller actual pixel usage on phone. need multiply them density actual pixels.

why difference in values returned?

i bitmaps return actual pixel size. read elsewhere require them placed in "res/drawable-nodpi", seems additional inconsistency?

for reference code places bitmap in imageview, , imageview inside linear layout looks this:

imageview.setimageresource(picids[position]);                        imageview.setscaletype(imageview.scaletype.center_inside);         layoutparams lp = new layoutparams(layoutparams.match_parent, layoutparams.match_parent);         imageview.setlayoutparams(lp);                 //-- linearlayoutinner.setgravity(gravity.center_horizontal|gravity.center_vertical); linearlayoutinner.addview(imageview);        

seems want set imageview auto adjust bounds: http://developer.android.com/reference/android/widget/imageview.html#setadjustviewbounds(boolean) , scale type fit_center

bitmap has getscaledwidth & getscaledheight give adjusted dimensions (pass in display metrics resources) view's getwidth & getheight should give pixel values of size, if parent has padding, match_parent / fill_parent match parent size minus padding (or margin).


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -