Android Method of Loading Pictures Using Control ImageView
- 2021-07-18 09:07:36
- OfStack
In Android loading picture 1 like the use of ImageView, here a simple record of 1 under the use of this control.
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 also load it in the program. For example:
@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);
}
An this parameter is passed when the ImageView object is built, indicating that it is associated with the current context (context). This Context is handled by the system and provides services such as resource resolution, retrieval of access databases, and preferences. Because the Activity class inherits from Context, and because your HelloWorld class is a subclass of Activity, it is also an Context. Therefore, you can pass this as your Context to ImageView reference.
Android ImageView How to load network picture resources, the code is also shared with you:
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 {
// Definition 1 Picture display controls
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Picture resource
String url = "/orignal/89429f6dhb99b4903ebcf&690";
// Get available pictures
Bitmap bitmap = getHttpBitmap(url);
imageView = (ImageView)this.findViewById(R.id.imageViewId);
// Display
imageView.setImageBitmap(bitmap);
}
/**
* Getting Web Picture Resources
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url){
URL myFileURL;
Bitmap bitmap=null;
try{
myFileURL = new URL(url);
// Get a connection
HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
// Set the timeout time to 6000 Milliseconds, conn.setConnectionTiem(0); Indicates that there is no time limit
conn.setConnectTimeout(6000);
// Connection settings get data flow
conn.setDoInput(true);
// Do not use caching
conn.setUseCaches(false);
// This sentence is optional and has no effect
//conn.connect();
// Get the data stream
InputStream is = conn.getInputStream();
// Parse to get a picture
bitmap = BitmapFactory.decodeStream(is);
// Close the data flow
is.close();
}catch(Exception e){
e.printStackTrace();
}
return bitmap;
}
}