简体   繁体   中英

Change image rotation in onConfigurationChange

I want to rotate image when configuration changes . I found a working code but it requires Api 17. How could i make it compatible for Api 11.

public class CustomImage extends ImageView {

    private Bitmap mSource;

    public CustomImage(Context context) {
        super(context);
        init();
    }

    private CustomImage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private CustomImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mSource = BitmapFactory.decodeResource(getResources(),
                R.drawable.icony);
    }

    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int px = Math.round(dp
                * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        int rotation = getDisplay().getRotation();

        // Checks the orientation of the screen

       /* if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            //Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }*/

        int angle = 0;
        switch (rotation) {
        case Surface.ROTATION_90:
            angle = -90;
            break;
        case Surface.ROTATION_180:
            angle = 180;
            break;
        case Surface.ROTATION_270:
            angle = 90;
            break;
        default:
            angle = 0;
            break;
        }

        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        Bitmap bmp = Bitmap.createBitmap(mSource, 0, 0, mSource.getWidth(),
                mSource.getHeight(), matrix, true);
        setImageBitmap(bmp);
    }

}

in onConfigurationChange i have to handle this image rotation in given methods.

 if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
 {
 }
 else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
 {
 } 

Or is there any other method which is preferable.

This statement requires API 17

int rotation = getDisplay().getRotation(); 

Display.getRotation() was introduced with api level 8, and probably is getDispaly() which requires api level 17. You can use getSystemService to retrieve the WindowManager and, through it, the default display:

WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int rotation = display.getRotation();

everything was already there at API level 11's time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM