效果图如下图所示:

Android 之多点触摸 手势拉伸放大缩小图片 并在ImageView上画圆及相框_第1张图片 

 

写入代码:

1.首先创建Activity

 

            
  1. public class GalleryMain extends Activity implements OnItemClickListener  
  2. {  
  3.     private ViewScroll detail;  
  4.     private ImageAdapter ia;  
  5.     private LinearLayout ll;  
  6.     private LinearLayout.LayoutParams parm;  
  7.     private Gallery g;    
  8.     @Override 
  9.     protected void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.           
  14.         g = (Gallery) findViewById(R.id.myggg);  
  15.         ll = (LinearLayout) findViewById(R.id.twill);  
  16.         parm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);  
  17.         ia = new ImageAdapter(this);  
  18.         detail = new ViewScroll(GalleryMain.this, ia.imgIds[0],g);    
  19.         ll.addView(detail,parm);  
  20.         g.setAdapter(ia);  
  21.         g.setOnItemClickListener(this);  
  22.     }  
  23.       
  24.     @Override 
  25.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)  
  26.     {  
  27.         ll.removeView(detail);  
  28.         detail = new ViewScroll(GalleryMain.this, ia.imgIds[arg2],g);     
  29.         ll.addView(detail,parm);  
  30.     }  
  31. }  

 

main.xml

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"   
  3.    android:orientation="vertical" 
  4.     android:layout_height="fill_parent" 
  5.     android:layout_width="fill_parent" android:gravity="top" android:id="@+id/twill" 
  6.     android:background="@android:color/transparent"> 
  7. <Gallery   
  8.   android:id="@+id/myggg" 
  9.   android:layout_width="fill_parent" 
  10.   android:layout_height="wrap_content" 
  11. /> 
  12.  
  13. LinearLayout> 

 

