Android进阶篇-自定义图片伸缩控件具体实例

   2015-07-28 0
核心提示:这篇文章介绍了Android自定义图片伸缩控件具体实例,有需要的朋友可以参考一下

ZoomImageView.java:

复制代码 代码如下:

/**
 * @author gongchaobin
 *
 *  自定义可伸缩的ImageView
 */
public class ZoomImageView extends View{
    /** 画笔类  **/
    private Paint mPaint;

    private Runnable mRefresh = null;
    /** 缩放手势监听类  **/
    private ScaleGestureDetector mScaleDetector;
    /** 手势识别类  **/
    private GestureDetector mGestureDetector;
    /** 当前被渲染的Bitmap **/
    private Bitmap mBitmap;

    private int mThisWidth = -1, mThisHeight = -1;

    private Runnable mOnLayoutRunnable = null;

    private Matrix mBaseMatrix = new Matrix();
    private Matrix mDisplayMatrix = new Matrix();
    private Matrix mSuppMatrix = new Matrix();
    private Matrix mMatrix = new Matrix();

    /** 最大的拉伸比例   **/
    private float mMaxZoom;

    private float[] mMatrixValues = new float[9];
    private Runnable mFling = null;

    private double mLastDraw = 0;
    static final int sPaintDelay = 250;

