Android编程实现图片的浏览、缩放、拖动和自动居中效果
本文实例讲述了Android编程实现图片的浏览、缩放、拖动和自动居中效果的方法。分享给大家供大家参考,具体如下:
Touch.java
/** *图片浏览、缩放、拖动、自动居中 */ publicclassTouchextendsActivityimplementsOnTouchListener{ Matrixmatrix=newMatrix(); MatrixsavedMatrix=newMatrix(); DisplayMetricsdm; ImageViewimgView; Bitmapbitmap; floatminScaleR;//最小缩放比例 staticfinalfloatMAX_SCALE=4f;//最大缩放比例 staticfinalintNONE=0;//初始状态 staticfinalintDRAG=1;//拖动 staticfinalintZOOM=2;//缩放 intmode=NONE; PointFprev=newPointF(); PointFmid=newPointF(); floatdist=1f; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.scale); imgView=(ImageView)findViewById(R.id.imag);//获取控件 bitmap=BitmapFactory.decodeResource(getResources(),this.getIntent() .getExtras().getInt("IMG"));//获取图片资源 imgView.setImageBitmap(bitmap);//填充控件 imgView.setOnTouchListener(this);//设置触屏监听 dm=newDisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm);//获取分辨率 minZoom(); center(); imgView.setImageMatrix(matrix); } /** *触屏监听 */ publicbooleanonTouch(Viewv,MotionEventevent){ switch(event.getAction()&MotionEvent.ACTION_MASK){ //主点按下 caseMotionEvent.ACTION_DOWN: savedMatrix.set(matrix); prev.set(event.getX(),event.getY()); mode=DRAG; break; //副点按下 caseMotionEvent.ACTION_POINTER_DOWN: dist=spacing(event); //如果连续两点距离大于10,则判定为多点模式 if(spacing(event)>10f){ savedMatrix.set(matrix); midPoint(mid,event); mode=ZOOM; } break; caseMotionEvent.ACTION_UP: caseMotionEvent.ACTION_POINTER_UP: mode=NONE; break; caseMotionEvent.ACTION_MOVE: if(mode==DRAG){ matrix.set(savedMatrix); matrix.postTranslate(event.getX()-prev.x,event.getY() -prev.y); }elseif(mode==ZOOM){ floatnewDist=spacing(event); if(newDist>10f){ matrix.set(savedMatrix); floattScale=newDist/dist; matrix.postScale(tScale,tScale,mid.x,mid.y); } } break; } imgView.setImageMatrix(matrix); CheckView(); returntrue; } /** *限制最大最小缩放比例,自动居中 */ privatevoidCheckView(){ floatp[]=newfloat[9]; matrix.getValues(p); if(mode==ZOOM){ if(p[0]<minScaleR){ matrix.setScale(minScaleR,minScaleR); } if(p[0]>MAX_SCALE){ matrix.set(savedMatrix); } } center(); } /** *最小缩放比例,最大为100% */ privatevoidminZoom(){ minScaleR=Math.min( (float)dm.widthPixels/(float)bitmap.getWidth(), (float)dm.heightPixels/(float)bitmap.getHeight()); if(minScaleR<1.0){ matrix.postScale(minScaleR,minScaleR); } } privatevoidcenter(){ center(true,true); } /** *横向、纵向居中 */ protectedvoidcenter(booleanhorizontal,booleanvertical){ Matrixm=newMatrix(); m.set(matrix); RectFrect=newRectF(0,0,bitmap.getWidth(),bitmap.getHeight()); m.mapRect(rect); floatheight=rect.height(); floatwidth=rect.width(); floatdeltaX=0,deltaY=0; if(vertical){ //图片小于屏幕大小,则居中显示。大于屏幕,上方留空则往上移,下方留空则往下移 intscreenHeight=dm.heightPixels; if(height<screenHeight){ deltaY=(screenHeight-height)/2-rect.top; }elseif(rect.top>0){ deltaY=-rect.top; }elseif(rect.bottom<screenHeight){ deltaY=imgView.getHeight()-rect.bottom; } } if(horizontal){ intscreenWidth=dm.widthPixels; if(width<screenWidth){ deltaX=(screenWidth-width)/2-rect.left; }elseif(rect.left>0){ deltaX=-rect.left; }elseif(rect.right<screenWidth){ deltaX=screenWidth-rect.right; } } matrix.postTranslate(deltaX,deltaY); } /** *两点的距离 */ privatefloatspacing(MotionEventevent){ floatx=event.getX(0)-event.getX(1); floaty=event.getY(0)-event.getY(1); returnFloatMath.sqrt(x*x+y*y); } /** *两点的中点 */ privatevoidmidPoint(PointFpoint,MotionEventevent){ floatx=event.getX(0)+event.getX(1); floaty=event.getY(0)+event.getY(1); point.set(x/2,y/2); } }
scale.xml
<?xmlversion="1.0"encoding="utf-8"?> <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"> <ImageView android:id="@+id/imag" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:scaleType="matrix"> </ImageView> </FrameLayout>
希望本文所述对大家Android程序设计有所帮助。