Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Android RecyclerView Zoom?

android java recyclerview zoom api 10+

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts

Posted 29 July 2015 - 03:08 PM

Hello everyone. 

 

What is the best approach to implementing zoom functionality for a RecyclerView? 

 

For API 11+, there is View.setScaleX () and View.setScaleY (); I have not tried them yet, but I would prefer to target API 10. This is because API 10+ covers over 99% of all devices on the play store, while increasing it to 11+ will cover only 94% (at least as of my last time checking; this is according to the AndroidStudio "New Project" dialog). 

 

The best workflow I can think of: 

  1. Override onTouchEvent () and dispatchDraw (), using ScaleGestureDetector and Canvas.scale () (as done here, answer by Schnodahipfe) 
  2. For the parameters to Canvas.scale (), keep pivot Y at Canvas.getHeight () / 2, and let the RecyclerView take care of the vertical scrolling. 
  3. Whenever RecyclerView vertical scroll is either 0 or at maximum, override scrolling to vary pivot Y instead of the default scroll position. 
  4. When pivot Y is at minimum or maximum, go back to default scroll behavior so that the RecyclerView can give that end-of-scroll feedback to the user. 

But it is a little complicated workflow. What is the best way to zoom for RecyclerView for API 10+? 

 

Thanks. 

 

 

As for what this is for, I want to write a handwritten-note-taking app. 


Regards,
RR

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts

Posted 14 August 2015 - 11:34 AM

So here is what I ended up doing:

  • For each RecyclerView row, make such a layout: 
    <HorizontalScrollView 
        ... > 
    
        <com.mydomain.projectname.NotebookPageView 
            android:id="@+id/notebook_page" 
            ... /> 
    
    </HorizontalScrollView> 
    
     
     
  • That way, there is vertical scrolling by the RecyclerView and horizontal scrolling by the HorizontalScrollView. 
     
  • On pinch-zoom gesture, call some NotebookPageView method that changes the zoom level. This will make NotebookPageView.onMeasure () return a larger size, because everything is zoomed in. So the whole NotebookPageView is larger, rather than just Canvas.scale () 
     
  • Notify the RecyclerView that the sizes / layout changed; these can be done with either: 
    NotebookView notebook = ... 
    ... NotebookView is a subclass of RecyclerView ... 
    
    ... 
    
    notebook.setAdapter (notebook.getAdapter ()); 
    
    // Will need to save and restore the scroll position, though 
    
    
    This will reset the whole list. 

    Or perhaps the following could do the job as well, but I have not tried it with this task yet: 
    public class NotebookPageView extends View { 
    
    ... 
    
        @Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) { 
            super.onMeasure (widthMeasureSpec, heightMeasureSpec); 
            
            ... do measuring ... 
            
            setMeasuredDimension (width, height); 
            
            if (getWidth () != getMeasuredWidth ()) 
                requestLayout (); 
        } 
    
    ... 
    
    } 

Regards,
RR




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download