android layout - Why is my image clipped after scaling? -
i having strange problem. scale image and, while scaling works correctly, image gets clipped. tried different scale types - things changed never make work.
just clear, here's need solve: 1. have horizontalscrollview
around imageview
, scrollview
around horizontalview
. 2. scroll around (using scrollto
of both scroll views) and, upon event, zoom in. 3. i'd happen imageview
scale around current scroll position.
here's layout:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <scrollview android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:overscrollmode="never"> <horizontalscrollview android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:overscrollmode="never"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="3dp" android:scaletype="fitcenter" /> </horizontalscrollview> </scrollview> </framelayout>
and here's scaling code (originalwidth
/originalheight
calculated @ scale of 1
; targetview
points imageview
):
public synchronized void changescale(float newscalefactor) { this.scalefactor = math.max(min_zoom, math.min(newscalefactor, max_zoom)); if (targetview != null && originalwidth > 0) { int newwidth = (int)(originalwidth * scalefactor); int newheight = (int)(originalheight * scalefactor); onscalechanged(targetview, scalefactor, newwidth, newheight); } } public void onscalechanged(view targetview, float scalefactor, int newwidth, int newheight) { viewgroup.layoutparams layoutparams = targetview.getlayoutparams(); layoutparams.width = newwidth; layoutparams.height = newheight; // needed increase pane size (rather zoom within initial layout) targetview.setlayoutparams(layoutparams); // tell system recalculate layout targetview.requestlayout(); // needed specify center of scaling horizontalscrollview horizontalscrollview = (horizontalscrollview)targetview.getparent(); scrollview vertscrollview = (scrollview)horizontalscrollview.getparent(); // ~~~ pivot points wrong targetview.setpivotx(horizontalscrollview.getscrollx() * scalefactor); targetview.setpivoty(vertscrollview.getscrolly() * scalefactor); // needed actual zooming targetview.setscalex(scalefactor); targetview.setscaley(scalefactor); }; public void zoomin(float scaledelta) { changescale(scalefactor + scaledelta); } public void zoomout(float scaledelta) { changescale(scalefactor - scaledelta); }
question 1: how prevent clipping? can't find right combination of scaletype
, layout resizing.
question 2: when use setscalex
/setscaley
, should pivot calculated after applying new scale factor or renderer take care of automatically?
after updating scale need invalidate(), , requestlayout() views.
targetview.invalidate(); targetview.requestlayout();
i calculate scale differently images. try scale image view using matrix scale type. need know size of bound dpi.
// scaled dpi int boundboxindp = getresources().getdisplaymetrics().densitydpi * scalefactor // determine how scale: dimension requiring less scaling // closer side. way image stays inside // bound , either x/y axis touches bound neither go over. float xscale = ((float) boundboxindp) / newwidth; float yscale = ((float) boundboxindp) / newheight; float scale = (xscale <= yscale) ? xscale : yscale; // scale using our calculated scale targetview.setscalex(scale); targetview.setscaley(scale);
as second question pivot. need set center of visible scroll area. the scrollable area should increased when change image size since using fit_center
;
Comments
Post a Comment