文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-06-06 10:33

关注

view_show.xml

代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <FrameLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <ImageView
            android:id="@+id/view_img"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:background="@drawable/camera_gray"/>
        <ImageButton
            android:id="@+id/view_close"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="top|left"
            android:layout_margin="5dip"/>
        <ImageButton
            android:id="@+id/view_del"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="top|right"
            android:layout_margin="5dip"/>
        <ImageButton
            android:id="@+id/view_narrow"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_marginRight="10dip"/>
        <ImageButton
            android:id="@+id/view_amplification"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_marginLeft="10dip"/>
    </FrameLayout>
</LinearLayout>

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;


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;
   
    @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;
        &nb sp;       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;
     &n bsp;          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(minSc aleR, 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实现ImageView图片缩放和拖动Android实现通过手势控制图片大小缩放的方法Android实现手势滑动多点触摸缩放平移图片效果(二)Android实现手势滑动多点触摸缩放平移图片效果Android手势滑动实现ImageView缩放图片大小Android开发之imageView图片按比例缩放的实现方法基于Android 实现图片平移、缩放、旋转同时进行Android编程实现图片的浏览、缩放、拖动和自动居中效果Android 图片缩放与旋转的实现详解Android控件实现图片缩放功能


免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