当前位置:  编程技术>移动开发

android 图片操作(缩放移动) 实例代码

    来源: 互联网  发布时间:2014-10-20

    本文导语:  view_show.xml 代码如下:                                                GalleryViewTouch.java: 代码如下:package com.kotei.lbs.Anthurium.LawCases; import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;imp...

view_show.xml

代码如下:



   
       
       
       
       
       
   

GalleryViewTouch.java:

代码如下:

package com.kotei.lbs.Anthurium.LawCases;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
 * 查看图片的操作
 * @author hongj
 */
public class GalleryViewTouch extends Activity  {
    private ImageView iv;
     private Bitmap bitmap=null;
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();
        DisplayMetrics dm;
        float minScaleR;// 最小缩放比例
        static final float MAX_SCALE = 4f;// 最大缩放比例
        static final int NONE = 0;// 初始状态
        static final int DRAG = 1;// 拖动
        static final int ZOOM = 2;// 缩放
        int mode = NONE;
        PointF prev = new PointF();
        PointF mid = new PointF();
        float dist = 1f;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.view_show);
        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);// 获取分辨率
        bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.bottom_layout_background);
        iv=(ImageView)findViewById(R.id.view_img);
        iv.setImageBitmap(bitmap);
        iv.setOnTouchListener(new ImageTouch(bitmap,dm.widthPixels,dm.heightPixels,iv));

    }

    class ImageTouch implements OnTouchListener {
        private ImageView iv;
         private Bitmap bitmap=null;
            Matrix matrix = new Matrix();
            Matrix savedMatrix = new Matrix();
            int dmwidth,dmheight;
            float minScaleR;// 最小缩放比例
            static final float MAX_SCALE = 4f;// 最大缩放比例
            static final int NONE = 0;// 初始状态
            static final int DRAG = 1;// 拖动
            static final int ZOOM = 2;// 缩放
            int mode = NONE;
            PointF prev = new PointF();
            PointF mid = new PointF();
            float dist = 1f;
            ImageTouch(Bitmap bitmap,int width,int height,ImageView iv){
                this.bitmap= bitmap;
                this.dmwidth=width;
                this.dmheight=height;
                this.iv=iv;
                initImage();
            }
            public void initImage(){
                if (dmwidth < bitmap.getWidth()|| dmheight < bitmap.getHeight()) {

                    minZoom();
                    center();
                    iv.setImageMatrix(matrix);
                } else {
                    iv.setScaleType(ScaleType.CENTER);
                }
            }
        @Override

        public boolean onTouch(View v, MotionEvent event) {
             switch (event.getAction() & MotionEvent.ACTION_MASK) {
                // 主点按下
                case MotionEvent.ACTION_DOWN:
                    Log.d("System.out", "ACTION_DOWN");
                    savedMatrix.set(matrix);
                    prev.set(event.getX(), event.getY());
                    mode = DRAG;
                    break; // 副点按下
                case MotionEvent.ACTION_POINTER_DOWN:
                    Log.d("System.out", "ACTION_POINTER_DOWN");
                    dist = spacing(event); // 如果连续两点距离大于10,则判定为多点模式
                    if (spacing(event) > 10f) {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        matrix.set(savedMatrix);
                        matrix.postTranslate(event.getX() - prev.x, event.getY()
                                - prev.y);

                    } else if (mode == ZOOM) {
                        float newDist = spacing(event);
                        if (newDist > 10f) {
                            matrix.set(savedMatrix);
                            float tScale = newDist / dist;
                            matrix.postScale(tScale, tScale, mid.x, mid.y);
                        }
                    }
                    break;
                }

                iv.setImageMatrix(matrix);
                if(mode!=NONE)
               CheckView();

                return true;
        }
         private void CheckView() {
                float p[] = new float[9];
                matrix.getValues(p);
                if (mode == ZOOM) {
                    if (p[0] < minScaleR) {
                        matrix.setScale(minScaleR, minScaleR);
                    }
                    if (p[0] > MAX_SCALE) {
                        matrix.set(savedMatrix);
                    }
                }
                center();
            }

         public void minZoom() {
                Log.i("test", bitmap.getWidth()+"");
                Log.i("test", bitmap.getHeight()+"");
                minScaleR = Math.min(
                        (float) dmwidth / (float) bitmap.getWidth(),
                        (float) dmheight / (float) bitmap.getHeight());
                if (minScaleR < 1.0) {
                    matrix.postScale(minScaleR, minScaleR);
                }
            }
          protected void center(boolean horizontal, boolean vertical) {
                Matrix m = new Matrix();
                m.set(matrix);
                RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
                m.mapRect(rect);
                float height = rect.height();
                float width = rect.width();
                Log.d("System.out",rect.top+"************"+ rect.bottom);
                float deltaX = 0, deltaY = 0;
                if (vertical) { // 图片小于屏幕大小,则居中显示。大于屏幕,上方留空则往上移,下方留空则往下移

                    int screenHeight = dmheight;
                    if (height < screenHeight) {
                        deltaY = (screenHeight - height) / 2 - rect.top;
                    } else if (rect.top > 0) {
                        deltaY = -rect.top;
                    } else if (rect.bottom < screenHeight) {
                        deltaY = iv.getHeight() - rect.bottom;
                    }
                    //deltaY-=50;
                }
                if (horizontal) {
                    int screenWidth = dmwidth;
                    if (width < screenWidth) {
                        deltaX = (screenWidth - width) / 2 - rect.left;
                    } else if (rect.left > 0) {
                        deltaX = -rect.left;
                    } else if (rect.right < screenWidth) {
                        deltaX = screenWidth - rect.right;
                    }
                }
                matrix.postTranslate(deltaX, deltaY);
            }
         public void center() {
                center(true, true);
            }
         private float spacing(MotionEvent event) {
                float x = event.getX(0) - event.getX(1);
                float y = event.getY(0) - event.getY(1);
                return FloatMath.sqrt(x * x + y * y);
            }

            /** * 两点的中点 */
            private void midPoint(PointF point, MotionEvent event) {
                float x = event.getX(0) + event.getX(1);
                float y = event.getY(0) + event.getY(1);
                point.set(x / 2, y / 2);
            }
    }
}


    
 
 

