650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/56/BD/wKiom1SL18DS-xfoAAB0b2g3jvk579.jpg "title=" All.jpg "alt=" Wkiom1sl18ds-xfoaab0b2g3jvk579.jpg "/>
Added image zoom in and out, with a total of three classes. Mainactivity is used only as an add-fragment, with all its code such as:
Package com.example.ex_1213_mypic;
Import Android.os.Bundle;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;
Import Android.view.Menu;
public class Mainactivity extends Fragmentactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Addfragment ();//Add Fragment
}
private void Addfragment () {
Fragmentmanager fm = Getsupportfragmentmanager ();
Fragmenttransaction ft = fm.begintransaction ();
Myfragment myfragment = new Myfragment ();
Ft.add (R.id.fl_replace, myfragment);
Ft.commit ();
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
Mainactivity add fragment on the winter vacation.
Add two default images to the fragment XML first:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/56/BD/wKiom1SL2gfwqftkAABY6LAm_sE175.jpg "title=" Xml.jpg "alt=" Wkiom1sl2gfwqftkaaby6lam_se175.jpg "/>
Get the desired image address and perform the download via Asynctask, put the downloaded picture into the declared bitmaplist, and then initialize the control after the download is complete:
Package com.example.ex_1213_mypic;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.util.ArrayList;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.ImageView;
public class Myfragment extends Fragment implements onclicklistener{
String [] urllist = {
"Http://7qn7nu.com1.z0.glb.clouddn.com/77.jpg",
"Http://7qn7nu.com1.z0.glb.clouddn.com/u=3516622004,3632540994&fm=59.jpg"
};
Private View layout;
arraylist<bitmap> bitmaplist = new arraylist<bitmap> ();
Public Myfragment () {
Required empty Public constructor
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
if (layout = = null) {
Layout = Getactivity (). Getlayoutinflater (). Inflate (r.layout.fragment_my, NULL);
Initbitmap ();//Download picture
}else{
ViewGroup parent = (ViewGroup) layout.getparent ();
Parent.removeview (layout);
}
return layout;
}
private int index;
private void Initbitmap () {
Myasync Myasync = new Myasync ();
if (Index < 2) {
Download image
Myasync.execute (Urllist[index]);
}else{
Initview ();//Initialize Control
}
}
Asynctask Download Process:
Class Myasync extends Asynctask<string, Void, bitmap>{
@Override
Protected Bitmap doinbackground (String ... params) {
URL url;
Bitmap Bitmap = null;
try {
url = new URL (params[0]);
urlconnection con=url.openconnection ();
HttpURLConnection con= (httpurlconnection) url.openconnection ();
Con.setdoinput (TRUE);
Con.connect ();
InputStream Is=con.getinputstream ();
Bitmap=bitmapfactory.decodestream (IS);
Is.close ();
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return bitmap;
}
@Override
protected void OnPostExecute (Bitmap Bitmap) {
TODO auto-generated Method Stub
Super.onpostexecute (bitmap);
if (bitmap! = null) {
if (Index < 2) {
Bitmaplist.add (bitmap);
index++;
Initbitmap ();
}
}
}
}
Display the downloaded bitmap to the control:
private void Initview () {
ImageView img1 = (ImageView) Layout.findviewbyid (R.ID.IMAGEVIEW1);
ImageView Img2 = (ImageView) Layout.findviewbyid (R.ID.IMAGEVIEW2);
Img1.setimagebitmap (bitmaplist.get (0));
Img2.setimagebitmap (Bitmaplist.get (1));
Img1.setonclicklistener (this);
Img2.setonclicklistener (this);
}
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/56/BB/wKioL1SL3gfjkooIAAKDgPFhYkw007.jpg "title=" After download. jpg "alt=" wkiol1sl3gfjkooiaakdgpfhykw007.jpg "/>
This way you can display the pictures on the network to the app, and remember to add the permissions:
<uses-permission android:name= "Android.permission.INTERNET"/>
Finally, the fragment pictures were uploaded to Mytouchactivity. First write a method in mytouchactivity to receive the image:
public void Getbitmapinfo (Bitmap Bitmap, int position) {
This.bitmap = bitmap;
This.position = position;
}
To perform a click event on a picture, first new out the Mytouchactivity object and invokes the method written above to pass in the picture:
Outgoing images to mytouchactivity, while jumping from current activity to mytouchactivity
private void Initclickbitmap (int x) {
Intent Intent = new Intent ();
Mytouchactivity touchactivity = new mytouchactivity ();
if (Bitmaplist.size () > x) {
Touchactivity.getbitmapinfo (Bitmaplist.get (x), x);
Intent.setclass (Getactivity (), mytouchactivity.class);
StartActivity (Intent);
}
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.ID.IMAGEVIEW1:
Initclickbitmap (0);
Break
Case R.ID.IMAGEVIEW2:
Initclickbitmap (1);
Break
Default
Break
}
}
The received bitmap will be displayed in the MyTouch.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/56/BD/wKiom1SL37Kwa4rcAANtQ80A83Q791.jpg "title=" Click After. jpg "alt=" wkiom1sl37kwa4rcaantq80a83q791.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/56/BB/wKioL1SL4SLyXctZAAOiNfgpIiU895.jpg "title=" phone. jpg "alt=" Wkiol1sl4slyxctzaaoinfgpiiu895.jpg "/>
Simple to write an example, for the needs of friends reference, Novice on the road, the shortcomings please the Heroes Haihan and more advice ha ~
This article is from "broken Childe's pseudo technology blog" blog, please be sure to keep this source http://pogongzi.blog.51cto.com/8982426/1589568
Android downloads images via URLs and enables fragment to interact with activity's images