android通過url下載圖片並實現fragment與activity的圖片互動

來源:互聯網
上載者:User

標籤:android   activity   url下載圖片   



        650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/56/BD/wKiom1SL18DS-xfoAAB0b2g3jvk579.jpg" title="all.jpg" alt="wKiom1SL18DS-xfoAAB0b2g3jvk579.jpg" />

    增加了圖片放大縮小功能,一共用到三個類。MainActivity僅作為添加fragment用,其全部代碼如:


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();//添加片段

}


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添加完fragment就放寒假了。


    先在fragment的xml裡添加兩張預設圖片:

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/56/BD/wKiom1SL2gfwqftkAABY6LAm_sE175.jpg" title="xml.jpg" alt="wKiom1SL2gfwqftkAABY6LAm_sE175.jpg" />


    得到所需圖片地址,並通過AsyncTask執行下載,將下載完的圖片放進聲明出來的bitmapList中,全部下載完成後再初始化控制項:

    

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();//下載圖片

}else{

ViewGroup parent = (ViewGroup) layout.getParent();

parent.removeView(layout);

}

return layout;

}


private int index;

private void initBitmap() {

MyAsync myAsync = new MyAsync();

if(index < 2){

//下載圖片

myAsync.execute(urlList[index]);

}else{

initView();//初始化控制項

}

}


 

AsyncTask下載過程:

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();

}

}

}

}



    將下載完成的bitmap顯示到控制項中:

    

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="下載後.jpg" alt="wKioL1SL3gfjkooIAAKDgPFhYkw007.jpg" />



    這樣就可以將網路上的圖片正常顯示到APP裡了,記得添加許可權:

    <uses-permission android:name="android.permission.INTERNET"/>


    最後fragment的圖片傳到MyTouchActivity裡。首先在MyTouchActivity寫一個方法用來接收圖片:


public void getBitmapInfo(Bitmap bitmap, int position){

this.bitmap = bitmap;

this.position = position;

}



    執行圖片的點擊事件時,先new 出MyTouchActivity對象並調用上面所寫方法傳入圖片:


    //傳出對應的圖片到MytouchActivity,同時從當前activity跳轉到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;

}

}



    在MyTouch裡將接收到的bitmap顯示出來就可以了。


    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/56/BD/wKiom1SL37Kwa4rcAANtQ80A83Q791.jpg" title="點擊之後.jpg" alt="wKiom1SL37Kwa4rcAANtQ80A83Q791.jpg" />


    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/56/BB/wKioL1SL4SLyXctZAAOiNfgpIiU895.jpg" title="手機.jpg" alt="wKioL1SL4SLyXctZAAOiNfgpIiU895.jpg" />


    簡單寫一個例子,供有需要的朋友參考,新手上路,不足之處請大俠們海涵並多指教哈~

    


    


本文出自 “破公子的偽技術部落格” 部落格,請務必保留此出處http://pogongzi.blog.51cto.com/8982426/1589568

android通過url下載圖片並實現fragment與activity的圖片互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.