android position view inside layout -
my activity has background image 1280x800 pixels. set using android:scaletype="centercrop"
.
there's flagstaff depicted on background image , need position image ("flag") above flagstaff.
if device's screen dimension 1280x800, "flag"'s position (850, 520). screen size can vary , android scales , shifts background image accordingly centercrop
flag. hence need assign somehow scale , shift "flag" image make placed nicely above flagstaff.
i have examined imageview.java , found scaletype used set private matrix mdrawmatrix
. have no read access field it's private.
so, given
@override public void ongloballayout() { imageview bg = ...; imageview flag = ...; int bgimagewidth = 1280; int bgimageheight = 800; int flagposx = 850; int flagposy = 520; // should here place flag nicely? }
you can see size of screen (context.getresources().getdisplaymetrics().widthpixels
, context.getresources().getdisplaymetrics().heightpixels
;) , calculate visible image, example (haven't tested should idea):
private void placeflag(context context) { imageview bg = new imageview(context); imageview flag = new imageview(context); int bgimagewidth = 1280; int bgimageheight = 800; int flagposx = 850; int flagposy = 520; int screenwidth = context.getresources().getdisplaymetrics().widthpixels; int screenheight = context.getresources().getdisplaymetrics().heightpixels; //calculate proportions between width of bg , screen double widthscale = (double) bgimagewidth / (double) screenwidth; double heightscale = (double) bgimageheight / (double) screenheight; //see real scale used, maximum between 2 values because using crop double realscale = math.max(widthscale, heightscale); //calculate position flag int flagrealx = (int) (flagposx * realscale); int flagrealy = (int) (flagposy * realscale); }
also, should doing in method ongloballayout
, in oncreate()
or inside constructor if want custom view.
Comments
Post a Comment