    public ZoomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        init();
    }

    public ZoomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    public ZoomImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    private void init() {
        mPaint = new Paint();
        // 图像抖动处理
        mPaint.setDither(true);
        // 过滤优化操作  加快显示
        mPaint.setFilterBitmap(true);
        // 去掉锯齿
        mPaint.setAntiAlias(true);

        /** 刷新线程  **/
        mRefresh = new Runnable() {
            @Override
            public void run() {
                postInvalidate();
            }
        };

        mScaleDetector = new ScaleGestureDetector(getContext(),new ScaleListener());
        mGestureDetector = new GestureDetector(getContext(),new MyGestureListener());

        // 判断是否是新的API  开启硬件加速
        if(Build.VERSION.SDK_INT >=  Build.VERSION_CODES.HONEYCOMB) {
            setLayerType(View.LAYER_TYPE_HARDWARE, null);
        }
    }

    public Bitmap getImageBitmap() {
        return mBitmap;
    }

    /** 回收Bitmap **/
    public void clear() {
        if(mBitmap != null && !mBitmap.isRecycled()) {
            mBitmap.recycle();
            mBitmap = null;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        // TODO Auto-generated method stub
        super.onLayout(changed, left, top, right, bottom);

        mThisWidth = right - left;
        mThisHeight = bottom - top;

        Runnable r = mOnLayoutRunnable;
        if (r != null) {
            mOnLayoutRunnable = null;
            r.run();
        }

        if (mBitmap != null) {
            setBaseMatrix(mBitmap, mBaseMatrix);
            setImageMatrix(getImageViewMatrix());
        }
    }

    private void setBaseMatrix(Bitmap bitmap, Matrix matrix) {
        float viewWidth = getWidth();
        float viewHeight = getHeight();

        matrix.reset();
        float widthScale = Math.min(viewWidth / (float)bitmap.getWidth(), 1.0f);
        float heightScale = Math.min(viewHeight / (float)bitmap.getHeight(), 1.0f);
        float scale;
        if (widthScale > heightScale) {
            scale = heightScale;
        } else {
            scale = widthScale;
        }

        /** 算取比例  进行平移   **/
        matrix.setScale(scale, scale);
        matrix.postTranslate(
                (viewWidth  - ((float)bitmap.getWidth()  * scale))/2F,
                (viewHeight - ((float)bitmap.getHeight() * scale))/2F);
    }

    protected Matrix getImageViewMatrix() {
        mDisplayMatrix.set(mBaseMatrix);
        mDisplayMatrix.postConcat(mSuppMatrix);
        return mDisplayMatrix;
    }

    public void setImageMatrix(Matrix m){
        /** Matrix是否为空并是否定义   **/
        if (m != null && m.isIdentity()) {
            m = null;
        }

        if (m == null && !this.mMatrix.isIdentity() || m != null && !this.mMatrix.equals(m)) {
            this.mMatrix.set(m);
            invalidate();
        }
    }

    static private void translatePoint(Matrix matrix, float [] xy) {
        matrix.mapPoints(xy);
    }

    /**
     * 设置Bitmap
     *
     * @param bitmap
     */
    public void setImageBitmap(final Bitmap bitmap) {
        final int viewWidth = getWidth();

        // 开启硬件加速
        if( Build.VERSION.SDK_INT >=  Build.VERSION_CODES.HONEYCOMB && bitmap!=null && bitmap.getHeight()>1800 )
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        if (viewWidth <= 0)  {
            mOnLayoutRunnable = new Runnable() {
                public void run() {
                    setImageBitmap(bitmap);
                }
            };
            return;
        }

        if (bitmap != null) {
            setBaseMatrix(bitmap, mBaseMatrix);
            this.mBitmap = bitmap;
        } else {
            mBaseMatrix.reset();
            this.mBitmap = bitmap;
        }

        mSuppMatrix.reset();
        setImageMatrix(getImageViewMatrix());
        mMaxZoom = maxZoom();
        zoomTo(zoomDefault());
    }

    public void zoomTo(float scale) {
        float width = getWidth();
        float height = getHeight();

        zoomTo(scale, width/2F, height/2F);
    }

    protected void zoomTo(float scale, float centerX, float centerY) {
        if (scale > mMaxZoom) {
            scale = mMaxZoom;
        }

        float oldScale = getScale();
        float deltaScale = scale / oldScale;

        /** 根据某个中心点按照比例缩放  **/
        mSuppMatrix.postScale(deltaScale, deltaScale, centerX, centerY);
        setImageMatrix(getImageViewMatrix());
        center(true, true, false);
    }

    /**
     * 计算中心位置
     *
     * @param vertical
     * @param horizontal
     * @param animate
     */
    protected void center(boolean vertical, boolean horizontal, boolean animate) {
        if (mBitmap == null)
            return;

        Matrix m = getImageViewMatrix();

        float [] topLeft  = new float[] { 0, 0 };
        float [] botRight = new float[] { mBitmap.getWidth(), mBitmap.getHeight() };

        translatePoint(m, topLeft);
        translatePoint(m, botRight);

        float height = botRight[1] - topLeft[1];
        float width  = botRight[0] - topLeft[0];

        float deltaX = 0, deltaY = 0;

        if (vertical) {
            int viewHeight = getHeight();
            if (height < viewHeight) {
                deltaY = (viewHeight - height)/2 - topLeft[1];
            } else if (topLeft[1] > 0) {
                deltaY = -topLeft[1];
            } else if (botRight[1] < viewHeight) {
                deltaY = getHeight() - botRight[1];
            }
        }

        if (horizontal) {
            int viewWidth = getWidth();
            if (width < viewWidth) {
                deltaX = (viewWidth - width)/2 - topLeft[0];
            } else if (topLeft[0] > 0) {
                deltaX = -topLeft[0];
            } else if (botRight[0] < viewWidth) {
                deltaX = viewWidth - botRight[0];
            }
        }

        postTranslate(deltaX, deltaY);
        if (animate) {
            Animation a = new TranslateAnimation(-deltaX, 0, -deltaY, 0);
            a.setStartTime(SystemClock.elapsedRealtime());
            a.setDuration(250);
            setAnimation(a);
        }
        setImageMatrix(getImageViewMatrix());
    }

    protected void postTranslate(float dx, float dy) {
        mSuppMatrix.postTranslate(dx, dy);
    }

    public float getScale() {
        return getScale(mSuppMatrix);
    }

    protected float getScale(Matrix matrix) {
        if(mBitmap!=null)
            return getValue(matrix, Matrix.MSCALE_X);
        else
            return 1f;
    }

    protected float getValue(Matrix matrix, int whichValue) {
        matrix.getValues(mMatrixValues);
        return mMatrixValues[whichValue];
    }

    /**
     * 计算最大的拉伸比例
     *
     * @return
     */
    protected float maxZoom() {
        if (mBitmap == null)
            return 1F;

        float fw = (float) mBitmap.getWidth()  / (float)mThisWidth;
        float fh = (float) mBitmap.getHeight() / (float)mThisHeight;
        float max = Math.max(fw, fh) * 16;
        return max;
    }

    /**
     * 原始显示比例
     *
     * @return
     */
    public float zoomDefault() {
        if (mBitmap == null)
            return 1F;

        float fw = (float)mThisWidth/(float)mBitmap.getWidth();
        float fh = (float)mThisHeight/(float)mBitmap.getHeight();
        return Math.max(Math.min(fw, fh),1);
    }

    protected void zoomTo(final float scale, final float centerX, final float centerY, final float durationMs) {
        final float incrementPerMs = (scale - getScale()) / durationMs;
        final float oldScale = getScale();
        final long startTime = System.currentTimeMillis();

        post(new Runnable() {
            public void run() {
                long now = System.currentTimeMillis();
                float currentMs = Math.min(durationMs, (float)(now - startTime));
                float target = oldScale + (incrementPerMs * currentMs);
                zoomTo(target, centerX, centerY);

                if (currentMs < durationMs) {
                    post(this);
                }
            }
        });
    }

    protected void scrollBy( float distanceX, float distanceY, final float durationMs ){
        final float dx = distanceX;
        final float dy = distanceY;
        final long startTime = System.currentTimeMillis();

        mFling = new Runnable() {
            float old_x    = 0;
            float old_y    = 0;

            public void run()
            {
                long now = System.currentTimeMillis();
                float currentMs = Math.min( durationMs, now - startTime );
                float x = easeOut( currentMs, 0, dx, durationMs );
                float y = easeOut( currentMs, 0, dy, durationMs );
                postTranslate( ( x - old_x ), ( y - old_y ) );
                center(true, true, false);

                old_x = x;
                old_y = y;
                if ( currentMs < durationMs ) {
                    post( this );
                }
            }
        };
        post( mFling );
    }

    private float easeOut( float time, float start, float end, float duration){
        return end * ( ( time = time / duration - 1 ) * time * time + 1 ) + start;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        if(mBitmap!=null && !mBitmap.isRecycled() ){
            if( Build.VERSION.SDK_INT >=  Build.VERSION_CODES.HONEYCOMB && getLayerType() == View.LAYER_TYPE_HARDWARE ){
                canvas.drawBitmap(mBitmap, mMatrix, null);
            }else{
                if( (System.currentTimeMillis()-mLastDraw) > sPaintDelay ){
                    canvas.drawBitmap(mBitmap, mMatrix, mPaint);
                    mLastDraw = System.currentTimeMillis();
                }
                else{
                    canvas.drawBitmap(mBitmap, mMatrix, null);
                    removeCallbacks(mRefresh);
                    postDelayed(mRefresh, sPaintDelay);
                }
            }
        }
    }

    /**
     * @author Administrator
     *
     * 手势缩放监听
     */
    class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            Log.i("ZoomImageView", "onScale");
            if(detector!=null && detector.isInProgress()){
                try{
                    float targetScale = getScale() * detector.getScaleFactor();
                    targetScale = Math.min(maxZoom(), Math.max(targetScale, 1.0f) );

                    zoomTo(targetScale, detector.getFocusX(), detector.getFocusY() );
                    invalidate();
                    return true;
                }catch(IllegalArgumentException e){
                    e.printStackTrace();
                }
            }
            return false;
        }
    };

    /**
     * @author Administrator
     *
     * 手势识别监听
     */
    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            Log.i("ZoomImageView", "onScroll");
            if((e1 != null && e1.getPointerCount() > 1) || (e2 != null && e2.getPointerCount() > 1)
                    || (mScaleDetector != null && mScaleDetector.isInProgress())){
                return false;
            }

            if(getScale() > zoomDefault() ) {
                removeCallbacks(mFling);
                postTranslate(-distanceX, -distanceY);
                center(true, true, false);
            }

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            Log.i("ZoomImageView", "onFling");
            // TODO Auto-generated method stub
            if ((e1!=null && e1.getPointerCount() > 1) || ( e2!=null && e2.getPointerCount() > 1))
                return false;
            if (mScaleDetector.isInProgress())
                return false;

            try{
                float diffX = e2.getX() - e1.getX();
                float diffY = e2.getY() - e1.getY();

                if ( Math.abs( velocityX ) > 800 || Math.abs( velocityY ) > 800 ) {
                    scrollBy( diffX / 2, diffY / 2, 300 );
                    invalidate();
                }
            }catch(NullPointerException  e){
                e.printStackTrace();
            }

            return super.onFling( e1, e2, velocityX, velocityY );
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // TODO Auto-generated method stub
            Log.i("ZoomImageView", "onDoubleTap");
            if ( getScale() > zoomDefault() ){
                zoomTo(zoomDefault());
            }
            else
                zoomTo(zoomDefault()*3, e.getX(), e.getY(),200);
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            // TODO Auto-generated method stub
            Log.i("ZoomImageView", "onSingleTapConfirmed");
            // 设置点击事件
            if(mImageTouchedListener != null) {
                mImageTouchedListener.onImageTouched();
                return false;
            }
            return super.onSingleTapConfirmed(e);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if(mBitmap != null) {
            mScaleDetector.onTouchEvent(event);

            if(!mScaleDetector.isInProgress()) {
                mGestureDetector.onTouchEvent(event);
            }
        }

        return true;
    };

    /**
     *
     * @author Administrator
     *
     * 点击接口
     */
    private onImageTouchedListener mImageTouchedListener;

    public interface onImageTouchedListener {
        void onImageTouched();
    }

    public void setOnImageTouchedListener(onImageTouchedListener listener ){
        this.mImageTouchedListener = listener;
    }

