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

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);
            }
    }
}

时间: 2024-09-08 05:52:22

android 图片操作(缩放移动) 实例代码的相关文章

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

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_par

Android 图片的颜色处理实例代码_Android

仿造美图秀秀移动鼠标调整seekbar,调整图片的颜色 项目布局如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma

Android实现图片轮播切换实例代码_Android

利用Android的ViewFlipper和AnimationUtils实现图片带有动画的轮播切换,其中当点击"上一张"图片时,切换到上一张图片:当点击"下一张"图片时,切换到下一张图片.其效果图如下: 设置布局文件,其内容如下: activity_image_flipper_shade.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xm

Android 底部导航控件实例代码_Android

一.先给大家展示下最终效果 通过以上可以看到,图一是简单的使用,图二.图三中为结合ViewPager共同使用,而且都可以随ViewPager的滑动渐变色,不同点是图二为选中非选中两张图片,图三的选中非选中是一张图片只是做了颜色变化. 二. 需求 我们希望做可以做成这样的,可以在xml布局中引入控件并绑定数据,在代码中设置监听回调,并且配置使用要非常简单! 三.需求分析 根据我们多年做不明确需求项目的经验,以上需求还算明确.那么我们可以采用在LinearLayout添加子View控件,这个子Vie

Android自定义控件下拉刷新实例代码_Android

实现效果: 图片素材: --> 首先, 写先下拉刷新时的刷新布局 pull_to_refresh.xml: <resources> <string name="app_name">PullToRefreshTest</string> <string name="pull_to_refresh">下拉可以刷新</string> <string name="release_to_refre

Android 底部导航控件实例代码

一.先给大家展示下最终效果 通过以上可以看到,图一是简单的使用,图二.图三中为结合ViewPager共同使用,而且都可以随ViewPager的滑动渐变色,不同点是图二为选中非选中两张图片,图三的选中非选中是一张图片只是做了颜色变化. 二. 需求 我们希望做可以做成这样的,可以在xml布局中引入控件并绑定数据,在代码中设置监听回调,并且配置使用要非常简单! 三.需求分析 根据我们多年做不明确需求项目的经验,以上需求还算明确.那么我们可以采用在LinearLayout添加子View控件,这个子Vie

android图片类型之间相互转换实现代码_Android

本文实例讲述了android图片类型之间相互转换实现代码.分享给大家供大家参考.具体如下: android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下: 1.Drawable → Bitmap Java代码如下: public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawab

PHP之图片上传类实例代码(加了缩略图)

有缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改下 <form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file&q

android图片类型之间相互转换实现代码

本文实例讲述了android图片类型之间相互转换实现代码.分享给大家供大家参考.具体如下: android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下: 1.Drawable → Bitmap Java代码如下: public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawab