Question

I have a subclass of HorizontalScrollView(parent) which is managing multiple child views inside it. Each View consists of a rectangle, few lines below it and TextView(See image). I have added pinch zoom gesture in it so when user pinch zoom all the child views which are between the two fingers are scaled according to scale factor. So for example my first finger is on Jan and second finger on March and I am doing pinch in, all three views (Jan,Feb and March) are scaling. I am scaling only in x direction. Also I have defined minimum and maximum width of a particular view.

Now I want to extend this feature. I want to include some kind of hierarchic feature in it. So if all the views in a particular hierarchy are in minimum width and if I try to further scale them down(by pinch zoom), then all these views must be replaced by a different View.

For example, say Jan,Feb and March is in one hierarchy and by pinch zoom I scaled down them to their minimum width. If I further try to scale it down, then in place of these three views there comes another View.

The purpose of this feature is that say I have months of a 10 year in HorizontalScrollView. So I have total 120 views(10 years*12 months). So if I scaled down all months of a year and further try to scale them down, then I should replace 12 month views by a year view.

Hopefully I am clear in my question. Can anyone tell me how to do it.

EDIT: I am storing scale factor of the child views in an array state[] and added two methods in my code

     boolean checkstate()
     {
         if(state[6]<=0.3f &&state[7]<=0.3f &&state[8]<=0.3f  )
             return true;
         else
             return false;
     }


    void changewithparent()
    {
        LinearLayout parent=(LinearLayout)takeparent();
        parent.removeViewAt(6);
        parent.removeViewAt(6);
        parent.removeViewAt(6);

        //parent.requestLayout();
        View v=kk.getView(6, null, parent);
        v.setBackgroundColor(Color.GREEN);
        parent.addView(v, 0);
     }

Also in ScaleListener I have added the following lines

    //start and end are the indices of child views which are scaling
for(int i=start;i<=last;i++)
            {
                LinearLayout ll=(LinearLayout)pp.getChildAt(i);
                DrawView view=(DrawView)ll.findViewById(R.id.drawview);

                view.mScaleFactor=mScaleFactor;
                view.invalidate();
                state[i]=view.sf;   updating the scale factor in parent by taking the value from child view
                change= checkstate();
                if(change==true)
                    changewithparent();

            } 

But my application is crashing whenever the minimum limit of child view is reached and I am trying to replace them with parent View

enter image description here

Was it helpful?

Solution

Since you know how wide your year view will be based on the min width of the months, could you include a year view with it's visibility set to GONE, then swap visibility of the months and Year as needed?

To make it a bit more dynamic, you could have an object for each view which stores it's state, which in this case would include something to tell you if it's at minimum or maximum width. When scaling, you would check each object for state changes. If your state check passes some rule (e.g. 12 month objects at min width, or a 10 year object above max width), you would request a layout which would then replace the views with the appropriate new views, and update your state objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top