您可能感兴趣的文章:

  • Android 图片缩放与旋转的实现详解
  • Android通过手势实现的缩放处理实例代码
  • Android 图像处理(类型转换,比例缩放,倒影,圆角)的小例子
  • android 多点触摸图片缩放的具体实现方法
  • Android开发之图形图像与动画(二)Animation实现图像的渐变/缩放/位移/旋转
  • android 大图片拖拽并缩放实现原理
  • Android中利用matrix 控制图片的旋转、缩放、移动
  • Android瀑布流实例 android_waterfall
  • Android的OpenGL编程实例 Android-GL
  • android 简单图片动画播放的实例代码
  • android WakeLock使用方法代码实例
  • android自动安装apk代码实例(不使用apk安装器安装)
  • android 弹出提示框的使用(图文实例)
  • 控制Android LED灯颜色的代码实例
  • Android中AnimationDrawable使用的简单实例
  • Android中将View的内容保存为图像的简单实例
  • Android入门之LinearLayout、AbsoluteLayout的用法实例讲解
  • android中Bitmap的放大和缩小实例代码
  • android中写一个内部类来选择文件夹中指定的图片类型实例说明
  • 怎样删除android的gallery中的图片实例说明
  • 在Android中 获取正在运行的Service 实例
  • Android根据电话号码获得联系人头像实例代码
  • Android调用默认浏览器打开指定Url的方法实例
  • android双缓冲技术实例详解
  • ANDROID 完美退出APP的实例代码
  • Android对sdcard扩展卡文件操作实例详解
  • Android 清除SharedPreferences 产生的数据(实例代码)
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • android 自定义Android菜单背景的代码
  • android panellistview 圆角实现代码
  • android网络编程之android连接网络的简单示例代码
  • 用ubuntu下载的android源代码不知下到哪去了
  • Android发送短信功能代码
  • Android Bitmap和Drawable相互转换的简单代码
  • android通过bitmap生成新图片关键性代码
  • android 跳转进市场的实现代码
  • Android键盘显示与隐藏代码
  • Android中监听系统网络连接打开或者关闭的实现代码
  • Android 显示和隐藏输入法实现代码
  • Android点亮屏幕或屏幕解锁和锁定以及其他相关权限实现代码
  • Android中Root权限获取的简单代码
  • android downsample降低音频采样频率代码
  • android连接wifi时获取广播地址代码
  • android layout 按比例布局的代码
  • Android应用开发中模拟按下HOME键的效果(实现代码)
  • Android读取用户号码,手机串号,SIM卡序列号的实现代码
  • Android获取apk程序签名信息代码示例
  • android禁止锁屏保持常亮(示例代码)
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android系统自带样式 (android:theme)
  • Android开发需要的几点注意事项总结
  • Android网络共享软件 Android Wifi Tether
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android 图标库 Android GraphView
  • Android访问与手机通讯相关类的介绍
  • 轻量级Android开发工具 Android Tools
  • Android及andriod无线网络Wifi开发的几点注意事项
  • Android 开发环境 Android Studio
  • Android 2.3 下StrictMode介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3