Android supports Scroll display of large charts

Source: Internet
Author: User
Tags gety

Problem:

I have a relatively large image, for example, the length and width are twice the size of the screen. The function I want to achieve is to first display the picture in the center, because the picture is too big, obviously only a part can be displayed, then, you can drag the image to achieve smooth scrolling (no trace of scrolling and refreshing can be seen ).

Like google Maps, if mapView is used, you can drag the entire map on the screen. However, because the map has a large amount of information, if one drag is too fast, the screen will display some refresh marks temporarily (gray grids ).

If you want to use mapView to load an existing image, but it is not successful, then the Srollview control and the most commonly used imageView are still not successful.

 

Solution:

After a large amount of information, you can solve the problem by using imageView with onTouch events.

Key code:

The imageView control in the layout file is as follows:

[Html] <span style = "font-size: 16px;"> <ImageView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/img"
Android: scaleType = "center"
Android: background = "# fff"
Android: src = "@ drawable/picName"
/>
</Span>
<Span style = "font-size: 16px;"> <ImageView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/img"
Android: scaleType = "center"
Android: background = "# fff"
Android: src = "@ drawable/picName"
/>
</Span>

 


The main code in the Activity file is as follows:

 

[Java] <span style = "font-size: 16px;"> @ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
 
SetContentView (R. layout. xml_name_layout );
 
Final ImageView switcherView = (ImageView) this. findViewById (R. id. img );
 
SwitcherView. setOnTouchListener (new View. OnTouchListener (){
 
Public boolean onTouch (View arg0, MotionEvent event ){
 
Float curX, curY;
 
Switch (event. getAction ()){
 
Case MotionEvent. ACTION_DOWN:
Mx = event. getX ();
My = event. getY ();
Break;
Case MotionEvent. ACTION_MOVE:
CurX = event. getX ();
CurY = event. getY ();
SwitcherView. scrollBy (int) (mx-curX), (int) (my-curY ));
Mx = curX;
My = curY;
Break;
Case MotionEvent. ACTION_UP:
CurX = event. getX ();
CurY = event. getY ();
SwitcherView. scrollBy (int) (mx-curX), (int) (my-curY ));
Break;
}
 
Return true;
}
});
 
}
</Span>
<Span style = "font-size: 16px;"> @ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

SetContentView (R. layout. xml_name_layout );

Final ImageView switcherView = (ImageView) this. findViewById (R. id. img );

SwitcherView. setOnTouchListener (new View. OnTouchListener (){

Public boolean onTouch (View arg0, MotionEvent event ){

Float curX, curY;

Switch (event. getAction ()){

Case MotionEvent. ACTION_DOWN:
Mx = event. getX ();
My = event. getY ();
Break;
Case MotionEvent. ACTION_MOVE:
CurX = event. getX ();
CurY = event. getY ();
SwitcherView. scrollBy (int) (mx-curX), (int) (my-curY ));
Mx = curX;
My = curY;
Break;
Case MotionEvent. ACTION_UP:
CurX = event. getX ();
CurY = event. getY ();
SwitcherView. scrollBy (int) (mx-curX), (int) (my-curY ));
Break;
}

Return true;
}
});

}
</Span>

 


The implementation result is as follows:

Source image
 

 
Effects in simulators
 

 

 



From Peking University-Google Android lab
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.