Android(安卓)之多点触摸 手势拉伸放大缩小图片 并在ImageView上画圆及相框
16lz
2021-01-25
效果图如下图所示:
写入代码:
1.首先创建Activity
- 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);
- }
- }
main.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_height="fill_parent"
- android:layout_width="fill_parent"android:gravity="top"android:id="@+id/twill"
- android:background="@android:color/transparent">
- <Gallery
- android:id="@+id/myggg"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
2. 可移动的ImageView 需要重写ImageView
- 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;
- }
- floatglowX=30;
- floatglowY=30;
- floatradius=20;
- @Override
- publicvoiddraw(Canvascanvas){
- super.draw(canvas);
- if(drawGlow)
- canvas.drawCircle(glowX,glowY,radius,paint);
- }
- Paintpaint=newPaint();
- {
- paint.setAntiAlias(true);
- paint.setColor(Color.RED);
- paint.setAlpha(100);
- };
- booleandrawGlow=false;
- /**
- *就算两点间的距离
- */
- 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:
- drawGlow=true;
- 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:
- /*判断是否超出范围并处理*/
- drawGlow=false;
- 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;
- }
- glowX=event.getX();
- glowY=event.getY();
- this.invalidate();
- 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);
- }
- }
3.
- /**
- *一个绝对布局
- *@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);
- }
4.Gallery上的被画上相框的ImageView
- packagecom.app;
- 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);
- }
- 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;
- }
- }
5.Gallery的适配器
- packagecom.app;
- 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;
- }
- }
更多相关文章
- Android(安卓)图片OutOfMemory异常bitmap size exceeds VM budge
- android绘制圆形图片、圆圈以及图片缩放
- GestureDetector.OnGestureListener 详解
- android 胡言乱语 2 android UI
- 网页 Android(安卓)套壳
- android 图片的缩放,bitmap的用法
- 完整版仿360等手机卫士火箭拖动版
- Android(安卓)Matrix源码详解
- Android通过webview调起微信和支付宝app进行支付