开发者

OnScrollChanged() called from scrollTo(x,y) but doesn't update view sizes inside the ScrollView

I have been writing a code to create a magnifying glass effect on ImageViews inside a scrollView. (I want to change the size of the ImageViews in the center to be bigger than the ones on the sides) I inherited HorizonalScrollView and overrode onScrollChanged(). In OnScrollChanged I changed the ImageViews size according to its location on the screen. (I changed the size with LinearLayout.LayoutParams) Everything is working, but when I tried to move the scrollView to the center, via scrollTo() in a scrollview.post() runnable, the scrollview moves to the specified location and onScrollChanged() is called but the ImageViews inside the scrollView don't change their size prop开发者_JAVA技巧erly (they change their size randomly). Only when I touch the screen and move the scrollView with my finger, the views change their size properly.

The main code :

public class Scroll extends Activity implements ScrollViewListener{
     /** Called when the activity is first created. */
 LinearLayout container;
 ObservableScrollView myScroll;
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

    container=(LinearLayout)findViewById(R.id.linearLayout1);
  myScroll=(ObservableScrollView)findViewById(R.id.horizontalScrollView1);
  myScroll.setScrollViewListener(this);
   for(int i=0;i<30;i++)
   {
        ImageView img=new ImageView(this);
        img.setImageResource(R.drawable.icon);
        container.addView(img);
   }


      myScroll.post(new Runnable() {
      @Override
       public void run() {
          myScroll.scrollBy(500,0);

    } 
    });







   }
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
        int oldx, int oldy) {


    int count=container.getChildCount();
     int [] xy={0,0};
        container.getChildAt(count-1).getLocationOnScreen(xy);


            int x1=xy[0];



        for(int i=0;i<count;i++)
            AdjustSize((ImageView)container.getChildAt(i));






}



        private void AdjustSize(ImageView image)
           {




            if(image.getVisibility()==View.VISIBLE){
               int [] xy={0,0};
                image.getLocationOnScreen(xy);
                int x1=xy[0];
                if((x1>10)&&(x1<790))
                {
                double xSize=220-(0.5*220*((Math.abs(x1-300)))/400);
                int xInt=(int)xSize;


                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(xInt,xInt);




                lp.gravity=Gravity.CENTER;   

                     image.setLayoutParams(lp);
                }
                     // image.setLayoutParams(new LinearLayout.LayoutParams(xInt, xInt));

               }


                }

          }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