【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】安卓 App雙擊放大和縮小圖片功能_第1頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】安卓 App雙擊放大和縮小圖片功能_第2頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】安卓 App雙擊放大和縮小圖片功能_第3頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】安卓 App雙擊放大和縮小圖片功能_第4頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】安卓 App雙擊放大和縮小圖片功能_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】安卓App雙擊放大和縮小圖片功能

直接上代碼:publicstaticBitmapscale(Bitmapbitmap,floatscaleWidth,floatscaleHeight){

intwidth=bitmap.getWidth();

intheight=bitmap.getHeight();

Matrixmatrix=newMatrix();

matrix.postScale(scaleWidth,scaleHeight);

Log.i(TAG,"scaleWidth:"+scaleWidth+",scaleHeight:"+scaleHeight);

returnBitmap.createBitmap(bitmap,0,0,width,height,matrix,true);}注意要比例設(shè)置正確否則可能會(huì)內(nèi)存溢出,比如曾經(jīng)使用圖片縮放時(shí)遇到這么個(gè)問(wèn)題:1java.lang.IllegalArgumentException:bitmapsizeexceeds32bits后來(lái)一行行查代碼,發(fā)現(xiàn)原來(lái)是scale的比例計(jì)算錯(cuò)誤,將原圖給放大了20多倍,導(dǎo)致內(nèi)存溢出所致,重新修改比例值后就正常了。好了,下面真正來(lái)看一下這個(gè)實(shí)現(xiàn)了放大和原大兩個(gè)級(jí)別的縮放的模塊。功能有:以觸摸點(diǎn)為中心放大(這個(gè)是網(wǎng)上其他的代碼沒(méi)有的)邊界控制(這個(gè)是網(wǎng)上其他的代碼沒(méi)有的)雙擊放大或縮?。ㄖ饕紤]到電阻屏)多點(diǎn)觸摸放大和縮小這個(gè)模塊已經(jīng)通過(guò)了測(cè)試,并且用戶也使用有一段時(shí)間了,是屬于比較穩(wěn)定的了。下面貼上QANDA.REN代碼及使用方法(沒(méi)有寫(xiě)測(cè)試項(xiàng)目,大家見(jiàn)諒):ImageControl類似一個(gè)用戶自定義的ImageView控件。用法將在下面的代碼中貼出。importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.Matrix;importandroid.util.AttributeSet;importandroid.util.FloatMath;importandroid.view.MotionEvent;importandroid.widget.ImageView;

