rotate image
时我想要configuration changes
。我发现了一个正常工作的代码,但它需要Api 17。我怎样才能使兼容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);
}
}
在onConfigurationChange
中的我必须在给定的方法中处理这个图像旋转。
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
}
else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
}
还是有其他方法更可取。
此声明需要API 17
int rotation = getDisplay().getRotation();
答案 0 :(得分:1)
Display.getRotation()
引入了api level 8,可能是getDispaly()
,需要api级别17.您可以使用getSystemService
检索WindowManager,并通过它检索默认显示:
WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int rotation = display.getRotation();
在API级别11的时间已经存在的一切