Android control Combination Application 7 (ending with source code)

Source: Internet
Author: User

Next, let's take a look at the image section on Weibo.

In the previous article, we used webview to process the image display in the URL and basically achieved the goal. However, there are still one or two details to be processed to improve the user experience.

First, you need to display a background image during the image download process, so that the user can see the image in the crowdsourced security testing area, rather than a blank area.

Second, after the image is taken down, the display effect should be dynamically adjusted based on the image size, so that the webview cannot be too large and the image is too white.

 

Therefore, we inherit a picturewebview from webview to solve the above two problems.

First, we need to implement the webview. picturelistener interface for this picturewebview.

Public abstract void onnewpicture (webview view, picture)

This method is used to send a notification when the image changes. The Code is as follows. The picture is a member variable of the picture type.

 @ Override <br/> Public void onnewpicture (webview view, picture) {<br/> If (picture! = NULL) {<br/> This. picture = picture; <br/> displaymetrics dm = getcontext (). getresources (). getdisplaymetrics (); <br/> int width = (INT) (picture. getwidth () * DM. density); <br/> int Height = (INT) (picture. getheight () * DM. density); <br/> setpicturelistener (null); <br/> viewgroup. layoutparams Lp = getlayoutparams (); <br/> LP. width = width; <br/> LP. height = height; <br/> setlayoutparams (LP); <br/>}< br/>}

 

Note that the layout of the control has been adjusted according to the picture size. Because each of our picturewebview instances only targets image URLs in a microblog, other printable elements in webview are not concerned here.

 

Next, to better deal with the second problem, we will rewrite the ondraw () method to plot the background and image. The Code is as follows:

 @ Override <br/> protected void ondraw (canvas) {<br/> super. ondraw (canvas); <br/> If (picture = NULL) {<br/> drawable background = This. getbackground (); <br/> If (background! = NULL) {<br/> background. setbounds (0, 0, background. getintrinsicwidth (), background. getintrinsicheight (); <br/> background. draw (canvas); <br/>}< br/>}

 

In this way, a custom webview is implemented. Modify the webview control in the blogview. xml layout file to picturewebview. The Code is as follows:

 <COM. wenbin. test. picturewebview Android: Id = "@ + ID/profileimage" <br/> Android: layout_width = "48dp" <br/> Android: layout_height = "48dp" <br/> Android: scrollbars = "NONE" <br/> Android: layout_alignparentleft = "true" <br/> Android: Background = "@ drawable/portrait"> <br/> </COM. wenbin. test. picturewebview>

 

 

Run the program to display the background image before downloading the Avatar or Weibo image. After downloading the image, the layout is automatically adjusted and no useless Border exists. See Android control Combination Application 1.

 

Conclusion: I wanted to write it in an article, but it was too long, so it was divided into many parts. I didn't expect csdn's hardworking editor to cover the "recommendation" chapter when the first article was published, and then I started to stay up late and rush to work. In the code, I did not pay attention to algorithms or other things, nor did I reach the commercial level. I used the android controls flexibly by imitating the Sina Weibo client interface, at the same time, we can see the flexibility that the android framework brings to developers.

 

Add all the source code and resources for your reference (the program was tested on Motorola milestone Android 2.1, some of which were sourced from Sina Weibo and used only for learning and testing ).

 

Click here to download the source code

 

-- Welcome to reprint, reprint please indicate the source of http://blog.csdn.net/caowenbin --

 

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.