2. 可移动的ImageView 需要重写ImageView

            
  1. public class TouchView extends ImageView {  
  2.     static final int NONE = 0;  
  3.     static final int DRAG = 1// 拖动中  
  4.     static final int ZOOM = 2// 缩放中  
  5.     static final int BIGGER = 3// 放大ing  
  6.     static final int SMALLER = 4// 缩小ing  
  7.     private int mode = NONE; // 当前的事件  
  8.  
  9.     private float beforeLenght; // 两触点距离  
  10.     private float afterLenght; // 两触点距离  
  11.     private float scale = 0.04f; // 缩放的比例 X Y方向都是这个值 越大缩放的越快  
  12.  
  13.     private int screenW;  
  14.     private int screenH;  
  15.  
  16.     /* 处理拖动 变量 */ 
  17.     private int start_x;  
  18.     private int start_y;  
  19.     private int stop_x;  
  20.     private int stop_y;  
  21.  
  22.     private TranslateAnimation trans; // 处理超出边界的动画  
  23.  
  24.     public TouchView(Context context, int w, int h) {  
  25.         super(context);  
  26.         this.setPadding(0000);  
  27.         screenW = w;  
  28.         screenH = h;  
  29.     }  
  30.  
  31.     float glowX = 30;  
  32.     float glowY = 30;  
  33.     float radius = 20;  
  34.  
  35.     @Override 
  36.     public void draw(Canvas canvas) {  
  37.         super.draw(canvas);  
  38.         if (drawGlow)  
  39.         canvas.drawCircle(glowX, glowY, radius, paint);  
  40.     }  
  41.  
  42.     Paint paint = new Paint();  
  43.     {  
  44.         paint.setAntiAlias(true);  
  45.         paint.setColor(Color.RED);  
  46.         paint.setAlpha(100);  
  47.     };  
  48.     boolean drawGlow = false;  
  49.  
  50.     /**  
  51.      * 就算两点间的距离  
  52.      */ 
  53.     private float spacing(MotionEvent event) {  
  54.         float x = event.getX(0) - event.getX(1);  
  55.         float y = event.getY(0) - event.getY(1);  
  56.         return FloatMath.sqrt(x * x + y * y);  
  57.     }  
  58.  
  59.     /**  
  60.      * 处理触碰..  
  61.      */ 
  62.     @Override 
  63.     public boolean onTouchEvent(MotionEvent event) {  
  64.         switch (event.getAction() & MotionEvent.ACTION_MASK) {  
  65.         case MotionEvent.ACTION_DOWN:  
  66.             drawGlow = true;  
  67.             mode = DRAG;  
  68.             stop_x = (int) event.getRawX();  
  69.             stop_y = (int) event.getRawY();  
  70.             start_x = (int) event.getX();  
  71.             start_y = stop_y - this.getTop();  
  72.             if (event.getPointerCount() == 2)  
  73.                 beforeLenght = spacing(event);  
  74.             break;  
  75.         case MotionEvent.ACTION_POINTER_DOWN:  
  76.             if (spacing(event) > 10f) {  
  77.                 mode = ZOOM;  
  78.                 beforeLenght = spacing(event);  
  79.             }  
  80.             break;  
  81.         case MotionEvent.ACTION_UP:  
  82.             /* 判断是否超出范围 并处理 */ 
  83.             drawGlow = false;  
  84.             int disX = 0;  
  85.             int disY = 0;  
  86.             if (getHeight() <= screenH || this.getTop() < 0) {  
  87.                 if (this.getTop() < 0) {  
  88.                     int dis = getTop();  
  89.                     this.layout(this.getLeft(), 0this.getRight(),  
  90.                             0 + this.getHeight());  
  91.                     disY = dis - getTop();  
  92.                 } else if (this.getBottom() > screenH) {  
  93.                     disY = getHeight() - screenH + getTop();  
  94.                     this.layout(this.getLeft(), screenH - getHeight(),  
  95.                             this.getRight(), screenH);  
  96.                 }  
  97.             }  
  98.             if (getWidth() <= screenW) {  
  99.                 if (this.getLeft() < 0) {  
  100.                     disX = getLeft();  
  101.                     this.layout(0this.getTop(), 0 + getWidth(),  
  102.                             this.getBottom());  
  103.                 } else if (this.getRight() > screenW) {  
  104.                     disX = getWidth() - screenW + getLeft();  
  105.                     this.layout(screenW - getWidth(), this.getTop(), screenW,  
  106.                             this.getBottom());  
  107.                 }  
  108.             }  
  109.             if (disX != 0 || disY != 0) {  
  110.                 trans = new TranslateAnimation(disX, 0, disY, 0);  
  111.                 trans.setDuration(500);  
  112.                 this.startAnimation(trans);  
  113.             }  
  114.             mode = NONE;  
  115.             break;  
  116.         case MotionEvent.ACTION_POINTER_UP:  
  117.             mode = NONE;  
  118.             break;  
  119.         case MotionEvent.ACTION_MOVE:  
  120.             /* 处理拖动 */ 
  121.             if (mode == DRAG) {  
  122.                 if (Math.abs(stop_x - start_x - getLeft()) < 88 
  123.                         && Math.abs(stop_y - start_y - getTop()) < 85) {  
  124.                     this.setPosition(stop_x - start_x, stop_y - start_y, stop_x  
  125.                             + this.getWidth() - start_x, stop_y - start_y  
  126.                             + this.getHeight());  
  127.                     stop_x = (int) event.getRawX();  
  128.                     stop_y = (int) event.getRawY();  
  129.                 }  
  130.             }  
  131.             /* 处理缩放 */ 
  132.             else if (mode == ZOOM) {  
  133.                 if (spacing(event) > 10f) {  
  134.                     afterLenght = spacing(event);  
  135.                     float gapLenght = afterLenght - beforeLenght;  
  136.                     if (gapLenght == 0) {  
  137.                         break;  
  138.                     } else if (Math.abs(gapLenght) > 5f) {  
  139.                         if (gapLenght > 0) {  
  140.                             this.setScale(scale, BIGGER);  
  141.                         } else {  
  142.                             this.setScale(scale, SMALLER);  
  143.                         }  
  144.                         beforeLenght = afterLenght;  
  145.                     }  
  146.                 }  
  147.             }  
  148.             break;  
  149.         }  
  150.         glowX = event.getX();  
  151.         glowY = event.getY();  
  152.         this.invalidate();  
  153.         return true;  
  154.     }  
  155.  
  156.     /**  
  157.      * 实现处理缩放  
  158.      */ 
  159.     private void setScale(float temp, int flag) {  
  160.  
  161.         if (flag == BIGGER) {  
  162.             this.setFrame(this.getLeft() - (int) (temp * this.getWidth()),  
  163.                     this.getTop() - (int) (temp * this.getHeight()),  
  164.                     this.getRight() + (int) (temp * this.getWidth()),  
  165.                     this.getBottom() + (int) (temp * this.getHeight()));  
  166.         } else if (flag == SMALLER) {  
  167.             this.setFrame(this.getLeft() + (int) (temp * this.getWidth()),  
  168.                     this.getTop() + (int) (temp * this.getHeight()),  
  169.                     this.getRight() - (int) (temp * this.getWidth()),  
  170.                     this.getBottom() - (int) (temp * this.getHeight()));  
  171.         }  
  172.     }  
  173.  
  174.     /**  
  175.      * 实现处理拖动  
  176.      */ 
  177.     private void setPosition(int left, int top, int right, int bottom) {  
  178.         this.layout(left, top, right, bottom);  
  179.     }  
  180.  
  181. }  

