Android uses control ImageView to load pictures _android

Source: Internet
Author: User
Tags time limit

Loading pictures on Android typically uses ImageView, which simply records how the control is used.

The simplest is to use the ImageView tag directly in XML:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical "
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent"
 >  
<imageview
 android:id= "@+id/iv" android:layout_width= "WRAP_"
 Content "
 android:layout_height=" wrap_content "
 android:src=" @drawable/welcome "
/>
</ Linearlayout>

If you don't want to be in XML, you can load it inside the program. Like what:

@Override
protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
  
 ImageView welcome = new ImageView (this);
 Welcome.setimageresource (r.drawable.welcome);
 Setcontentview (welcome);
}

A This parameter was passed when the ImageView object was built, representing the current context (contexts) association. This context is handled by the system and provides services such as resource resolution, access to databases, and preferences. Because the activity class inherits from the context, and because your HelloWorld class is a subclass of the activity, it is also a context. Therefore, you can pass this as your context to the ImageView reference.

Android ImageView How to load network Image resources , the code also share to everyone:

Package Com.android.antking.imageview; 
Import Java.io.InputStream; 
Import java.net.HttpURLConnection; 
Import java.net.MalformedURLException; 
 
Import Java.net.URL; 
Import android.app.Activity; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.os.Bundle; 
Import Android.util.Log; 
Import Android.view.View; 
 
Import Android.widget.ImageView; 
  public class Mainactivity extends activity {//define a picture display control private ImageView ImageView; /** called the activity is a. 
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.main); 
    Picture resource String url = "/orignal/89429f6dhb99b4903ebcf&690"; 
    Get the picture available Bitmap Bitmap = gethttpbitmap (URL); 
    ImageView = (ImageView) This.findviewbyid (R.ID.IMAGEVIEWID); 
     
  Display Imageview.setimagebitmap (bitmap); /** * Access to the Web image resources * @param URL * @return * * * PublIC Static Bitmap gethttpbitmap (String url) {URL myfileurl; 
    Bitmap Bitmap=null; 
      try{myfileurl = new URL (URL); 
      Get Connected HttpURLConnection conn= (httpurlconnection) myfileurl.openconnection (); 
      Set timeout time to 6000 milliseconds, Conn.setconnectiontiem (0), indicating no time limit conn.setconnecttimeout (6000); 
      The connection setting obtains the data stream Conn.setdoinput (true); 
      Do not use cache conn.setusecaches (false); 
      This sentence is dispensable, does not affect//conn.connect (); 
      Gets the data stream InputStream is = Conn.getinputstream (); 
      Parse get picture bitmap = Bitmapfactory.decodestream (IS); 
    Close Data Flow is.close (); 
    }catch (Exception e) {e.printstacktrace (); 
     
  return bitmap;  } 
}

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.