Android实现ImageView图片双击放大及缩小
本文实例介绍了Android实现ImageView图片双击放大及缩小的相关技巧,分享给大家供大家参考,具体内容如下
publicclassDoubleScaleImageViewextendsImageViewimplementsOnTouchListener,OnGlobalLayoutListener{ privatebooleanisFirst=false; privatefloatdoubleScale;//双击放大的值 privateMatrixmScaleMatrix; privatefloatdefaultScale;//默认的缩放值 privateintmLastPinterCount;//记录上一次多点触控的数量 privatefloatmLastX; privatefloatmLastY; privateintmTouchSlop; privatebooleanisCanDrag; privatebooleanisCheckLeft; privatebooleanisCheckTop; privateGestureDetectormGestureDetector; publicDoubleScaleImageView(Contextcontext){ this(context,null); } publicDoubleScaleImageView(Contextcontext,AttributeSetattrs){ this(context,attrs,0); } @SuppressLint("ClickableViewAccessibility") publicDoubleScaleImageView(Contextcontext,AttributeSetattrs,intdefStyleAttr){ super(context,attrs,defStyleAttr); mScaleMatrix=newMatrix(); setScaleType(ScaleType.MATRIX); setOnTouchListener(this); //getScaledTouchSlop是一个距离,表示滑动的时候,手的移动要大于这个距离才开始移动控件。如果小于这个距离就不触发移动控件 mTouchSlop=ViewConfiguration.get(context).getScaledTouchSlop(); mGestureDetector=newGestureDetector(context,newGestureDetector.SimpleOnGestureListener(){ @Override publicbooleanonDoubleTap(MotionEvente){ floatx=e.getX(); floaty=e.getY(); if(getScale()<doubleScale){ mScaleMatrix.postScale(doubleScale/getScale(),doubleScale/getScale(),x,y);//放大 } else{ mScaleMatrix.postScale(defaultScale/getScale(),defaultScale/getScale(),x,y);//缩小 } setImageMatrix(mScaleMatrix); returnsuper.onDoubleTap(e); } }); } @Override protectedvoidonAttachedToWindow(){//view附加到窗体上时调用该方法 super.onAttachedToWindow(); getViewTreeObserver().addOnGlobalLayoutListener(this); } @SuppressWarnings("deprecation") @Override protectedvoidonDetachedFromWindow(){//将视图从窗体上分离的时候调用该方法。 super.onDetachedFromWindow(); getViewTreeObserver().removeGlobalOnLayoutListener(this); } @Override publicvoidonGlobalLayout(){//在这个方法中获取ImageView加载完成后的图片 if(!isFirst){ //获取控件的宽度和高度 intwidth=getWidth(); intheight=getHeight(); //得到我们的图片以及图片的宽度及高度 Drawabledrawable=getDrawable(); if(drawable==null){return;} intimageWidth=drawable.getIntrinsicWidth();//图片的宽度 intimageHeight=drawable.getIntrinsicHeight();//图片的高度 floatscale=1.0f; //如果图片宽度大于控件宽度,但是图片高度小于控件高度,我们要缩小图片 if(imageWidth>width&&imageHeight<height){ scale=width*1.0f/imageWidth; } //如果图片宽度小于控件宽度,但是图片高度大于控件高度,我们要缩小图片 if(imageWidth<width&&imageHeight>height){ scale=height*1.0f/imageHeight; } //如果图片的宽度都大于或小于控件宽度,我们则要对图片进行对应缩放,保证图片占满控件 if((imageWidth>width&&imageHeight>height)||(imageWidth<width&&imageHeight<height)){ scale=Math.min(width*1.0f/imageWidth,height*1.0f/imageHeight); } //初始化对应的缩放值 defaultScale=scale; doubleScale=defaultScale*2; //图片缩放后,将图片要移动到控件中心 intdx=width/2-imageWidth/2; intdy=height/2-imageHeight/2; mScaleMatrix.postTranslate(dx,dy); mScaleMatrix.postScale(defaultScale,defaultScale,width/2,height/2); setImageMatrix(mScaleMatrix); isFirst=true; } } @SuppressLint("ClickableViewAccessibility") @Override publicbooleanonTouch(Viewv,MotionEventevent){ if(mGestureDetector.onTouchEvent(event)){returntrue;} floatx=0; floaty=0; intpointerCount=event.getPointerCount();//获取放在屏幕上的手指数量 for(inti=0;i<pointerCount;i++){ x+=event.getX(i); y+=event.getY(i); } x/=pointerCount; y/=pointerCount; if(mLastPinterCount!=pointerCount){ isCanDrag=false; mLastX=x; mLastY=y; } mLastPinterCount=pointerCount; switch(event.getAction()){ caseMotionEvent.ACTION_MOVE: floatdx=x-mLastX; floatdy=y-mLastY; isCanDrag=isMove(dx,dy); if(isCanDrag){ RectFrectf=getMatrixRectf(); if(null!=getDrawable()){ isCheckLeft=isCheckTop=true; if(rectf.width()<getWidth()){//如果图片宽度小于控件宽度(屏幕宽度)不允许横向移动 dx=0; isCheckLeft=false; } if(rectf.height()<getHeight()){//如果图片高度小于控件高度(屏幕高度)不允许纵向移动 dy=0; isCheckTop=false; } mScaleMatrix.postTranslate(dx,dy); checkTranslateWithBorder(); setImageMatrix(mScaleMatrix); } } mLastX=x; mLastY=y; break; caseMotionEvent.ACTION_UP: caseMotionEvent.ACTION_CANCEL: mLastPinterCount=0; break; } returntrue; } /** *移动图片时进行边界检查 *@description: *@date2016-1-8下午4:02:24 */ privatevoidcheckTranslateWithBorder(){ RectFrectf=getMatrixRectf(); floatdelX=0; floatdelY=0; intwidth=getWidth(); intheight=getHeight(); if(rectf.top>0&&isCheckTop){ delY=-rectf.top; } if(rectf.bottom<height&&isCheckTop){ delY=height-rectf.bottom; } if(rectf.left>0&&isCheckLeft){ delX=-rectf.left; } if(rectf.right<width&&isCheckLeft){ delX=width-rectf.right; } mScaleMatrix.postTranslate(delX,delY); } //判断是否有移动 privatebooleanisMove(floatx,floaty){ returnMath.sqrt(x*x+y*y)>mTouchSlop; } /** *获取图片的位置 *@description: *@date2016-1-8上午9:02:10 */ privateRectFgetMatrixRectf(){ Matrixmatrix=mScaleMatrix; RectFrecft=newRectF(); if(getDrawable()!=null){ recft.set(0,0,getDrawable().getIntrinsicWidth(),getDrawable().getIntrinsicHeight()); matrix.mapRect(recft); } returnrecft; } //获取当前图片的缩放值 privatefloatgetScale(){ floatvalues[]=newfloat[9]; mScaleMatrix.getValues(values); returnvalues[Matrix.MSCALE_X]; } }
以上就是安卓实现ImageView图片双击放大及缩小的全部代码,希望对大家的学习有所帮助。