手势放大旋转效果

效果图

xml

<ImageView
        android:id="@+id/know_star"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix"
        android:src="@drawable/earth_know"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

activity

    private Matrix mMatrix = new Matrix();
    private float mScaleFactor = .4f;
    private float mRotationDegrees = 0.f;
    private float mFocusX = 0.f;
    private float mFocusY = 0.f;
    private int mImageHeight, mImageWidth;
    private ParticleSystem ps;
    private ScaleGestureDetector mScaleDetector;  //定义图片的放大缩小
    private RotateGestureDetector mRotateDetector;  //设置图片旋转
    private ImageView knowHelp;

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knowledge);
        initView();
        
        // 获取屏幕的中心点
        Display display = getWindowManager().getDefaultDisplay();
        mFocusX = display.getWidth() / 2f;    //屏幕的宽度一半
        mFocusY = display.getHeight() / 3f;   //屏幕的高度一半

        // 确定维度的地球图像
        Drawable d = this.getResources().getDrawable(stars[star - 1]);
        mImageHeight = d.getIntrinsicHeight();
        mImageWidth = d.getIntrinsicWidth();

        // 视图缩放和转动的矩阵
        float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
        float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;

        mMatrix.postScale(mScaleFactor, mScaleFactor);
        /**
         * 用Matrix类中的平移方法设置地球的位置
         * */
        mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);
        knowStar.setImageMatrix(mMatrix);     //将地球设置到屏幕中心

        // 设置手势检测器
        mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());  //图片缩放
        mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());  //图片的旋转
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        knowBack = (ImageView) findViewById(R.id.know_back);
        knowStar = (ImageView) findViewById(R.id.know_star);
        knowKnow = (ImageView) findViewById(R.id.know_know);
        // 将图片设置触碰监听
        knowStar.setOnTouchListener(this);
        knowHelp = (ImageView) findViewById(R.id.know_help);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
    }

    /**
     * 设置手势
     */
    @SuppressLint("ClickableViewAccessibility")
    public boolean onTouch(View v, MotionEvent event) {
        knowHelp.setVisibility(View.GONE);

        mScaleDetector.onTouchEvent(event);  //设置图片缩放的监听
        mRotateDetector.onTouchEvent(event);  //设置图片的旋转监听

        float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
        float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;
        mMatrix.reset();
        mMatrix.postScale(mScaleFactor, mScaleFactor);
        mMatrix.postRotate(mRotationDegrees, scaledImageCenterX, scaledImageCenterY);
        mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);

        ImageView view = (ImageView) v;
        view.setImageMatrix(mMatrix);
        return true; // 指示事件处理
    }

    /**
     * 图片的放大缩小的监听
     */
    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();
            //不要让物体太小或太大。
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
            return true;
        }
    }

    /**
     * 图片转动的监听
     */
    private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
        @Override
        public boolean onRotate(RotateGestureDetector detector) {
            mRotationDegrees -= detector.getRotationDegreesDelta();
            return true;
        }
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值