<GestureDetector>通过手势缩放图片

package com.test.gesturetest;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * 通过手势缩放图片
 */
public class GestureZoomActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {

    //定义检测手势的实例
    GestureDetector mDetector;

    ImageView image;

    //初始化图片
    Bitmap bitmap;
    //定义图片的宽高
    int width, height;

    //记录当前的缩放比
    float currentScale = 1;

    Matrix mMatrix; //控制图片缩放的对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_zoom);

        //创建手势检测对象
        mDetector = new GestureDetector(this, this);

        image = (ImageView) findViewById(R.id.show);

        mMatrix = new Matrix();

        //获取被缩放的原图片
        bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.flower);

        //获得位图的宽高
        width = bitmap.getWidth();
        height = bitmap.getHeight();

        //设置ImageView 初始化时显示的图片
        image.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.flower));
    }

    //重写该方法是要把 Activity 的触摸事件交给 GestureDetector 来处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        velocityX = velocityX > 4000 ? 4000 : velocityX;
        velocityX = velocityX < -4000 ? -4000 : velocityX;

        //根据手势的速度来计算缩放比.如果 velocityX > 0 放大图片,否则缩小图片
        currentScale += currentScale * velocityX / 4000.0f;

        //保证 currentScale 不会等于0
        currentScale = currentScale > 0.01 ? currentScale : 0.01f;

        //重置 mMatrix
        mMatrix.reset();

        //缩放 mMatrix
        mMatrix.setScale(currentScale, currentScale, 160, 200);

        BitmapDrawable temp = (BitmapDrawable) image.getDrawable();

        //如果还未回收,就强制回收
        if (!temp.getBitmap().isRecycled()&&null!=temp) {
            temp.getBitmap().recycle();
        }

        //根据源位图创建新的 位图
        Bitmap copyBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, mMatrix, true);

        image.setImageBitmap(copyBitmap);

        return true;
    }
}

布局界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.gesturetest.GestureZoomActivity">
    <ImageView
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        />
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值