多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > Android 高清加載巨圖方案 拒絕壓縮圖片

Android 高清加載巨圖方案 拒絕壓縮圖片

來源:程序員人生   發(fā)布時(shí)間:2016-03-17 11:26:35 閱讀次數(shù):4016次

Android 高清加載巨圖方案 謝絕緊縮圖片

1、概述

距離上1篇博客有段時(shí)間沒更新了,主要是最近有些私事致使的,那末就先來1篇簡單1點(diǎn)的博客脈動(dòng)回來。

對加載圖片,大家都不陌生,1般為了盡量避免OOM都會(huì)依照以下做法:

  1. 對圖片顯示:根據(jù)需要顯示圖片控件的大小對圖片進(jìn)行緊縮顯示。
  2. 如果圖片數(shù)量非常多:則會(huì)使用LruCache等緩存機(jī)制,將所有圖片占據(jù)的內(nèi)容保持在1個(gè)范圍內(nèi)。

其實(shí)對圖片加載還有種情況,就是單個(gè)圖片非常巨大,并且還不允許緊縮。比如顯示:世界地圖、清明上河圖、微博長圖等。

那末對這類需求,該如何做呢?

首先不緊縮,依照原圖尺寸加載,那末屏幕肯定是不夠大的,并且斟酌到內(nèi)存的情況,不可能1次性整圖加載到內(nèi)存中,所以肯定是局部加載,那末就需要用到1個(gè)類:

  • BitmapRegionDecoder

其次,既然屏幕顯示不完,那末最最少要添加1個(gè)上下左右拖動(dòng)的手勢,讓用戶可以拖動(dòng)查看。

那末綜上,本篇博文的目的就是去自定義1個(gè)顯示巨圖的View,支持用戶去拖動(dòng)查看,大概的效果圖以下:

好吧,這清明上河圖太長了,想要觀看全圖,文末下載,圖片在assets目錄。固然如果你的圖,高度也很大,肯定也是可以上下拖動(dòng)的。

2、初識(shí)BitmapRegionDecoder

BitmapRegionDecoder主要用于顯示圖片的某1塊矩形區(qū)域,如果你需要顯示某個(gè)圖片的指定區(qū)域,那末這個(gè)類非常適合。

對該類的用法,非常簡單,既然是顯示圖片的某1塊區(qū)域,那末最少只需要1個(gè)方法去設(shè)置圖片;1個(gè)方法傳入顯示的區(qū)域便可;詳見:

  • BitmapRegionDecoder提供了1系列的newInstance方法來構(gòu)造對象,支持傳入文件路徑,文件描寫符,文件的inputstrem等。

    例如:

    BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
  • 上述解決了傳入我們需要處理的圖片,那末接下來就是顯示指定的區(qū)域。

    bitmapRegionDecoder.decodeRegion(rect, options);

    參數(shù)1很明顯是1個(gè)rect,參數(shù)2是BitmapFactory.Options,你可以控制圖片的inSampleSize,inPreferredConfig等。

那末下面看1個(gè)超級簡單的例子:

package com.zhy.blogcodes.largeImage; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapRegionDecoder; import android.graphics.Rect; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import com.zhy.blogcodes.R; import java.io.IOException; import java.io.InputStream; public class LargeImageViewActivity extends AppCompatActivity { private ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_large_image_view); mImageView = (ImageView) findViewById(R.id.id_imageview); try { InputStream inputStream = getAssets().open("tangyan.jpg"); //取得圖片的寬、高 BitmapFactory.Options tmpOptions = new BitmapFactory.Options(); tmpOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, tmpOptions); int width = tmpOptions.outWidth; int height = tmpOptions.outHeight; //設(shè)置顯示圖片的中心區(qū)域 BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bitmap = bitmapRegionDecoder.decodeRegion(new Rect(width / 2 - 100, height / 2 - 100, width / 2 + 100, height / 2 + 100), options); mImageView.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } } }

上述代碼,就是使用BitmapRegionDecoder去加載assets中的圖片,調(diào)用bitmapRegionDecoder.decodeRegion解析圖片的中間矩形區(qū)域,返回bitmap,終究顯示在ImageView上。

效果圖:

上面的小圖顯示的即為下面的大圖的中間區(qū)域。

ok,那末目前我們已了解了BitmapRegionDecoder的基本用戶,那末往外分散,我們需要自定義1個(gè)控件去顯示巨圖就很簡單了,首先Rect的范圍就是我們View的大小,然后根據(jù)用戶的移動(dòng)手勢,不斷去更新我們的Rect的參數(shù)便可。

3、自定義顯示大圖控件

根據(jù)上面的分析呢,我們這個(gè)自定義控件思路就非常清晰了:

  • 提供1個(gè)設(shè)置圖片的入口
  • 重寫onTouchEvent,在里面根據(jù)用戶移動(dòng)的手勢,去更新顯示區(qū)域的參數(shù)
  • 每次更新區(qū)域參數(shù)后,調(diào)用invalidate,onDraw里面去regionDecoder.decodeRegion拿到bitmap,去draw

理清了,發(fā)現(xiàn)so easy,下面上代碼:

package com.zhy.blogcodes.largeImage.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapRegionDecoder; import android.graphics.Canvas; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import java.io.IOException; import java.io.InputStream; /** * Created by zhy on 15/5/16. */ public class LargeImageView extends View { private BitmapRegionDecoder mDecoder; /** * 圖片的寬度和高度 */ private int mImageWidth, mImageHeight; /** * 繪制的區(qū)域 */ private volatile Rect mRect = new Rect(); private MoveGestureDetector mDetector; private static final BitmapFactory.Options options = new BitmapFactory.Options(); static { options.inPreferredConfig = Bitmap.Config.RGB_565; } public void setInputStream(InputStream is) { try { mDecoder = BitmapRegionDecoder.newInstance(is, false); BitmapFactory.Options tmpOptions = new BitmapFactory.Options(); // Grab the bounds for the scene dimensions tmpOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, tmpOptions); mImageWidth = tmpOptions.outWidth; mImageHeight = tmpOptions.outHeight; requestLayout(); invalidate(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (Exception e) { } } } public void init() { mDetector = new MoveGestureDetector(getContext(), new MoveGestureDetector.SimpleMoveGestureDetector() { @Override public boolean onMove(MoveGestureDetector detector) { int moveX = (int) detector.getMoveX(); int moveY = (int) detector.getMoveY(); if (mImageWidth > getWidth()) { mRect.offset(-moveX, 0); checkWidth(); invalidate(); } if (mImageHeight > getHeight()) { mRect.offset(0, -moveY); checkHeight(); invalidate(); } return true; } }); } private void checkWidth() { Rect rect = mRect; int imageWidth = mImageWidth; int imageHeight = mImageHeight; if (rect.right > imageWidth) { rect.right = imageWidth; rect.left = imageWidth - getWidth(); } if (rect.left < 0) { rect.left = 0; rect.right = getWidth(); } } private void checkHeight() { Rect rect = mRect; int imageWidth = mImageWidth; int imageHeight = mImageHeight; if (rect.bottom > imageHeight) { rect.bottom = imageHeight; rect.top = imageHeight - getHeight(); } if (rect.top < 0) { rect.top = 0; rect.bottom = getHeight(); } } public LargeImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } @Override public boolean onTouchEvent(MotionEvent event) { mDetector.onToucEvent(event); return true; } @Override protected void onDraw(Canvas canvas) { Bitmap bm = mDecoder.decodeRegion(mRect, options); canvas.drawBitmap(bm, 0, 0, null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = getMeasuredWidth(); int height = getMeasuredHeight(); int imageWidth = mImageWidth; int imageHeight = mImageHeight; //默許直接顯示圖片的中心區(qū)域,可以自己去調(diào)理 mRect.left = imageWidth / 2 - width / 2; mRect.top = imageHeight / 2 - height / 2; mRect.right = mRect.left + width; mRect.bottom = mRect.top + height; } }

根據(jù)上述源碼:

  1. setInputStream里面去取得圖片的真實(shí)的寬度和高度,和初始化我們的mDecoder
  2. onMeasure里面為我們的顯示區(qū)域的rect賦值,大小為view的尺寸
  3. onTouchEvent里面我們監(jiān)聽move的手勢,在監(jiān)聽的回調(diào)里面去改變r(jià)ect的參數(shù),和做邊界檢查,最后invalidate
  4. 在onDraw里面就是根據(jù)rect拿到bitmap,然后draw了

ok,上面其實(shí)不復(fù)雜,不過大家有無注意到,這個(gè)監(jiān)聽用戶move手勢的代碼寫的有點(diǎn)奇怪,恩,這里模仿了系統(tǒng)的ScaleGestureDetector,編寫了MoveGestureDetector,代碼以下:

  • MoveGestureDetector

    package com.zhy.blogcodes.largeImage.view; import android.content.Context; import android.graphics.PointF; import android.view.MotionEvent; public class MoveGestureDetector extends BaseGestureDetector { private PointF mCurrentPointer; private PointF mPrePointer; //僅僅為了減少創(chuàng)建內(nèi)存 private PointF mDeltaPointer = new PointF(); //用于記錄終究結(jié)果,并返回 private PointF mExtenalPointer = new PointF(); private OnMoveGestureListener mListenter; public MoveGestureDetector(Context context, OnMoveGestureListener listener) { super(context); mListenter = listener; } @Override protected void handleInProgressEvent(MotionEvent event) { int actionCode = event.getAction() & MotionEvent.ACTION_MASK; switch (actionCode) { case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: mListenter.onMoveEnd(this); resetState(); break; case MotionEvent.ACTION_MOVE: updateStateByEvent(event); boolean update = mListenter.onMove(this); if (update) { mPreMotionEvent.recycle(); mPreMotionEvent = MotionEvent.obtain(event); } break; } } @Override protected void handleStartProgressEvent(MotionEvent event) { int actionCode = event.getAction() & MotionEvent.ACTION_MASK; switch (actionCode) { case MotionEvent.ACTION_DOWN: resetState();//避免沒有接收到CANCEL or UP ,保險(xiǎn)起見 mPreMotionEvent = MotionEvent.obtain(event); updateStateByEvent(event); break; case MotionEvent.ACTION_MOVE: mGestureInProgress = mListenter.onMoveBegin(this); break; } } protected void updateStateByEvent(MotionEvent event) { final MotionEvent prev = mPreMotionEvent; mPrePointer = caculateFocalPointer(prev); mCurrentPointer = caculateFocalPointer(event); //Log.e("TAG", mPrePointer.toString() + " , " + mCurrentPointer); boolean mSkipThisMoveEvent = prev.getPointerCount() != event.getPointerCount(); //Log.e("TAG", "mSkipThisMoveEvent = " + mSkipThisMoveEvent); mExtenalPointer.x = mSkipThisMoveEvent ? 0 : mCurrentPointer.x - mPrePointer.x; mExtenalPointer.y = mSkipThisMoveEvent ? 0 : mCurrentPointer.y - mPrePointer.y; } /** * 根據(jù)event計(jì)算多指中心點(diǎn) * * @param event * @return */ private PointF caculateFocalPointer(MotionEvent event) { final int count = event.getPointerCount(); float x = 0, y = 0; for (int i = 0; i < count; i++) { x += event.getX(i); y += event.getY(i); } x /= count; y /= count; return new PointF(x, y); } public float getMoveX() { return mExtenalPointer.x; } public float getMoveY() { return mExtenalPointer.y; } public interface OnMoveGestureListener { public boolean onMoveBegin(MoveGestureDetector detector); public boolean onMove(MoveGestureDetector detector); public void onMoveEnd(MoveGestureDetector detector); } public static class SimpleMoveGestureDetector implements OnMoveGestureListener { @Override public boolean onMoveBegin(MoveGestureDetector detector) { return true; } @Override public boolean onMove(MoveGestureDetector detector) { return false; } @Override public void onMoveEnd(MoveGestureDetector detector) { } } }
  • BaseGestureDetector

    package com.zhy.blogcodes.largeImage.view; import android.content.Context; import android.view.MotionEvent; public abstract class BaseGestureDetector { protected boolean mGestureInProgress; protected MotionEvent mPreMotionEvent; protected MotionEvent mCurrentMotionEvent; protected Context mContext; public BaseGestureDetector(Context context) { mContext = context; } public boolean onToucEvent(MotionEvent event) { if (!mGestureInProgress) { handleStartProgressEvent(event); } else { handleInProgressEvent(event); } return true; } protected abstract void handleInProgressEvent(MotionEvent event); protected abstract void handleStartProgressEvent(MotionEvent event); protected abstract void updateStateByEvent(MotionEvent event); protected void resetState() { if (mPreMotionEvent != null) { mPreMotionEvent.recycle(); mPreMotionEvent = null; } if (mCurrentMotionEvent != null) { mCurrentMotionEvent.recycle(); mCurrentMotionEvent = null; } mGestureInProgress = false; } }

    你可能會(huì)說,1個(gè)move手勢弄這么多代碼,太麻煩了。的確是的,move手勢的檢測非常簡單,那末之所以這么寫呢,主要是為了可以復(fù)用,比如現(xiàn)在有1堆的XXXGestureDetector,當(dāng)我們需要監(jiān)聽甚么手勢,就直接拿個(gè)detector來檢測多方便。我相信大家肯定也愁悶過Google,為何只有ScaleGestureDetector而沒有RotateGestureDetector呢。

根據(jù)上述,大家應(yīng)當(dāng)理解了為何要這么做,當(dāng)時(shí)不強(qiáng)迫,每一個(gè)人都有個(gè)性。

不過值得1提的是:上面這個(gè)手勢檢測的寫法,不是我想的,而是1個(gè)開源的項(xiàng)目https://github.com/rharter/android-gesture-detectors,里面包括很多的手勢檢測。對應(yīng)的博文是:http://code.almeros.com/android-multitouch-gesture-detectors#.VibzzhArJXg那面上面兩個(gè)類就是我偷學(xué)了的~ 哈

4、測試

測試其實(shí)沒撒好說的了,就是把我們的LargeImageView放入布局文件,然后Activity里面去設(shè)置inputstream了。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.zhy.blogcodes.largeImage.view.LargeImageView android:id="@+id/id_largetImageview" android:layout_width="match_parent" android:layout_height="match_parent"/> RelativeLayout>

然后在Activity里面去設(shè)置圖片:

package com.zhy.blogcodes.largeImage; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.zhy.blogcodes.R; import com.zhy.blogcodes.largeImage.view.LargeImageView; import java.io.IOException; import java.io.InputStream; public class LargeImageViewActivity extends AppCompatActivity { private LargeImageView mLargeImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_large_image_view); mLargeImageView = (LargeImageView) findViewById(R.id.id_largetImageview); try { InputStream inputStream = getAssets().open("world.jpg"); mLargeImageView.setInputStream(inputStream); } catch (IOException e) { e.printStackTrace(); } } }

效果圖:

ok,那末到此,顯示巨圖的方案和詳細(xì)的代碼就描寫完成了,整體還是非常簡單的。
但是,在實(shí)際的項(xiàng)目中,可能會(huì)有更多的需求,比如增加放大、縮??;增加快滑手勢等等,那末大家可以去參考這個(gè)庫:https://github.com/johnnylambada/WorldMap,該庫基本實(shí)現(xiàn)了絕大多數(shù)的需求,大家根據(jù)本文這個(gè)思路再去看這個(gè)庫,也會(huì)簡單很多,定制起來也容易。我這個(gè)地圖的圖就是該庫里面提供的。

哈,掌握了這個(gè),以后面試進(jìn)程中也能夠悄悄的裝1把了,當(dāng)你優(yōu)雅的答完android加載圖片的方案以后,然后接1句,其實(shí)還有1種情況,就是高清顯示巨圖,那末我們應(yīng)當(dāng)…相信面試官對你的印象會(huì)好很多~ have a nice day ~


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美日韩中文一区 | 中文字幕亚洲欧美日韩高清 | aⅴ天堂网| 日韩 欧美 亚洲 | 亚洲一区二区三区影院 | 国产成+人欧美+综合在线观看 | 男女激情视频在线观看 | 亚洲无av码一区二区三区 | 在线观看中文字幕 | 九九精品久久久久久久久 | 亚洲男人的天堂久久精品 | 国产一二三区有声小说 | 国产一区二区久久精品 | 尤物视频在线观看视频 | 精品国产欧美一区二区三区成人 | 久久免费观看国产精品 | 国产高清在线精品一区在线 | 国产成人精品久久一区二区小说 | 久久精品国产主播一区二区 | 精品一区二区三区在线视频观看 | 亚洲婷婷影院 | 宅男午夜 | 奇奇午夜理伦三级 | 免费爽视频 | 欧美亚洲国产精品久久久 | 国产婷婷丁香久久综合 | 国内精品视频免费观看 | 午夜精品久久久久久久2023 | 一级做a爰全过程免费视频毛片 | 亚洲高清影院 | www.色网| 91精品成人免费国产片 | 老司机深夜福利在线 | 精品国产亚洲一区二区在线3d | 午夜三级在线 | 国产97在线 | 亚洲 | 最近中文字幕在线播放 | 日本大臿亚洲香蕉大片 | 欧美中日韩在线 | 国产免费亚洲 | 琪琪在线观看 |