xml布局:

复制代码 代码如下:

<com.example.pay.ZoomImageView
        android:scaleType="matrix"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/zommImageView"
        />

activity调用方法:

复制代码 代码如下:

mZoomImageView = (ZoomImageView) findViewById(R.id.zommImageView);
        mZoomImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        mZoomImageView.setOnImageTouchedListener(new onImageTouchedListener() {

            @Override
            public void onImageTouched() {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "11111", Toast.LENGTH_LONG).show();
            }
        });

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 说一说Android Studio和IDEA中一个很有用的内存调试插件
    说一说Android Studio和IDEA中一个很有用的内存
    JetBrains JVM Debugger Memory View plugin 在我最近的研发活动期间寻找新的工具,以提高我的开发经验,使Android Studio的生活更轻松,我发现一个有用的插件,我从来没有听说过。 这就是为什么,我决定写这个强大的工具,它如何帮助我与内存调试我的应用程
  • 安卓中通知功能的具体实现
    安卓中通知功能的具体实现
    通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。使用通知的步骤1、需要一个NotificationManager来获得NotificationManager manager = (NotificationManager
    02-05 安卓开发
  • Android view系统分析-setContentView
    Android view系统分析-setContentView
    第一天上班,列了一下今年要学习的东西。主要就是深入学习Android相关的系统源代码,夯实基础。对于学习Android系统源代码,也没什么大概,就从我们平常使用最基础的东西学起,也就是从view这个切入点开始学习Android的源码,在没分析源码之前,我们有的时候
    02-05 安卓开发
  • 如何进行网络视频截图/获取视频的缩略图
    如何进行网络视频截图/获取视频的缩略图
    小编导读:获取视频的缩略图,截图正在播放的视频某一帧,是在音视频开发中,常遇到的问题。本文是主要用于点播中截图视频,同时还可以获取点播视频的缩略图进行显示,留下一个问题,如下图所示, 如果要获取直播中节目视频缩略图,该怎么做呢?(ps:直播是直
  • Android NDK 层发起 HTTP 请求的问题及解决
    Android NDK 层发起 HTTP 请求的问题及解决
    前言新的一年,大家新年快乐~~鸡年大吉!本次给大家带来何老师的最新文章~虽然何老师还在过节,但依然放心不下广大开发者,在此佳节还未结束之际,给大家带来最新的技术分享~ 事件的起因不说了,总之是需要实现一个 NDK 层的网络请求。为了多端适用,还是选择
  • SDK热更之如何在SDK代码中自动插桩及如何生成补
    写在前面本文是SDKHotfix相关的SDK热更系列文章中的一篇,以下为项目及系列文章相关链接:SDKHotfix整体介绍:http://blog.bihe0832.com/sdk_hotfix_project.htmlSDKHotfix对应github地址:https://github.com/bihe0832/SDKHoxFix这篇文章主要介绍一下SDK热更
  • 安装量破千万的第一个产品,我总结了3句话
    安装量破千万的第一个产品,我总结了3句话
    在今天的文章中,作者回顾了自己的第一个产品,他说“我做的第一款产品,是我的一块里程碑。”一起来看看~背景老牌大型互联网公司,部门内部创业的一个项目。我作为产品经理,也是第一次做产品经理,主导产品项目。实际上,项目初期包括我和安卓开发2个人。开
  • 移动周刊第 176 期:Android 知识梳理
    移动周刊第 176 期:Android 知识梳理
    写在前面 本期移动周刊第 176 期如约而至,聚焦 Android、iOS、VR/AR/MR、直播等前沿移动开发技术,收录一周最热点,解读开发技巧,每周三移动周刊抢先看,我们希望从中能够让你有一些收获,如果你有好的文章以及优化建议,请发送邮件至mobilehub@csdn.net,
  • Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突
    Android插件化(六): OpenAtlasの改写aapt以防
    引言Android应用程序的编译中,负责资源打包的是aapt,如果不对打包后的资源ID进行控制,就会导致插件中的资源ID冲突。所以,我们需要改写aapt的源码,以达到通过某种方式传递资源ID的Package ID,通过aapt打包时获取到这个Package ID并且应用才插件资源的命名
    02-05 安卓开发
  • Android架构(一)MVP架构在Android中的实践
    Android架构(一)MVP架构在Android中的实践
    为什么要重视程序的架构设计 对程序进行架构设计的原因,归根结底是为了 提高生产力 。通过设计是程序模块化,做到模块内部的 高聚合 和模块之间的 低耦合 (如依赖注入就是低耦合的集中体现)。 这样做的好处是使得程序开发过程中,开发人员主需要专注于一点,
    02-05 安卓开发
点击排行