• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

手势放大旋转效果

互联网 diligentman 2周前 (04-08) 3次浏览

手势放大旋转效果

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


程序员灯塔
转载请注明原文链接:手势放大旋转效果
喜欢 (0)