欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-01-02 08:55:25
view_show.xml 复制代码 代码如下:

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;

/**
 * 查看图片的操作
 * @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);
            }
    }
}