android imageview 多点触碰MultiTouch实现图片拖拽移动缩放.docx
《android imageview 多点触碰MultiTouch实现图片拖拽移动缩放.docx》由会员分享,可在线阅读,更多相关《android imageview 多点触碰MultiTouch实现图片拖拽移动缩放.docx(13页珍藏版)》请在冰豆网上搜索。
androidimageview多点触碰MultiTouch实现图片拖拽移动缩放
刚用android手机发现手机自带的图片浏览器挺酷可以用手指移动缩放还有动画效果
Intentintent=newIntent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(recentFile),"image/*");
startActivity(intent);
就可以调用系统的图片浏览器查看手机上的图片了
于是想仿照着写一个
到网上看了不少资料大概分为两种实现方式
->源码
一种是利用Matrix的postTranslate和postScale方法分别进行移动和缩放
这种方式实质是对ImageView中的drawable进行缩放和移动
imageview组件本身并没有移动和缩放这种方法实现起来比较简单但是不知道如何获得经过移动后的drawable的坐标和大小比较郁闷因为调用imageview的各种方法拿到的都是其本身的大小和坐标
而另一种是直接对imageview进行操作,直接移动和改变组件本身的大小从而实现移动和缩放
核心类继承->ImageView并加入了一些动画效果
importandroid.content.Context;
importandroid.util.FloatMath;
importandroid.view.MotionEvent;
importandroid.view.animation.TranslateAnimation;
importandroid.widget.ImageView;
/**
*继承ImageView实现了多点触碰的拖动和缩放
*@authorAdministrator
*
*/
publicclassTouchViewextendsImageView
{
staticfinalintNONE=0;
staticfinalintDRAG=1;//拖动中
staticfinalintZOOM=2;//缩放中
staticfinalintBIGGER=3;//放大ing
staticfinalintSMALLER=4;//缩小ing
privateintmode=NONE;//当前的事件
privatefloatbeforeLenght;//两触点距离
privatefloatafterLenght;//两触点距离
privatefloatscale=0.04f;//缩放的比例XY方向都是这个值越大缩放的越快
privateintscreenW;
privateintscreenH;
/*处理拖动变量*/
privateintstart_x;
privateintstart_y;
privateintstop_x;
privateintstop_y;
privateTranslateAnimationtrans;//处理超出边界的动画
publicTouchView(Contextcontext,intw,inth)
{
super(context);
this.setPadding(0,0,0,0);
screenW=w;
screenH=h;
}
/**
*就算两点间的距离
*/
privatefloatspacing(MotionEventevent){
floatx=event.getX(0)-event.getX
(1);
floaty=event.getY(0)-event.getY
(1);
returnFloatMath.sqrt(x*x+y*y);
}
/**
*处理触碰..
*/
@Override
publicbooleanonTouchEvent(MotionEventevent)
{
switch(event.getAction()&MotionEvent.ACTION_MASK){
caseMotionEvent.ACTION_DOWN:
mode=DRAG;
stop_x=(int)event.getRawX();
stop_y=(int)event.getRawY();
start_x=(int)event.getX();
start_y=stop_y-this.getTop();
if(event.getPointerCount()==2)
beforeLenght=spacing(event);
break;
caseMotionEvent.ACTION_POINTER_DOWN:
if(spacing(event)>10f){
mode=ZOOM;
beforeLenght=spacing(event);
}
break;
caseMotionEvent.ACTION_UP:
/*判断是否超出范围并处理*/
intdisX=0;
intdisY=0;
if(getHeight()<=screenH||this.getTop()<0)
{
if(this.getTop()<0)
{
intdis=getTop();
this.layout(this.getLeft(),0,this.getRight(),0+this.getHeight());
disY=dis-getTop();
}
elseif(this.getBottom()>screenH)
{
disY=getHeight()-screenH+getTop();
this.layout(this.getLeft(),screenH-getHeight(),this.getRight(),screenH);
}
}
if(getWidth()<=screenW)
{
if(this.getLeft()<0)
{
disX=getLeft();
this.layout(0,this.getTop(),0+getWidth(),this.getBottom());
}
elseif(this.getRight()>screenW)
{
disX=getWidth()-screenW+getLeft();
this.layout(screenW-getWidth(),this.getTop(),screenW,this.getBottom());
}
}
if(disX!
=0||disY!
=0)
{
trans=newTranslateAnimation(disX,0,disY,0);
trans.setDuration(500);
this.startAnimation(trans);
}
mode=NONE;
break;
caseMotionEvent.ACTION_POINTER_UP:
mode=NONE;
break;
caseMotionEvent.ACTION_MOVE:
/*处理拖动*/
if(mode==DRAG){
if(Math.abs(stop_x-start_x-getLeft())<88&&Math.abs(stop_y-start_y-getTop())<85)
{
this.setPosition(stop_x-start_x,stop_y-start_y,stop_x+this.getWidth()-start_x,stop_y-start_y+this.getHeight());
stop_x=(int)event.getRawX();
stop_y=(int)event.getRawY();
}
}
/*处理缩放*/
elseif(mode==ZOOM){
if(spacing(event)>10f)
{
afterLenght=spacing(event);
floatgapLenght=afterLenght-beforeLenght;
if(gapLenght==0){
break;
}
elseif(Math.abs(gapLenght)>5f)
{
if(gapLenght>0){
this.setScale(scale,BIGGER);
}else{
this.setScale(scale,SMALLER);
}
beforeLenght=afterLenght;
}
}
}
break;
}
returntrue;
}
/**
*实现处理缩放
*/
privatevoidsetScale(floattemp,intflag){
if(flag==BIGGER){
this.setFrame(this.getLeft()-(int)(temp*this.getWidth()),
this.getTop()-(int)(temp*this.getHeight()),
this.getRight()+(int)(temp*this.getWidth()),
this.getBottom()+(int)(temp*this.getHeight()));
}elseif(flag==SMALLER){
this.setFrame(this.getLeft()+(int)(temp*this.getWidth()),
this.getTop()+(int)(temp*this.getHeight()),
this.getRight()-(int)(temp*this.getWidth()),
this.getBottom()-(int)(temp*this.getHeight()));
}
}
/**
*实现处理拖动
*/
privatevoidsetPosition(intleft,inttop,intright,intbottom){
this.layout(left,top,right,bottom);
}
}
装载类一个layout
importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.view.View;
importandroid.widget.AbsoluteLayout;
importandroid.widget.ImageView.ScaleType;
/**
*一个绝对布局
*@authorAdministrator
*
*/
@SuppressWarnings("deprecation")
publicclassViewScrollextendsAbsoluteLayout
{
privateintscreenW;//可用的屏幕宽
privateintscreenH;//可用的屏幕高总高度-上面组件的总高度
privateintimgW;//图片原始宽
privateintimgH;//图片原始高
privateTouchViewtv;
publicViewScroll(Contextcontext,intresId,ViewtopView)
{
super(context);
screenW=((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
screenH=((Activity)context).getWindowManager().getDefaultDisplay().getHeight()-(topView==null?
190:
topView.getBottom()+50);
tv=newTouchView(context,screenW,screenH);
tv.setImageResource(resId);
Bitmapimg=BitmapFactory.decodeResource(context.getResources(),resId);
imgW=img.getWidth();
imgH=img.getHeight();
intlayout_w=imgW>screenW?
screenW:
imgW;//实际显示的宽
intlayout_h=imgH>screenH?
screenH:
imgH;//实际显示的高
if(layout_w==screenW||layout_h==screenH)
tv.setScaleType(ScaleType.FIT_XY);
tv.setLayoutParams(newAbsoluteLayout.LayoutParams(layout_w,layout_h,layout_w==screenW?
0:
(screenW-layout_w)/2,layout_h==screenH?
0:
(screenH-layout_h)/2));
this.addView(tv);
}
}
Activity:
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.Gallery;
importandroid.widget.LinearLayout;
importandroid.widget.AdapterView.OnItemClickListener;
/**
*activity
*@authorAdministrator
*
*/
publicclassGalleryMainextendsActivityimplementsOnItemClickListener
{
privateViewScrolldetail;
privateImageAdapteria;
privateLinearLayoutll;
privateLinearLayout.LayoutParamsparm;
privateGalleryg;
@Override
protectedvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
g=(Gallery)findViewById(R.id.myggg);
ll=(LinearLayout)findViewById(R.id.twill);
parm=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
ia=newImageAdapter(this);
detail=newViewScroll(GalleryMain.this,ia.imgIds[0],g);
ll.addView(detail,parm);
g.setAdapter(ia);
g.setOnItemClickListener(this);
}
@Override
publicvoidonItemClick(AdapterView
>arg0,Viewarg1,intarg2,longarg3)
{
ll.removeView(detail);
detail=newViewScroll(GalleryMain.this,ia.imgIds[arg2],g);
ll.addView(detail,parm);
}
}
配合Gallery的适配器类:
importandroid.content.Context;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.Gallery;
importandroid.widget.ImageView;
importandroid.widget.ImageView.ScaleType;
/**
*Gallery的适配器类
*@authorAdministrator
*
*/
publicclassImageAdapterextendsBaseAdapter
{
/*图片素材*/
publicint[]imgIds={R.drawable.jpg,R.drawable.pic};
privateContextcontext;
publicImageAdapter(Contextcontext)
{
this.context=context;
}
@Override
publicintgetCount()
{
returnimgIds.length;
}
@Override
publicObjectgetItem(intposition)
{
returnnull;
}
@Override
publiclonggetItemId(intposition)
{
return0;
}
@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent)
{
ImageViewimg=newImageViewImp(context);
img.setImageResource(imgIds[position]);
img.setScaleType(ScaleType.CENTER);
img.setLayoutParams(newGallery.LayoutParams(155,150));
returnimg;
}
}
gallery中image的实现类:
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Matrix;
importandroid.graphics.Paint;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.view.MotionEvent;
importandroid.widget.ImageView;
/**
*ImageAdapter中ImageView的实现类
*@authorAdministrator
*
*/
publicclassImageViewImpextendsImageView
{
privateintalpha=250;
privatebooleanpressed=false;
publicImageViewImp(Contextcontext)
{
super(context);
}
Matrixm;
publicvoidshow()
{
newThread(){
publicvoidrun(){
inttime=2000;
try
{
pressed=true;
while(time>0)
{
Thread.sleep(200);
time-=200;
alpha-=25;
postInvalidate();
}
pressed=false;
}
catch(Exceptione)
{
e.printStackTrace();
}
};
}.start();
}
@Override
publicbooleanonTouchEvent(MotionEventevent)
{
if(event.getAction()==MotionEvent.ACTION_DOWN)
show();
returnfalse;
}
@Override
protectedvoidonDraw(Canvascanvas)
{
Paintp=newPaint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(10);
BitmapDrawablebd=(BitmapDrawable)getDrawable();
if(bd!
=null)
{
canvas.drawBitmap(imageScale(bd.getBitmap(),107,113),21,18,p);
}
canvas.drawBitmap(BitmapFactory.decodeResource(getContext().getResources(),R.drawable.kua),0,0,p);
if(isPressed())
{
canvas.drawRect(5,5,140,140,p);
}
if(pressed)
{
p.setAlpha(alpha);
canvas.drawRect(5,5,140,140,p);
}
}
publicstaticBitmapimageScale(Bitmapbitmap,intdst_w,intdst_h){
intsrc_w=bitmap.getWidth();
intsrc_h=bitmap.getHeight();
floatscale_w=((float)dst_w)/src_w;
floatscale_h=((float)dst_h)/src_h;
Matrixmatrix=newMatrix();
matrix.postScale(scale_w,scale_h);
Bitmapdstbmp=Bitmap.createBitmap(bitmap,0,0,src_w,src_h,matrix,true);
returndstbmp;
}
}
各种xml->布局->style->colo