本次最主要的是采用區(qū)域解碼器BitmapRegionDecoder以及使用到了GestureDetector
現(xiàn)在進入正題
先進行初始化
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int):super(context, attrs, defStyleAttr, defStyleRes){
mContext=context
init()
}
fun init(){
mRect= Rect();
gestureDetector=GestureDetector(mContext,this)
mOptions=BitmapFactory.Options()
setOnTouchListener(this)
mScroller= Scroller(mContext)
}
然后進行一些計算主要是計算出一些縮放比scale霎俩,以及我們要顯示的區(qū)域
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
scale = (measuredWidth / mImageWidth).toFloat()
if (scale < 0.1) {
scale = 2f
}
displayWidth = mImageWidth / 2
if (mImageWidth / 2 > measuredWidth) {
displayWidth = measuredWidth
}
displayHeight = (measuredHeight / scale).toInt()
mRect.top = 0
mRect.left = 0
mRect.bottom = displayHeight
mRect.right = displayWidth
mMatrix!!.setScale(scale,scale)
}
/*
BitmapRegionDecoder.newInstance(inputStream,false)
//重點說下isShareable:true:表示淺復(fù)制(shallow copy)它浅,false:表示深度復(fù)制(deep copy)
淺拷貝:是指在拷貝對象時脚曾,對于基本數(shù)據(jù)類型的變量會重新復(fù)制一份,而對于引用類型的變量只是對引用進行貝埃跷。深拷貝: 則是對對象及該對象關(guān)聯(lián)的對象內(nèi)容,都會進行一份拷貝(內(nèi)存中創(chuàng)建新的副本)邮利。
這個方法是主要是獲取圖片提供給別人調(diào)用的
*/
fun setImage(inputStream : InputStream){
mOptions.inJustDecodeBounds=true//主要是獲取bitmap一些信息例如寬高
BitmapFactory.decodeStream(inputStream,null,mOptions)
mImageHeight=mOptions.outHeight
mImageWidth=mOptions.outWidth
mOptions.inMutable=true//主要是做復(fù)用的
mOptions.inPreferredConfig=Bitmap.Config.RGB_565//沒有特殊要求的一般設(shè)置成這個占用內(nèi)存小
mOptions.inJustDecodeBounds=false
bitmapRegionDecoder= BitmapRegionDecoder.newInstance(inputStream,false)
}
//這里主要是開始繪制根據(jù)mRect的位置繪制出我們想要看到的地方弥雹,mOptions.inBitmap這個是做復(fù)用內(nèi)存
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
if(bitmapRegionDecoder==null){
return
}
mOptions.inBitmap=mBitmap
mBitmap= bitmapRegionDecoder.decodeRegion(mRect,mOptions)
canvas!!.drawBitmap(mBitmap!!,mMatrix,null)
}
//主要是我們手中滑動的時候
override fun onScroll(p0: MotionEvent?, p1: MotionEvent?, p2: Float, p3: Float): Boolean {
mRect.offset(p2.toInt(), p3.toInt())//直接改變Rect位置
exceptionalaseCertain()
return false
}
//處理一些極端情況例如到底部 頂部。左右兩邊
fun exceptionalaseCertain(){
if( mRect.top<0){
mRect.top=0
mRect.bottom=displayHeight
}
if(mRect.bottom>mImageHeight){
mRect.bottom=mImageHeight
mRect.top=mImageHeight-displayHeight
}
if(mRect.left<0){
mRect.left=0
mRect.right=displayWidth
}
if(mRect.right>mImageWidth){
mRect.left=mImageWidth-displayWidth
mRect.right=mImageWidth
}
invalidate()
}
/**
* 處理慣性問題
* @param e1 .手勢起點的移動事件
* @param e2 當前手勢點的移動事件
* @param p2 每秒移動的x點
* @param p3 每秒移動的y點
* @return
*/
override fun onFling(p0: MotionEvent?, p1: MotionEvent?, p2: Float, p3: Float): Boolean {
mScroller.fling(mRect.left,mRect.top, (-p2).toInt(), (-p3).toInt(),0,mImageWidth-displayWidth,0,mImageHeight-displayHeight)
return false
}
其調(diào)用的api
mBigView.setImage(inputStream);
好了本次是以Kotlin語言的做介紹的
這是demo地址
https://github.com/yang1992yff/longbitmap.git
里面包含了java和kotlin寫法