After learning Javase, now began to continue my Android life, before learning Javase, through the UI interface to do the simulation game "snake" and the basic version of the calculator, now learning Android close to have 20 days to start making analog phone assistant download, make phone assistant download , the key is to practice the ListView, and some of the related optimizations for the ListView, as well as some bugs and fixes resulting from the optimization.
This time needs to use HFS external software, to solve the problem of network, 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/73/6D/ Wkiom1x9vpoycsc0aaeplbbxhiu884.jpg "title=" Hfs.png "alt=" wkiom1x9vpoycsc0aaeplbbxhiu884.jpg "/> and some pictures for download time display. such as mobile phone QQ,, and other software,
Download the software when the need to download is time, because our main main thread can not do time-consuming work, so when the download needs to download in the sub-thread, the need to use the asynctask here. The Doinbackground in Asynctask is downloaded in a sub-thread, Onprogressupdate, OnPostExecute (String), and OnPostExecute are all operating in the main thread. Onprogressupdate is half of the download in Doinbackground, want to operate on the main thread, OnPostExecute is to be downloaded before execution, OnPostExecute (String) is downloaded after the operation.
Doinbackground (String) We need to transfer the download path during the next load.
The download uses the URL, for example: url url = new URL (imageUrl), the first time you start to open the software, the entire page layout, and just start our software icon, log. All need to download, download the image used to bitmapfactory, for example: Bitmap bitmap= Bitmapfactory.decodestream (OpenStream); After a single download is complete, you need to change it in the page, because the main thread cannot make a series of changes to the page, so you need to download the bitmap in Doinbackground. Return to OnPostExecute, in OnPostExecute can do a series of operations on the main thread, image.setimagebitmap (result), so that the beginning of the basic page can be completed,
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/6F/wKiom1X9aCGSue_kAAcWigzwBUk090.jpg "title=" Image_ 02.png "alt=" Wkiom1x9acgsue_kaacwigzwbuk090.jpg "/>
These open software is the most basic display, the icon and data are from the network, each time the slide will be constantly downloaded icon, downloaded the icon he will again in the download, the experience is very bad. So we have to save the bitmap after downloading the icon, when it has been downloaded do not download, directly from the container to take. For example: Bitmap Bitmap = imagebitmaplist.get (position), save when the picture Bitmap is downloaded, but it takes time for us to download the process. So in the present if still fast up and down sliding or the same will cause the picture to be downloaded more than once, solve: because we each time the download will be new out of asynctask to download our images, for example: myasynctask asynctask = new Myasynctask (image,position); So we let the new Asynctask perform conditional execution and save the asynctask corresponding to each line. Each time you want to be under the time to look at the container inside the asynctask is there, if there is a description is being downloaded, we will not have to download the corresponding. For example:
Bitmap Bitmap = imagebitmaplist.get (position);
if (bitmap==null) {
Myasynctask Asynctask = asynctasklist.get (position);
if (asynctask==null) {
Asynctask = new Myasynctask (image,position);
Asynctask.execute (Imageurllist.get (position));
Asynctasklist.put (position, asynctask);
}
} else {
Image.setimagebitmap (bitmap);
}
This way you can avoid downloading icons multiple times while downloading.
The ListView is optimized to create a new view each time it slides up and down. So to re-use the view, you can replace it with a vanishing view Convertview to create a new view. So Convertview can be judged, if Convertview is null, we create a new view. If it is not empty, it will be directly used to reuse, this can solve the problem of resources, will not cause waste of resources. For example:
View inflate =null;
if (convertview==null) {
Layoutinflater layoutinflater = Getlayoutinflater ();
Inflate = layoutinflater.inflate (r.layout.layout, NULL);
} else {
Inflate=convertview;
}
As with view, every time we have a statement at the beginning of the statement
ImageView image = (ImageView) Inflate.findviewbyid (r.id.image);
ProgressBar progressBar1 = (ProgressBar) Inflate.findviewbyid (R.ID.PROGRESSBAR1);
Button button1 = (button) Inflate.findviewbyid (R.id.button1);
TextView Text = (TextView) Inflate.findviewbyid (R.ID.TEXTVIEW1);
Several components can also be reused. Put it all in one class and take it out when you need it, for example:
View inflate =null;
Myview Myview =null;
if (convertview==null) {
MyView = new MyView ();
Layoutinflater layoutinflater = Getlayoutinflater ();
Inflate = layoutinflater.inflate (r.layout.layout, NULL);
Myview.image = (ImageView) Inflate.findviewbyid (r.id.image);
Myview.progressbar1 = (ProgressBar) Inflate.findviewbyid (R.ID.PROGRESSBAR1);
Myview.button1 = (Button) Inflate.findviewbyid (R.id.button1);
Myview.text = (TextView) Inflate.findviewbyid (R.ID.TEXTVIEW1);
Inflate.settag (MyView);
} else {
Inflate=convertview;
myview= (MyView) Inflate.gettag ();
}
This allows for multiple reuse. ;
However, due to the reuse of the relationship, in each picture is still in the download process is if the picture after the first download is complete, the back of the picture has not downloaded the relationship, to swipe up and down. Will find that the first icon appears at the bottom, after a period of time will find the picture of the jump problem, this is because the view reuse, such as the 7th row of the No. 0 row, the 7th line of the icon has not been downloaded, originally should be the default original image, However, since the icon has been changed on line No. 0, he is not the default icon, so the previous icon will be displayed.
FIX: In the beginning to download the icon, change the picture before changing the picture to our default image, for example:
Myview.image.setImageResource (R.drawable.ic_launcher);
This can only solve the basic problem, if the first is just a swipe, there will be no icon flicker, if the beginning of time when the picture is still downloading when the fast swipe up and down, this will still appear the image flash. The replacement of the view is regular when we slide lightly, but if the view is not regular when it starts to slide, he does not appear as if the image is to be replaced during the sliding process.
For example:
public void Changview (ImageView image)
{
This.image = image;
}
Asynctask.changview (Myview.image);
And when you finish downloading the picture, you must make changes to the current line before it is displayed.
For example:
int FVP = Mlistview.getfirstvisibleposition ();
int LVP = Mlistview.getlastvisibleposition ();
if (POSITION>=FVP&&POSITION<=LVP) {
Save Bitmap
Imagebitmaplist.put (position, result);
Image.setimagebitmap (result);
}
The above is to open the interface when the most basic display, all the data from the network, the next is when we click the button download, will be the basic file download, as before the image download, the file download is downloaded after the click Download, all operations as above the image is basically the same, The only thing is that when you click on the download, the progress bar of the file will follow the download progress of the continuous promotion. This is the need to use Onprogressupdate (), in the use of onprogressupdate, the data download progress is from publishprogress, operation code such as;
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/6F/wKiom1X9mRmyI2cjAAE2eJkdH7g093.jpg "title=" Image_ 04.png "alt=" Wkiom1x9mrmyi2cjaae2ejkdh7g093.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/6F/wKiom1X9mUTioybWAALjuMWqZ0o529.jpg "title=" Image_ 03.png "alt=" Wkiom1x9mutioybwaaljumwqz0o529.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/6D/wKioL1X9m5DAVzM_AACk03kDOEc436.jpg "title=" Image_ 06.png "alt=" Wkiol1x9m5davzm_aack03kdoec436.jpg "/>
Problem: Because of the initial optimization, the reuse of the view, and the basic components, resulting in a bug, when the swipe up and down other places do not click to download, his ProgressBar in the progress bar is also displayed boxes.
Solve:
As in the picture, define the container, which is used to save, whether the current is in the download, download, download the successful three states, in the alignment to do the corresponding operation, and with the replacement, the operation code is as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/6D/wKioL1X9qNfQGjEaAAKZZjoCCS0407.jpg "style=" float: none; "title=" Image07.png "alt=" Wkiol1x9qnfqgjeaaakzzjoccs0407.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/6F/wKiom1X9ppzR17gLAAJ87Xcweto030.jpg "style=" float: none; "title=" Image_08.png "alt=" Wkiom1x9ppzr17glaaj87xcweto030.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/6D/wKioL1X9qNjyRcf0AAHA9lV_MlM171.jpg "style=" float: none; "title=" Image_09.png "alt=" Wkiol1x9qnjyrcf0aaha9lv_mlm171.jpg "/>
In this simulation of mobile phone assistant download, has basically been completed.
This article from the "Java" blog, reproduced please contact the author!
Android Analog phone assistant download