代码之家  ›  专栏  ›  技术社区  ›  Kris

需要帮助在android中旋转图像视图和方向更改动画(4侧旋转)

  •  1
  • Kris  · 技术社区  · 11 年前

    我有一个图像视图,我需要在android中旋转(使用类似IOS的动画)方向改变时的图像视图(纵向、横向、反向纵向和反向横向)。

    请指教

    void rotateAndSet(int angle) {
    
        if (currentAngle != angle || currentImage != currentBaseImage) { // This
            // is
            // to
            // remove
            // unnecessary
            // drawing
            currentAngle = angle;
            currentImage = currentBaseImage;
            myImg = decodeBase64(currentBaseImage);
    
            matrix = new Matrix();
            matrix.postRotate(angle);
            Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
                    myImg.getHeight(), matrix, true);
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            param.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    
            /*RotateAnimation animation = new RotateAnimation(0, 90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 
            animation.setInterpolator(new LinearInterpolator()); 
            animation.setFillAfter(true); 
            animation.setDuration(800); 
            ivFullScreen.startAnimation(animation); */
            ivFullScreen.setLayoutParams(param);
            ivFullScreen.setImageBitmap(rotated);
        }
    
    }
    

    enter image description here

    1 回复  |  直到 11 年前
        1
  •  0
  •   Kris    11 年前

    我得到了它

    SensorChanged上的公共无效(SensorEvent事件){

        synchronized (this) {
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                RotateAnimation animation = null;
                if (event.values[0] < 4 && event.values[0] > -4) {
                    if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) {
                        // UP
                        orientation = ExifInterface.ORIENTATION_ROTATE_90;
                        Log.i("testme","up");
                        animation = getRotateAnimation(0);
                        degrees = 0;
                    } else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) {
                        // UP SIDE DOWN
                        Log.i("testme","up side down");
                        orientation = ExifInterface.ORIENTATION_ROTATE_270;
                        animation = getRotateAnimation(180);
                        degrees = 180;
                    }
                } else if (event.values[1] < 4 && event.values[1] > -4) {
                    if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) {
                        // LEFT
                        Log.i("testme","left");
                        orientation = ExifInterface.ORIENTATION_NORMAL;
                        animation = getRotateAnimation(-270);
                        degrees =-270;
                    } else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) {
                        // RIGHT
                        Log.i("testme","right");
                        orientation = ExifInterface.ORIENTATION_ROTATE_180;
                        animation = getRotateAnimation(270);
                        degrees = 270;
                    }
                }
                if (animation != null) {
                    ivFullScreen.startAnimation(animation);
                }
            }
    
        }
    
    }
    
    推荐文章