Search code examples
screen-orientationandroid-orientationrotateanimationscaleanimation

Need help on rotating image view with animation on orientation change (4 side rotation) in android


i have a image view and i need to rotate(with IOS like animation) the image view on orientation change in android(portrait,landscape,reverse portrait and reverse landscape).

please advice

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


Solution

  • i got it

    public void onSensorChanged(SensorEvent event) {

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