3.

            
  1. /**  
  2.  * 一个绝对布局   
  3.  * @author Administrator  
  4.  *  
  5.  */ 
  6. @SuppressWarnings("deprecation")  
  7. public class ViewScroll extends AbsoluteLayout  
  8. {  
  9.     private int screenW;    //可用的屏幕宽  
  10.     private int screenH;    //可用的屏幕高   总高度-上面组件的总高度  
  11.     private int imgW;       //图片原始宽  
  12.     private int imgH;       //图片原始高  
  13.     private TouchView tv;  
  14.  
  15.     public ViewScroll(Context context,int resId,View topView)  
  16.     {  
  17.         super(context);  
  18.         screenW = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();  
  19.         screenH = ((Activity)context).getWindowManager().getDefaultDisplay().getHeight()-(topView==null?190:topView.getBottom()+50);  
  20.         tv = new TouchView(context,screenW,screenH);  
  21.         tv.setImageResource(resId);  
  22.         Bitmap img = BitmapFactory.decodeResource(context.getResources(), resId);  
  23.         imgW = img.getWidth();  
  24.         imgH = img.getHeight();  
  25.         int layout_w = imgW>screenW?screenW:imgW; //实际显示的宽  
  26.         int layout_h = imgH>screenH?screenH:imgH; //实际显示的高  
  27.         if(layout_w==screenW||layout_h==screenH)  
  28.             tv.setScaleType(ScaleType.FIT_XY);  
  29.         tv.setLayoutParams(new AbsoluteLayout.LayoutParams(layout_w,layout_h , layout_w==screenW?0:(screenW-layout_w)/2, layout_h==screenH?0:(screenH-layout_h)/2));  
  30.         this.addView(tv);  
  31.     } 

 

4.Gallery上的被画上相框的ImageView

            
  1. package com.app;  
  2.  
  3.  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Color;  
  9. import android.graphics.Matrix;  
  10. import android.graphics.Paint;  
  11. import android.graphics.drawable.BitmapDrawable;  
  12. import android.view.MotionEvent;  
  13. import android.widget.ImageView;  
  14. /**  
  15.  * ImageAdapter中ImageView的实现类  
  16.  * @author Administrator  
  17.  *  
  18.  */ 
  19. public class ImageViewImp extends ImageView  
  20. {  
  21.     private int alpha = 250;  
  22.     private boolean pressed = false;  
  23.     public ImageViewImp(Context context)  
  24.     {  
  25.         super(context);  
  26.     }  
  27.       
  28.     public void show()  
  29.     {  
  30.         new Thread(){  
  31.             public void run() {  
  32.                 int time = 2000;  
  33.                 try 
  34.                 {  
  35.                     pressed = true;  
  36.                     while(time>0)  
  37.                     {  
  38.                         Thread.sleep(200);  
  39.                         time -= 200;  
  40.                         alpha-= 25;  
  41.                           
  42.                         postInvalidate();  
  43.                     }  
  44.                     pressed = false;  
  45.                 }  
  46.                 catch (Exception e)  
  47.                 {  
  48.                     e.printStackTrace();  
  49.                 }  
  50.             };  
  51.         }.start();  
  52.     }  
  53.       
  54.     @Override 
  55.     public boolean onTouchEvent(MotionEvent event)  
  56.     {  
  57.           
  58.         if(event.getAction() == MotionEvent.ACTION_DOWN)  
  59.             show();  
  60.  
  61.         return false;  
  62.     }  
  63.       
  64.       
  65.     @Override 
  66.     protected void onDraw(Canvas canvas)  
  67.     {  
  68.         Paint p = new Paint();  
  69.         p.setColor(Color.WHITE);  
  70.         p.setStyle(Paint.Style.STROKE);  
  71.         p.setStrokeWidth(10);  
  72.         BitmapDrawable bd = (BitmapDrawable) getDrawable();  
  73.  
  74.         if(bd!=null)  
  75.         {  
  76.             canvas.drawBitmap(p_w_picpathScale(bd.getBitmap(), 107113), 21,18, p);    
  77.         }  
  78.         canvas.drawBitmap(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.kua), 00, p);  
  79.         if(isPressed())  
  80.         {  
  81.             canvas.drawRect(5,5,140,140,p);  
  82.         }  
  83.         if(pressed)  
  84.         {  
  85.             p.setAlpha(alpha);  
  86.             canvas.drawRect(5,5,140,140,p);  
  87.         }  
  88.     }  
  89.       
  90.     public static Bitmap p_w_picpathScale(Bitmap bitmap, int dst_w, int dst_h){    
  91.           int  src_w = bitmap.getWidth();  
  92.           int  src_h = bitmap.getHeight();  
  93.           float scale_w = ((float)dst_w)/src_w;  
  94.           float  scale_h = ((float)dst_h)/src_h;  
  95.           Matrix  matrix = new Matrix();  
  96.           matrix.postScale(scale_w, scale_h);  
  97.           Bitmap dstbmp = Bitmap.createBitmap(bitmap, 00, src_w, src_h, matrix, true);        
  98.           return dstbmp;  
  99.     }  
  100.  
  101. }  

 

