Android Authority Programming Challenge Practice Chapter 16

Source: Internet
Author: User

16.7  Challenge Exercise: Optimize photo display Now that you can see the photos you've taken, you can't see their details. Create a dialogfragment that can display a scaled version of a photo. Just click on the thumbnail, it will pop up this dialogfragment, let the user see the scale version of the bad habits of the scene image.

Create a layout file with a root node of ImageView, with the following code:

<?xml version="1.0"encoding="Utf-8"?><ImageView xmlns:android="http://schemas.android.com/apk/res/android"Android:id="@+id/iv"Android:layout_width="wrap_content"Android:layout_height="wrap_content"></ImageView>

Next, create a picturedialogfragment inheritance dialogfragement to display the larger image.

1 Package com.bignerdranch.android.criminallntent;2 3 import Android.app.Dialog;4 import Android.os.Bundle;5 import Android.support.annotation.NonNull;6 import android.support.v4.app.DialogFragment;7 import Android.view.View;8 import Android.widget.ImageView;9 Ten /** One * Created by Leo on 2017/7/16. A  */ -  -  Public classPicturedialogfragment extends Dialogfragment { the  -      Public Staticpicturedialogfragment newinstance (String path) { -Bundle args =NewBundle (); -Args.putstring ("Path", path); +  -Picturedialogfragment picturedialogfragment =Newpicturedialogfragment (); + picturedialogfragment.setarguments (args); A         returnpicturedialogfragment; at     } -  - @NonNull - @Override -      PublicDialog Oncreatedialog (Bundle savedinstancestate) { -         //get to the path that was passed before. inString path = Getarguments (). GetString ("Path"); -         //Create a dialog toFinal Dialog Dialog =NewDialog (Getactivity ()); +         //set the layout of the dialog for the previously created layout file with only one ImageView - Dialog.setcontentview (r.layout.picture); the         //Locate the control *ImageView ImageView =(ImageView) Dialog.findviewbyid (R.ID.IV); $         //Use the tools of the Pictureutils class to get the Bitmap of the zoomPanax Notoginseng Imageview.setimagebitmap (Pictureutils.getscalebitmap (Path,getactivity ())); -         //set the Click event, dialog disappears when you click on the picture.  theImageview.setonclicklistener (NewView.onclicklistener () { + @Override A              Public voidOnClick (View v) { the Dialog.dismiss (); +             } -         }); $  $         returnDialog; -     } -}

Finally, modify the code in the Crimefragment, so that when you click on the thumbnail, a large image is displayed.

Add a click event to the thumbnail control

1Mphotoview.setonclicklistener (NewView.onclicklistener () {2 @Override3              Public voidOnClick (View v) {4Fragmentmanager Manager =Getfragmentmanager ();5Picturedialogfragment picturedialogfragment =picturedialogfragment.newinstance (Mphotofile.getpath ());6Picturedialogfragment.settargetfragment (crimefragment. This,0);7Picturedialogfragment.show (Manager,"a");8             }9});

16.8 Challenge Exercise: optimize thumbnail loading = mimageview.getviewtreeobserver (); we can set the Viewtreeobserver object to include Ongloballayoutlistener The various listeners. With the Ongloballayoutlistener listener, you can monitor the delivery of any layout and control the occurrence of events. Adjust the code to use a valid Mphotoview size, and then call the Updatephotoview () method when there is a layout switch. 

Modify the Updatephotoview () method in the Crimefragment () method.

1 Private voidUpdatephotoview (intWidthintheight) {2         if(Mphotofile = =NULL|| !mphotofile.exists ()) {3Mphotoview.setimagedrawable (NULL);4}Else {5Mbitmap =Pictureutils.getscaledbitmap (Mphotofile.getpath (), width,height);6 Mphotoview.setimagebitmap (mbitmap);7         }8}

The width and height of the two parameters are added, so the Updatephoto method is scaled according to the parameters we pass.

Then add the following code in the Oncreateview () method:

1Mphotoobserver =mphotoview.getviewtreeobserver ();2 Mphotoobserver.addongloballayoutlistener (3                 NewViewtreeobserver.ongloballayoutlistener () {4 @Override5                      Public voidongloballayout () {6 Updatephotoview (Mphotoview.getwidth (), Mphotoview.getheight ());7                     }8});

First, an observer who registers a listener view tree can listen for any layout transfer and control the occurrence of the event.

This adds a Ongloballayoutlistener function to register a callback function that is called when the global layout changes in a view tree or the visual state of a view in the view tree changes.

Android Authority Programming Challenge Practice Chapter 16

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.