前兩天在面試當中被問到有沒有做過加載高清大圖,當時確實沒有做過,聽面試官提到可以動態(tài)加載圖片的顯示區(qū)域?;貋砗笤诰W上找到了一篇鴻洋大神的博文悔啊-_-!為什么早點沒有看到。廢話不多說代碼如下:
一、BitmapRegionDecoder
BitmapRegionDecoder主要用于顯示圖片的某一塊矩形區(qū)域,所以可以利用它來完成大圖片的動態(tài)區(qū)域顯示。
- 簡單用法:
- BitmapRegionDecoder提供了一系列的newInstance方法來構造對象,支持傳入文件路徑,文件描述符,文件的inputstrem等。
例如:
- BitmapRegionDecoder提供了一系列的newInstance方法來構造對象,支持傳入文件路徑,文件描述符,文件的inputstrem等。
BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder
.newInstance(is, false);
- BitmapRegionDecoder.decodeRegion()方法,通過傳入矩形區(qū)域即可顯示圖片的指定區(qū)域。
Bitmap bitmap = bitmapRegionDecoder.decodeRegion(rect, options);
參數一為矩形區(qū)域,參數二為BitmapFactory.Options。
- 一個簡單的使用例子:
// load Assets image
try {
InputStream is = getAssets().open("qm.jpg");
BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
tmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, tmpOptions);
int width = tmpOptions.outWidth;
int height = tmpOptions.outHeight;
// 設置顯示圖片的中心區(qū)域
BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder
.newInstance(is, false);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// JPG圖片沒有Aphla通道使用該顏色模式更加節(jié)省內存
options.inPreferredConfig = Bitmap.Config.RGB_565;
// 設置顯示區(qū)域的矩形大小
Rect rect = new Rect(
width / 2 - 100, height / 2 - 100,
width / 2 + 100, height / 2 + 100);
// 通過BitmapRegionDecoder來解析顯示區(qū)域的圖像
Bitmap bitmap = bitmapRegionDecoder.decodeRegion(rect, options);
mShow.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
一個簡單的使用例子,整體自定義View的設計思想,即是通過滑動時獲得的坐標不斷設置矩形區(qū)域的偏移坐標,然后不斷繪制View來完成動態(tài)顯示大圖的效果。具體代碼大家可以自行參考鴻洋大神的博客,我這里將我注釋的代碼分享給大家。
二、LargeImageView
- 首先手勢檢測的兩個類:BaseGestureDetector、MoveGestureDetector。
public abstract class BaseGestureDetector {
protected boolean mGestureInProgress;
protected MotionEvent mPreMotionEvent;
protected MotionEvent mCurrentMotionEvent;
protected Context mContext;
public BaseGestureDetector(Context context) {
mContext = context;
}
public boolean onTouchEvent(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;
}
}
這里有一個關于抽象類的構造方法需要提一下,具體說明大家可以自行移至這里,結論即是抽象類的子類在創(chuàng)建對象時,也會跟非抽象類的子類一樣,都會默認調用父類的無參構造方法。
public class MoveGestureDetector extends BaseGestureDetector {
private PointF mCurrentPointer;
private PointF mPrePointer;
//僅僅為了減少創(chuàng)建內存
private PointF mDeltaPointer = new PointF();
//用于記錄最終結果,并返回
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 ,保險起見
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;
}
/**
* 根據event計算多指中心點
*
* @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)
{
}
}
}
關于這兩個檢測手勢移動的類我還沒有研究,各位同學可以自行研究。
- 最后自定義View的代碼如下:
public class LargeImageView extends View {
private BitmapRegionDecoder mDecoder;
/**
* 圖片的寬高
*/
private int mImageWidth,mImageHeight;
/**
* 矩形顯示的區(qū)域(volatile修飾的對象線程安全)
*/
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 LargeImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setInputStream(InputStream is) {
try {
mDecoder = BitmapRegionDecoder.newInstance(is, false);
BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
tmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, tmpOptions);
mImageWidth = tmpOptions.outWidth;
mImageHeight = tmpOptions.outHeight;
// Log.d("xns", "width:" + mImageWidth + " ,height:" + mImageHeight);
// 重新布局View
requestLayout();
invalidate();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
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();
// 只要圖片在X或Y方向上大于View的寬高才進行矩形區(qū)域的偏移
if (mImageWidth > getWidth()) {
mRect.offset(-moveX, 0);
checkWidth();
// 偏移后重新繪制顯示圖片
invalidate();
}
if (mImageHeight > getHeight())
{
mRect.offset(0, -moveY);
checkHeight();
invalidate();
}
return true;
}
});
}
private void checkHeight() {
Rect rect = mRect;
int imageHeight = mImageHeight;
if (rect.bottom > imageHeight)
{
rect.bottom = imageHeight;
rect.top = imageHeight - getHeight();
}
if (rect.top < 0)
{
rect.top = 0;
rect.bottom = getHeight();
}
}
private void checkWidth() {
Rect rect = mRect;
int imageWidth = mImageWidth;
if (rect.right > imageWidth) {
rect.right = imageWidth;
rect.left = imageWidth - getWidth();
}
if (rect.left < 0) {
rect.left = 0;
rect.right = getWidth();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//Log.d("xns", "x:" + event.getX() + " ,y:" + event.getY());
mDetector.onTouchEvent(event);
return true;
}
@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ū)域
mRect.left = imageWidth / 2 - width / 2;
mRect.top = imageHeight / 2 - height / 2;
mRect.right = mRect.left + width;
mRect.bottom = mRect.top + height;
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = mDecoder.decodeRegion(mRect, options);
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
- 使用代碼如下:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private LargeImageView mLIv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLIv = (LargeImageView) findViewById(R.id.iv_large);
// load Assets image
try {
InputStream is = getAssets().open("qm.jpg");
// 調用自定義大圖加載View
mLIv.setInputStream(is);
} catch (IOException e) {
e.printStackTrace();
}
}
}
寫在最后
最近找工作找的很煩躁,最后寫給自己的寄語,以此激勵自己一下,靜心思考,勇往直前。