5.Gallery的适配器

            
  1. package com.app;  
  2.  
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.BaseAdapter;  
  7. import android.widget.Gallery;  
  8. import android.widget.ImageView;  
  9. import android.widget.ImageView.ScaleType;  
  10. /**  
  11.  * Gallery的适配器类  
  12.  * @author Administrator  
  13.  *  
  14.  */ 
  15. public class ImageAdapter extends BaseAdapter  
  16. {  
  17.     /*图片素材*/ 
  18.     public int[] imgIds = {R.drawable.jpg,R.drawable.pic};  
  19.       
  20.     private Context context;  
  21.       
  22.     public ImageAdapter(Context context)  
  23.     {  
  24.         this.context = context;       
  25.     }  
  26.       
  27.     @Override 
  28.     public int getCount()  
  29.     {  
  30.         return imgIds.length;  
  31.     }  
  32.  
  33.     @Override 
  34.     public Object getItem(int position)  
  35.     {  
  36.         return null;  
  37.     }  
  38.  
  39.     @Override 
  40.     public long getItemId(int position)  
  41.     {  
  42.         return 0;  
  43.     }  
  44.  
  45.     @Override 
  46.     public View getView(int position, View convertView, ViewGroup parent)  
  47.     {  
  48.         ImageView img = new ImageViewImp(context);  
  49.         img.setImageResource(imgIds[position]);  
  50.         img.setScaleType(ScaleType.CENTER);  
  51.         img.setLayoutParams(new Gallery.LayoutParams(155,150));  
  52.         return img;  
  53.     }  
  54.  
  55. }  

 

更多相关文章

  1. 刚开始安卓,记录一个刚做的图片缩放程序
  2. android WindowManager可拖动悬浮按钮
  3. Android单张图片查看、单指移动、双指缩放、双击最大化或最小化
  4. Android 实现图片的自动缩放,适应分辨率不同的手机
  5. Android中实现双指缩放的功能
  6. Android两个recyview直接的item拖动
  7. android 图片处理 resize 探秘(图片缩放、压缩问题)
  8. Android 图片的浏览、缩放、拖动和自动居中
  9. Android控件拖动

随机推荐

  1. Eclipse 开发 Android, Hello FormStuff(
  2. android列出目录下的所有图片
  3. Android(安卓)媒体:网络视频播放器的基本
  4. [Android] conversion to dalvik format
  5. Android(安卓)Unable to execute dex: ja
  6. Android(安卓)强制横屏或竖屏设置
  7. Android: Performing Network Operations
  8. android开发模式LiveData+ViewModel+Room
  9. Android(安卓)SQLiter cursor的使用
  10. Ubuntu android studio 调试 android wea