我正在开发一个应用程序,我需要在其中提供不同方向的不同活动的背景图像。所以我以这种方式接近:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLanguage();
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
}
Run Code Online (Sandbox Code Playgroud)
和
android:configChanges="locale|orientation|screenSize"
Run Code Online (Sandbox Code Playgroud)
并在onCreate()假设用户将在纵向模式下启动应用程序时,我为纵向设置了背景图像。
一切正常,因为当用户改变他们的模式时,相应的背景被设置等等。
但是,如果用户在手机处于横向模式时启动此应用程序,正如我在用户将以纵向模式启动应用程序之前假设的那样,在第一次启动时显示的纵向图像。
那么我该如何解决这个问题呢?一句话,为不同的方向设置不同的背景图像的最佳方法是什么?我在正确的轨道上吗?
我已将位图图像转换为字符串以保存它:
............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
然后我从字符串中检索位图以设置活动的背景,如下所示:
byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);
Run Code Online (Sandbox Code Playgroud)
一切正常,但图像质量大大降低.如何保持图像质量与原始图像相同?我在这里做错了什么,降低了质量?