Android開發-----顯示長圖


圖片標題

問題

ImageView加載長圖的時候,由於有長度上的限制,所以遇到過長圖片的時候,無法完全顯示,

解決方案

方法一:使用subsampling-scale-image-view庫 
在網上一個論壇找到這個庫的名字,然后github,的確可以加載長圖,但是一時半會兒沒有找到控制初始顯示比例適應屏幕的方法,由於開發周期緊,所以不能花太多精力去研究這個庫,所以沒有使用

方法二:使用Glide+Bitmap 

Glide.with(this).load(RequestUrl.BASE_URL + photoUrl)
     .downloadOnly(new SimpleTarget<File>() {
        @Override
        public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
        // 將保存的圖片地址給SubsamplingScaleImageView,這里注意設置ImageViewState設置初始顯示比例
            Bitmap bitmap= BitmapFactory.decodeFile(resource.getAbsolutePath(),getBitmapOption(1));
        // 顯示處理好的Bitmap圖片
            imageView.setImageBitmap(bitmap);
    }
});

另一種方法的解決思路是先將圖片從網上下載到本地,然后經過Bitmap處理后,再加載

但是僅僅這樣似乎還不夠,然后想到了ImageView的adjustViewBounds屬性,設置它為true

android:adjustViewBounds="true"

方法三:使用HttpUrlConnection + Bitmap 
這個方法和方法二的思路是一模一樣的,只是這個使用的android自己的HttpUrlConnection訪問網絡: 
/**

* 點擊獲取圖片 * @param view */
public void start(View view) {  final String path = "http://192.168.3.162:8080/gotoclass/test.jpg";  new ShowLongImgAsyncTask().execute(path); }
/** * 自定義AsyncTask */ class ShowLongImgAsyncTask extends AsyncTask<String, Void, File> { @Override protected void onPreExecute() { super.onPreExecute(); imageView.setImageBitmap(null); }
@Override
protected void onPostExecute(File result) { super.onPostExecute(result); if (result != null) { if (file != null && file.exists()) { Bitmap bitmap = BitmapFactory.decodeFile(result.getAbsolutePath(), getBitmapOption(1)); imageView.setImageBitmap(bitmap); } } else { System.out.println("---------文件為null-----------"); } }
@Override
protected File doInBackground(String... params) { File b = getImageBitmap(params[0]); return b; } }

/** * HttpUrlConnection訪問網絡 * 並下載圖片到本地 * @param url * @return */ private File getImageBitmap(String url) { URL imgurl = null; Bitmap bitmap = null; File file = null; HttpURLConnection urlConnection; try { imgurl = new URL(url); urlConnection = (HttpURLConnection) imgurl.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); InputStream is = urlConnection.getInputStream(); //構建一個file對象用於存儲圖片 file = new File(Environment.getExternalStorageDirectory(), "pic.jpg"); fos = new FileOutputStream(file); int len = 0; byte[] buffer = new byte[1024]; //將輸入流寫入到我們定義好的文件中 while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); }

is.close(); fos.close(); }
catch (IOException e) { e.printStackTrace(); } return file; }
/** * 設置Bitmap的Options */ private BitmapFactory.Options getBitmapOption(int inSampleSize) { System.gc(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; options.inSampleSize = inSampleSize; return options; }

 

這個和方法二都是將圖片下載到本地,然后轉為Bitmap設置給ImageView, 
但是如果不存文件,而是直接一個Bitmap,也能實現同樣的效果

/**
* HttpUrlConnection訪問網絡
* 並下載圖片到本地
* @param url
* @return
*/
private Bitmap getImageBitmap(String url) {
    URL imgurl = null;
    Bitmap bitmap = null;
    File file = null;
    HttpURLConnection urlConnection;
    try {
        imgurl = new URL(url);
        urlConnection = (HttpURLConnection)
        imgurl.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();
        InputStream is = urlConnection.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
}
return null; }
class ShowLongImgAsyncTask extends AsyncTask<String, Void, Bitmap> { @Override protected void onPreExecute() { super.onPreExecute(); imageView.setImageBitmap(null); }
@Override
protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (result != null) { if (file != null && file.exists()) { imageView.setImageBitmap(result); } } else { System.out.println("---------文件為null-----------"); } }
@Override
protected Bitmap doInBackground(String... params) { Bitmap b = getImageBitmap(params[0]); return b; } }

!!!不要忘記設置

android:adjustViewBounds="true"

由此方法一和方法二可推測,使用方式或框架,將圖片下載到本地,然后經bitmap處理,都應該能正常顯示

方法四:快速解決方案 使用WebView

在做這個功能的時候,突發奇想,既然WebView是可以加載一個鏈接地址的,那么從網絡獲取的圖片也是一個鏈接地址,那么應該也可以使用WebView來解決這個長圖的問題

webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://192.168.3.162:8080/gotoclass/test.jpg");

但是如果僅僅這么做,圖片有可能在寬度上出現顯示問題,所以還要讓它適應屏幕縮放

WebSettings settings = webView.getSettings();
// 設置可任意縮放
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);

 

方法五:BitmapRegionDecoder

  還有一種是通過BitmapRegionDecoder來顯示局部圖片一點點加載的,似乎可行,由於項目時間緊,沒有仔細研究

 

如有錯誤, 敬請指正, 感激不盡!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM