Android实现图片放大缩小

package com.min.Test_Gallery;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DisplayImage extends Activity implements OnTouchListener, OnGestureListener  {
    private static final String TAG = "DisplayImage";
    private static final int FLING_MIN_DISTANCE = 100;
    private static final int FLING_MIN_VELOCITY = 200;
    
    
    /* 相关变量声明 */
    private ImageView mImageView;
    private Button mButton01;
    private Button mButton02;
    private FrameLayout layout1;
    private LinearLayout layoutImage;
    private Bitmap bmp;
    private int id=0;
    private int displayWidth;
    private int displayHeight;
    private float scaleWidth=1;
    private float scaleHeight=1;
    private GestureDetector mGestureDetector;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
        /* 加载display.xml Layout */
        setContentView(R.layout.display);
        
        /* 取得屏幕分辨率大小 */
        DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        displayWidth=dm.widthPixels;
        displayHeight=dm.heightPixels;
        
        /* 初始化相关变量 */
        Bundle bundle = this.getIntent().getExtras();
        Integer imageId = bundle.getInt("imageId");
        Log.i(TAG, "onCreate, imageId = " + imageId);
                       
        bmp=BitmapFactory.decodeResource(getResources(), imageId);
        mImageView = (ImageView)findViewById(R.id.myImageView);
        mImageView.setImageBitmap(bmp);
        mImageView.setOnTouchListener(this);
        mImageView.setLongClickable(true);
        
        layout1 = (FrameLayout)findViewById(R.id.layout1);
        layoutImage = (LinearLayout)findViewById(R.id.layoutImage);
        mButton01 = (Button)findViewById(R.id.myButton1);
        mButton02 = (Button)findViewById(R.id.myButton2);
        
        /* 缩小按钮onClickListener */
        mButton01.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                small();
            }
        });
        
        /* 放大按钮onClickListener */
        mButton02.setOnClickListener(new Button.OnClickListener() {
            @Override       
            public void onClick(View v) {
                big();
            }
        });
    }  
    
    // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
