2012-04-01 46 views
0

我正在開發Android應用,的Android - GridView的的onResume

一次,然後,顯示另一活動所選的這個Apps是創建一個包含一些圖像(圖像從URL加載)一個GridView,

部分當GridView中的一個圖像是onClick時,將會出現全屏圖像。

問題:

當我輸入GridView的活性,它需要一些第二加載所有圖像中的GridView正常。

然後我點擊圖片中的一個進入全屏幕活動,然後單擊後退按鈕返回到GridView控件,

但是,回到gridview的時候,就像再次加載所有的圖像需要一定的第二裝載。

我想知道爲什麼gridview activity會在onResume時加載幾秒鐘?

例如,在Google Play中,任何應用程序中的示例圖像的全屏視圖都可以立即返回到之前的視圖。

封閉式代碼:

GridView控件:

public class ManagePhoto extends Activity { 

ImageAdapter ia; 
GridView gridview; 
InputStream inputStream; 
private static final int SELECT_PICTURE = 1; 
private String selectedImagePath; 
TextView tvRunningMark; 
boolean bRunning; 
String[] purl; 
Bitmap[] bm; 
String the_string_response; 
TouchImageView touch; 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.photo_manage); 

    //gv is Gobal Value 
    final Gvalue gv = ((Gvalue) getApplicationContext()); 

    gridview = (GridView) findViewById(R.id.gv_photo); 
    gridview.setOnItemClickListener(new GridView.OnItemClickListener() { 
     public void onItemClick(AdapterView adapterView, View view,int position, long id) { 
      gv.setbm(bm[position]); 
      Intent myIntent = new Intent(adapterView.getContext(), FullScreenImage.class); 
      startActivityForResult(myIntent, 0); 
     } 
    }); 
    new GridTask().execute(); 
} 

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return purl.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some 
            // attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(200,200)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     try { 
      bm[position] = loadBitmap(purl[position]); 
      imageView.setImageBitmap(bm[position]); 

     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return imageView; 
    } 
} 

class GridTask extends AsyncTask<Void, String, Void> { 

    @Override 
    protected void onPostExecute(Void result) { 
     gridview.setAdapter(ia); 
     final LinearLayout llo_probar = (LinearLayout)findViewById(R.id.llo_probar); 
     llo_probar.setVisibility(LinearLayout.GONE); 
     gridview.setVisibility(GridView.VISIBLE); 
    } 

    @Override 
    protected void onPreExecute() {   
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
    } 

    @Override 
    protected Void doInBackground(Void... params) {   
     getphoto(); 
     bm = new Bitmap[purl.length]; 
     ia = new ImageAdapter(ManagePhoto.this); 
     return null; 
    } 

} 

private Bitmap loadBitmap(String url) throws MalformedURLException,IOException { 
    return BitmapFactory.decodeStream(new FlushedInputStream(
      (InputStream) new URL(url).getContent())); 
} 

class FlushedInputStream extends FilterInputStream { 
    public FlushedInputStream(final InputStream inputStream) { 
     super(inputStream); 
    } 

    @Override 
    public long skip(final long n) throws IOException { 
     long totalBytesSkipped = 0L; 

     while (totalBytesSkipped < n) { 
      long bytesSkipped = in.skip(n - totalBytesSkipped); 

      if (bytesSkipped == 0L) { 
       int bytesRead = read(); 

       if (bytesRead < 0) { // we reached EOF 
        break; 
       } 

       bytesSkipped = 1; 
      } 

      totalBytesSkipped += bytesSkipped; 
     } 

     return totalBytesSkipped; 
    } 
} 

public void getphoto(){ 
    final Gvalue gv = ((Gvalue) getApplicationContext()); 
    final TextView tv_fn = (TextView) findViewById(R.id.tv_fn); 
    String result = ""; 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("sql", 
      ("select * from personal_photo where member_id = " + gv.getuid()))); 

    InputStream is = null; 
    // http post 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://" + gv.getserverIP() 
       + "/android_getdata.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error in http connection " + e.toString()); 
    } 
    // convert response to string 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 

     result = sb.toString(); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error converting result " + e.toString()); 
    } 

    // parse json data 
    try { 
     List url = new ArrayList(); 
     JSONArray jArray = new JSONArray(result); 
     for (int i = 0; i < jArray.length(); i++) { 
      JSONObject json_data = jArray.getJSONObject(i); 
      url.add(json_data.getString("save_location")); 
     } 
     int size = url.size(); 
     purl = new String[size]; 
     for (int j = 0; j < size; j++) { 
      purl[j] = (String) url.get(j); 
     } 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 
} 

public void toast(String text) { 
    Toast.makeText(ManagePhoto.this, text, 5).show(); 
} 
} 

全屏:

public class FullScreenImage extends Activity { 
TouchImageView touch; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    new ShowPhotoTask().execute(); 
} 

class ShowPhotoTask extends AsyncTask<Void, String, Void> { 


    @Override 
    protected void onPostExecute(Void result) { 
     setContentView(touch); 
    } 

    @Override 
    protected void onPreExecute() { 
     setContentView(R.layout.full_image); 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     final Gvalue gv = ((Gvalue) getApplicationContext()); 
     touch = new TouchImageView(FullScreenImage.this); 
     touch.setMaxZoom(4f); // change the max level of zoom, default is 3f 
     touch.setImageBitmap(gv.getbm()); 
     return null; 
    } 

} 
} 

回答

0

從您的getView代碼中您的ImageAdapter,每次您將再次從互聯網上加載圖像。

你應該下載圖像到本地,下次,當你設置圖像時,你首先嚐試從本地獲取圖像。

以及你應該把得到的位圖放在線程中,因爲你已經把線程中的解析JSON。

這是demo,我認爲它會幫助你。

0

在你的GridView的onclick你與startActivityForResults開始活動(意向,0);用startActivity(intent)替換它;

當您完成FullScreenActivity時,只需使用finish();

可能會解決你的問題

+0

我只是改變了以.. startActivity(意向);並在完成()在FullScreenActivity onBackPressed(),但無法正常工作..感謝您的幫助 – 2012-04-01 13:04:38

+0

現在是什麼結果? – Ishu 2012-04-01 13:39:55

+0

相同,在onBack中也需要一些第二加載 – 2012-04-01 14:14:30

0

它是如此緩慢的,因爲你不與後臺線程,緩存執行你的操作方法。 一個簡單更好的解決方案可能是,含有插入所有的位圖的集合通過AsyncTasks,你會執行下載圖片。

有更好的解決方案,但它們更難實施。 例如,你可以考慮保留一個線程池來解析你的runnables,代表「下載http://jhon.doe.jpg」,然後「在UI線程上顯示」。

+0

對不起,我是android開發的新手。你能否給我一些關於更好的解決方案的建議?其實,您是否有任何關於'Google Play'是如何做到這一點的想法? – 2012-04-01 13:13:42

0

正如你已經寫了下面功能getView()方法中:

bm[position] = loadBitmap(purl[position]); 

我會說你應該實現代碼負荷影像異步。在此邏輯中,圖像在下載完成後會同步到存儲卡中。所以下次它將直接從存儲卡中加載,而不是再次從web加載它。

下面是一個代碼示例,你可以給一個嘗試:Android - Universal Image Loader