How to make zoomable LinearLayout?
use this jar in your application
1.Create a new layout (R.layout.zoomableview) for Views that i want to apply the zooming functionality.
2.Place it inside ZoomView.
3.Then place the ZoomView to the main container where you want to show the zoomable view.
private ZoomView zoomView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoomable);
View v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.zoomableview, null, false);
v.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
zoomView = new ZoomView(this);
zoomView.addView(v);
main_container = (LinearLayout) findViewById(R.id.main_container);
main_container.addView(zoomView); }
Zoom with LinearLayout
No, sorry, there is nothing built in for zoom on normal widgets, AFAIK. WebView
and MapView
know how to zoom. Anything else you are on your own.
Android - zoom in/out RelativeLayout with spread/pinch
So I created a subclass of RelativeLayout
as described in the above mentioned topics. It looks like this:
public class ZoomableRelativeLayout extends RelativeLayout {
float mScaleFactor = 1;
float mPivotX;
float mPivotY;
public ZoomableRelativeLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ZoomableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ZoomableRelativeLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
protected void dispatchDraw(Canvas canvas) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(mScaleFactor, mScaleFactor, mPivotX, mPivotY);
super.dispatchDraw(canvas);
canvas.restore();
}
public void scale(float scaleFactor, float pivotX, float pivotY) {
mScaleFactor = scaleFactor;
mPivotX = pivotX;
mPivotY = pivotY;
this.invalidate();
}
public void restore() {
mScaleFactor = 1;
this.invalidate();
}
}
My implementation of the SimpleOnScaleGestureListener
looks like this:
private class OnPinchListener extends SimpleOnScaleGestureListener {
float startingSpan;
float endSpan;
float startFocusX;
float startFocusY;
public boolean onScaleBegin(ScaleGestureDetector detector) {
startingSpan = detector.getCurrentSpan();
startFocusX = detector.getFocusX();
startFocusY = detector.getFocusY();
return true;
}
public boolean onScale(ScaleGestureDetector detector) {
mZoomableRelativeLayout.scale(detector.getCurrentSpan()/startingSpan, startFocusX, startFocusY);
return true;
}
public void onScaleEnd(ScaleGestureDetector detector) {
mZoomableRelativeLayout.restore();
}
}
Hope this helps!
Update:
You can integrate OnPinchListener
for your ZoomableRelativelayout
by using ScaleGestureDetector
:
ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(this, new OnPinchListener());
And you are required to bind touch listener of Zoomable layout with the touch listener of ScaleGestureDetector:
mZoomableLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
});
Related Topics
Android: Get Facebook Friends List
How to Display Gif in React-Native Android App
Bufferoverflowexception When Building Application
Multiple Dex Files Define Lcom/Google/Android/Gms/Internal/Zzau
How to Create/Write File in the Root of the Android Device
How to Disable Crashlytics During Development
Finding the Dominant Color of an Image in an Android @Drawable
How to Show Popupwindow at Special Location
Don't Collapse Toolbar When Recyclerview Fits the Screen
Android: Autocomplete Textview Similar to the Facebook App
Nexus 5X Reverse Landscape Sensor Fix in a Android Camera Preview App
Elevation on Android Lollipop Not Working
Android Options Menu Icon Won't Display
Recycle-View Inflating Different Row :- Getting Exception While Binding the Data
How to Move Main Content with Drawer Layout Left Side
Android Design Considerations: Asynctask VS Service (Intentservice)
How to Read Properties Defined in Local.Properties in Build.Gradle