Images in ScrollView in android

前端 未结 3 956
滥情空心
滥情空心 2020-12-01 23:03

In my app I want to place a .png file and I want it to be viewed as a scrolled image at both landscape and portrait modes, please, suggest a code or example....

3条回答
  •  离开以前
    2020-12-01 23:43

    A simple solution is to scroll a container which contains an ImageView much bigger than the container:

    
    
    
        
    
            
            
        
    
    
    

    And then use code to scroll it:

    public class StartActivity extends Activity {
        private LinearLayout container;   
        private int currentX;   
        private int currentY; 
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
            container = (LinearLayout) findViewById(R.id.Container);
            container.scrollTo(220, 400);
        }
    
        @Override  
        public boolean onTouchEvent(MotionEvent event) { 
          switch (event.getAction()) { 
              case MotionEvent.ACTION_DOWN: { 
                  currentX = (int) event.getRawX(); 
                  currentY = (int) event.getRawY(); 
                  break; 
              } 
    
              case MotionEvent.ACTION_MOVE: { 
                  int x2 = (int) event.getRawX(); 
                  int y2 = (int) event.getRawY(); 
                  container.scrollBy(currentX - x2 , currentY - y2); 
                  currentX = x2; 
                  currentY = y2; 
                  break; 
              }    
              case MotionEvent.ACTION_UP: { 
                  break; 
              } 
          } 
            return true;  
        } 
    }
    

    Here many improvements can be made such as limit the scrolling range etc... The other way is to control ImageView's matrix... Then you can load an image into a bitmap and draw a portion of it on canvas etc

提交回复
热议问题