//        Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();    
        Log.i(TAG, "onDown...");
        
        return false;
    }

    /* 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,
     * 多个ACTION_MOVE, 1个ACTION_UP触发
     * 参数解释:
     * e1:第1个ACTION_DOWN MotionEvent
     * e2:最后一个ACTION_MOVE MotionEvent
     * velocityX:X轴上的移动速度,像素/秒
     * velocityY:Y轴上的移动速度,像素/秒
     * 触发条件 :
     * X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
     * @see android.view.GestureDetector$OnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onFling...");
        
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE    
                 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
            // Fling left

             Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();    
         } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE    
                 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    

             // Fling right

             Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();    
         }  
        
        return false;
    }

    // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onLongPress...");
        
    }

    // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onScroll...");
        
        return false;
    }

    // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
    // 注意和onDown()的区别,强调的是没有松开或者拖动的状态
    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onShowPress...");
        
    }

    // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onSingleTapUp...");
        
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onTouch...");
        
        // Set button visible
        mButton01.setVisibility(View.VISIBLE);
        mButton02.setVisibility(View.VISIBLE);
        
        return  mGestureDetector.onTouchEvent(event);    
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        // TODO Auto-generated method stub
//        super.onTouchEvent(event);
//        
//        Log.i(TAG, "onTouchEvent");
//        // Set button visible
//        mButton01.setVisibility(View.VISIBLE);
//        mButton02.setVisibility(View.VISIBLE);
//        
//        return true;
//    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        super.onKeyDown(keyCode, event);
        
        Log.i(TAG, "onKeyDown...");
        // Set button visible
        mButton01.setVisibility(View.VISIBLE);
        mButton02.setVisibility(View.VISIBLE);
        
        return true;
    }

    /* 图片缩小的method */
    private void small()    {
        int bmpWidth=bmp.getWidth();
        int bmpHeight=bmp.getHeight();
        
        Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
        
        /* 设置图片缩小的比例 */
        double scale=0.8;
        /* 计算出这次要缩小的比例 */
        scaleWidth=(float) (scaleWidth*scale);
        scaleHeight=(float) (scaleHeight*scale);
        /* 产生reSize后的Bitmap对象 */
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,
                bmpHeight,matrix,true);
        
        if(id==0)      {
            /* 如果是第一次按,就删除原来默认的ImageView */
            layoutImage.removeView(mImageView);
        } else {
            /* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
            layoutImage.removeView((ImageView)findViewById(id));
        }
        
        /* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
        id++;
        ImageView imageView = new ImageView(this);
        imageView.setId(id);
        imageView.setImageBitmap(resizeBmp);
        layoutImage.addView(imageView);
        Log.i(TAG, "imageView.getWidth() = " + imageView.getWidth()
                + ", imageView.getHeight() = " + imageView.getHeight());
        setContentView(layout1);
        /* 因为图片放到最大时放大按钮会disable,所以在缩小时把它重设为enable */
        mButton02.setEnabled(true);
        mButton02.setTextColor(Color.MAGENTA);
    }
    
    /* 图片放大的method */
    private void big() {
        int bmpWidth=bmp.getWidth();
        int bmpHeight=bmp.getHeight();
        
        Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
        
        /* 设置图片放大的比例 */
        double scale=1.25;
        /* 计算这次要放大的比例 */
        scaleWidth=(float)(scaleWidth*scale);
        scaleHeight=(float)(scaleHeight*scale);
        /* 产生reSize后的Bitmap对象 */
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,
                bmpHeight,matrix,true);
        
        if(id==0) {
            /* 如果是第一次按,就删除原来设置的ImageView */
            layoutImage.removeView(mImageView);
        } else {
            /* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
            layoutImage.removeView((ImageView)findViewById(id));
        }
        
        /* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
        id++;
        ImageView imageView = new ImageView(this);
        imageView.setId(id);
        imageView.setImageBitmap(resizeBmp);
        layoutImage.addView(imageView);
        setContentView(layout1);
        /* 如果再放大会超过屏幕大小,就把Button disable */
        if( scaleWidth * scale * bmpWidth > bmpWidth * 3 ||
            scaleHeight * scale * bmpHeight > bmpWidth * 3 ||
            scaleWidth * scale * bmpWidth > displayWidth * 5 ||
            scaleHeight * scale * bmpHeight > displayHeight * 5) {
                mButton02.setEnabled(false);
                mButton02.setTextColor(Color.GRAY);
            } else {
                mButton02.setEnabled(true);
                mButton02.setTextColor(Color.MAGENTA);
            }
        }
    

}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout1"
    >

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"   
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="19"
        android:scrollbars="vertical"
        android:fadingEdge="vertical">
    <HorizontalScrollView
        android:layout_height="fill_parent"
        android:layout_width="wrap_content">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/layoutImage"
            >
            <ImageView
                android:id="@+id/myImageView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="19"
                android:paddingTop="5dip"
                android:paddingBottom="5dip"
                />
        </LinearLayout>
    </HorizontalScrollView >
    </ScrollView>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/myButton1"
            android:layout_width="45dip"
            android:layout_height="30dip"
            android:layout_alignParentLeft="true"
            android:gravity="left"
            style="@style/my_style_button"
            android:visibility="gone"
            android:text="缩小"
            />
        <Button
            android:id="@+id/myButton2"
            android:layout_width="45dip"
            android:layout_height="30dip"
            android:layout_alignParentRight="true"
            android:gravity="right"
            style="@style/my_style_button"
            android:visibility="gone"
            android:text="放大"
            />
    </RelativeLayout>
</FrameLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是实现图片放大缩小功能ZoomControls控件的用法实例: 1. 在 XML 布局文件中添加 ZoomControls 控件: ```xml <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="matrix" android:src="@drawable/image" /> <ZoomControls android:id="@+id/zoomControls" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" /> </FrameLayout> ``` 2. 在 Activity 中获取 ImageView 和 ZoomControls 对象,并设置监听器: ```java public class MainActivity extends AppCompatActivity { private ImageView imageView; private ZoomControls zoomControls; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); zoomControls = findViewById(R.id.zoomControls); zoomControls.setOnZoomInClickListener(new View.OnClickListener() { @Override public void onClick(View v) { float scale = imageView.getScaleX() + 0.5f; imageView.setScaleX(scale); imageView.setScaleY(scale); } }); zoomControls.setOnZoomOutClickListener(new View.OnClickListener() { @Override public void onClick(View v) { float scale = imageView.getScaleX() - 0.5f; if (scale < 1) { scale = 1; } imageView.setScaleX(scale); imageView.setScaleY(scale); } }); } } ``` 3. 在监听器中实现图片的缩放功能,通过 ImageView 的 setScaleX() 和 setScaleY() 方法实现缩放。 以上就是实现图片放大缩小功能ZoomControls控件的用法实例,希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值