publicclassImageControlextendsImageView{

publicImageControl(Contextcontext){

super(context);

//TODOAuto-generatedconstructorstub

}

publicImageControl(Contextcontext,AttributeSetattrs){

super(context,attrs);

//TODOAuto-generatedconstructorstub

}

publicImageControl(Contextcontext,AttributeSetattrs,intdefStyle){

super(context,attrs,defStyle);

//TODOAuto-generatedconstructorstub

}

//ImageViewimg;

MatriximgMatrix=null;//定義圖片的變換矩陣

staticfinalintDOUBLE_CLICK_TIME_SPACE=300;//雙擊時(shí)間間隔

staticfinalintDOUBLE_POINT_DISTANCE=10;//兩點(diǎn)放大兩點(diǎn)間最小間距

staticfinalintNONE=0;

staticfinalintDRAG=1;//拖動(dòng)操作

staticfinalintZOOM=2;//放大縮小操作

privateintmode=NONE;//當(dāng)前模式

floatbigScale=3f;//默認(rèn)放大倍數(shù)

BooleanisBig=false;//是否是放大狀態(tài)

longlastClickTime=0;//單擊時(shí)間

floatstartDistance;//多點(diǎn)觸摸兩點(diǎn)距離

floatendDistance;//多點(diǎn)觸摸兩點(diǎn)距離

floattopHeight;//狀態(tài)欄高度和標(biāo)題欄高度

BitmapprimaryBitmap=null;

floatcontentW;//屏幕內(nèi)容區(qū)寬度

floatcontentH;//屏幕內(nèi)容區(qū)高度

floatprimaryW;//原圖寬度

floatprimaryH;//原圖高度

floatscale;//適合屏幕縮放倍數(shù)

BooleanisMoveX=true;//是否允許在X軸拖動(dòng)

BooleanisMoveY=true;//是否允許在Y軸拖動(dòng)

floatstartX;

floatstartY;

floatendX;

floatendY;

floatsubX;

floatsubY;

floatlimitX1;

floatlimitX2;

floatlimitY1;

floatlimitY2;

ICustomMethodmCustomMethod=null;

/**

*初始化圖片

*

*@parambitmap

*

要顯示的圖片

*@paramcontentW

*

內(nèi)容區(qū)域?qū)挾?/p>

*@paramcontentH

*

內(nèi)容區(qū)域高度

*@paramtopHeight

*

狀態(tài)欄高度和標(biāo)題欄高度之和

*/

publicvoidp_w_picpathInit(Bitmapbitmap,intcontentW,intcontentH,

inttopHeight,ICustomMethodiCustomMethod){

this.primaryBitmap=bitmap;

this.contentW=contentW;

this.contentH=contentH;

this.topHeight=topHeight;

mCustomMethod=iCustomMethod;

primaryW=primaryBitmap.getWidth();

primaryH=primaryBitmap.getHeight();

floatscaleX=(float)contentW/primaryW;

floatscaleY=(float)contentH/primaryH;

scale=scaleX<scaleY?scaleX:scaleY;

if(scale<1&&1/scale<bigScale){

bigScale=(float)(1/scale+0.5);

}

imgMatrix=newMatrix();

subX=(contentW-primaryW*scale)/2;

subY=(contentH-primaryH*scale)/2;

this.setImageBitmap(primaryBitmap);

this.setScaleType(ScaleType.MATRIX);

imgMatrix.postScale(scale,scale);

imgMatrix.postTranslate(subX,subY);

this.setImageMatrix(imgMatrix);

}

/**

*按下操作

*

*@paramevent

*/

publicvoidmouseDown(MotionEventevent){

mode=NONE;

startX=event.getRawX();

startY=event.getRawY();

if(event.getPointerCount()==1){

//如果兩次點(diǎn)擊時(shí)間間隔小于一定值,則默認(rèn)為雙擊事件

if(event.getEventTime()-lastClickTime<DOUBLE_CLICK_TIME_SPACE){

changeSize(startX,startY);

}elseif(isBig){

mode=DRAG;

}

}

lastClickTime=event.getEventTime();

}

/**

*非第一個(gè)點(diǎn)按下操作

*

*@paramevent

*/

publicvoidmousePointDown(MotionEventevent){

startDistance=getDistance(event);

if(startDistance>DOUBLE_POINT_DISTANCE){

mode=ZOOM;

}else{

mode=NONE;

}

}

/**

*移動(dòng)操作

*

*@paramevent

*/

publicvoidmouseMove(MotionEventevent){

if((mode==DRAG)&&(isMoveX||isMoveY)){

float[]XY=getTranslateXY(imgMatrix);

floattransX=0;

floattransY=0;

if(isMoveX){

endX=event.getRawX();

transX=endX-startX;

if((XY[0]+transX)<=limitX1){

transX=limitX1-XY[0];

}

if((XY[0]+transX)>=limitX2){

transX=limitX2-XY[0];

}

}

if(isMoveY){

endY=event.getRawY();

transY=endY-startY;

if((XY[1]+transY)<=limitY1){

transY=limitY1-XY[1];

}

if((XY[1]+transY)>=limitY2){

transY=limitY2-XY[1];

}

}

imgMatrix.postTranslate(transX,transY);

startX=endX;

startY=endY;

this.setImageMatrix(imgMatrix);

}elseif(mode==ZOOM&&event.getPointerCount()>1){

endDistance=getDistance(event);

floatdif=endDistance-startDistance;

if(Math.abs(endDistance-startDistance)>DOUBLE_POINT_DISTANCE){

if(isBig){

if(dif<0){

changeSize(0,0);

mode=NONE;

}

}elseif(dif>0){

floatx=event.getX(0)/2+event.getX(1)/2;

floaty=event.getY(0)/2+event.getY(1)/2;

changeSize(x,y);

mode=NONE;

}

}

}

}

/**

*鼠標(biāo)抬起事件

*/

publicvoidmouseUp(){

mode=NONE;

}

/**

*圖片放大縮小

*

*@paramx

*

點(diǎn)擊點(diǎn)X坐標(biāo)

*@paramy

*

點(diǎn)擊點(diǎn)Y坐標(biāo)

*/

privatevoidchangeSize(floatx,floaty){

if(isBig){

//如果處于最大狀態(tài),則還原

imgMatrix.reset();

imgMatrix.postScale(scale,scale);

imgMatrix.postTranslate(subX,subY);

isBig=false;

}else{

imgMatrix.postScale(bigScale,bigScale);//在原有矩陣后乘放大倍數(shù)

floattransX=-((bigScale-1)*x);

floattransY=-((bigScale-1)*(y-topHeight));//(bigScale-1)(y-statusBarHeight-subY)+2*subY;

floatcurrentWidth=primaryW*scale*bigScale;//放大后圖片大小

floatcurrentHeight=primaryH*scale*bigScale;

//如果圖片放大后超出屏幕范圍處理

if(currentHeight>contentH){

limitY1=-(currentHeight-contentH);//平移限制

limitY2=0;

isMoveY=true;//允許在Y軸上拖動(dòng)

floatcurrentSubY=bigScale*subY;//當(dāng)前平移距離

//平移后,內(nèi)容區(qū)域上部有空白處理辦法

if(-transY<currentSubY){

transY=-currentSubY;

}

//平移后,內(nèi)容區(qū)域下部有空白處理辦法

if(currentSubY+transY<limitY1){

transY=-(currentHeight+currentSubY-contentH);

}

}else{

//如果圖片放大后沒(méi)有超出屏幕范圍處理,則不允許拖動(dòng)

isMoveY=false;

}

if(currentWidth>contentW){

limitX1=-(currentWidth-contentW);

limitX2=0;

isMoveX=true;

floatcurrentSubX=bigScale*subX;

if(-transX<currentSubX){

transX=-currentSubX;

}

if(currentSubX+transX<limitX1){

transX=-(currentWidth+currentSubX-contentW);

}

}else{

isMoveX=false;

}

imgMatrix.postTranslate(transX,transY);

isBig=true;

}

this.setImageMatrix(imgMatrix);

if(mCustomMethod!=null){

mCustomMethod.customMethod(isBig);

}

}

/**

*獲取變換矩陣中X軸偏移量和Y軸偏移量

*

*@parammatrix

*

變換矩陣

*@return

*/

privatefloat[]getTranslateXY(Matrixmatrix){

float[]values=newfloat[9];

matrix.getValues(values);

float[]floats=newfloat[2];

floats[0]=values[Matrix.MTRANS_X];

floats[1]=values[Matrix.MTRANS_Y];

returnfloats;

}

/**

*獲取兩點(diǎn)間的距離

*

*@paramevent

*@return

*/

privatefloatgetDistance(MotionEventevent){

floatx=event.getX(0)-event.getX(1);

floaty=event.getY(0)-event.getY(1);

returnFloatMath.sqrt(x*x+y*y);

}

/**

*@authorAdministrator用戶自定義方法

*/

publicinterfaceICustomMethod{

publicvoidcustomMethod(BooleancurrentStatus);

}}

ImageVewActivity這個(gè)用于測(cè)試的Activityimportandroid.app.Activity;importandroid.graphics.Bitmap;importandroid.graphics.Rect;importandroid.graphics.drawable.BitmapDrawable;importandroid.os.Bundle;importandroid.view.MotionEvent;importandroid.view.View;importandroid.widget.LinearLayout;importandroid.widget.TextView;importandroid.widget.Toast;importejiang.boiler.ImageControl.ICustomMethod;importejiang.boiler.R.id;

publicclassImageViewActivityextendsActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

//TODOAuto-generatedmethodstub

super.onCreate(savedInstanceState);

setContentView(R.mon_p_w_picpath_view);

findView();

}

publicvoidonWindowFocusChanged(booleanhasFocus){

super.onWindowFocusChanged(hasFocus);

init();

}

ImageControlimgControl;

LinearLayoutllTitle;

TextViewtvTitle;

privatevoidfindView(){

imgControl=(ImageControl)findViewById(mon_p_w_picpathview_p_w_picpathControl1);

llTitle=(LinearLayout)findViewById(mon_p_w_picpathview_llTitle);

tvTitle=(TextView)findViewById(mon_p_w_picpathview_title);

}

privatevoidinit(){

tvTitle.setText("圖片測(cè)試");

//這里可以為imgcontrol的圖片路徑動(dòng)態(tài)賦值

//

Bitmapbmp;

if(imgControl.getDrawingCache()!=null){

bmp=Bitmap.createBitmap(imgControl.getDrawingCache());

}else{

bmp=((BitmapDrawable)imgControl.getDrawable()).getBitmap();

}

Rectframe=newRect();

getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

intstatusBarHeight=frame.top;

intscreenW=this.getWindowManager().getDefaultDisplay().getWidth();

intscreenH=this.getWindowManager().getDefaultDisplay().getHeight()

-statusBarHeight;

if(bmp!=null){

imgControl.p_w_picpathInit(bmp,screenW,screenH,statusBarHeight,

newICustomMethod(){

@Override

publicvoidcustomMethod(BooleancurrentStatus){

//當(dāng)圖片處于放大或縮小狀態(tài)時(shí),控制標(biāo)題是否顯示

if(currentStatus){

llTitle.setVisibility(View.GONE);

}else{

llTitle.setVisibility(View.VISIBLE);

}

}

});

}

else

{

Toast.makeText(ImageViewActivity.this,"圖片加載失敗,請(qǐng)稍候再試!",Toast.LENGTH_SHORT)

.show();

}

}

@Override

publicbooleanonTouchEvent(MotionEventevent){

switch(event.getAction()&MotionEvent.ACTION_MASK){

caseMotionEvent.ACTION_DOWN:

imgControl.mouseDown(event);

break;

/**

*非第一個(gè)點(diǎn)按下

*/

caseMotionEvent.ACTION_POINTER_DOWN:

imgControl.mousePointDown(event);

break;

caseMotionEvent.ACTION_MOVE:

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論