简体   繁体   English

如何使背景图像持久化

[英]How to make background image persist

I've asked this before, but my understanding has increased slightly. 我之前曾问过这个问题,但是我的理解有所增加。 I've figured out how to get the user to choose a custom background image on a layout. 我想出了如何让用户在布局上选择自定义背景图片的方法。 I use this: 我用这个:

in my onCreate method: 在我的onCreate方法中:

Button player = (Button) setBg.findViewById(R.id.plBg);

                player.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        Intent i = new Intent(Intent.ACTION_PICK, 
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        i.setDataAndType(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");                                      
                        startActivityForResult(i, RESULT_LOAD_PLAYER);

                        setBg.dismiss();
                    }
                });

and in my onActivityResult method: 在我的onActivityResult方法中:

    @Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_PLAYER && resultCode == RESULT_OK && null != data) {
         Uri selectedImage = data.getData();
         String[] filePathColumn = {MediaStore.Images.Media.DATA};

         Cursor cursor = getContentResolver().query(selectedImage,
                 filePathColumn, null, null, null);
         cursor.moveToFirst();

         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         String picturePath = cursor.getString(columnIndex);
         cursor.close();

         playerBg = (ImageView) findViewById(R.id.playerBg);
         playerBg.setImageBitmap(BitmapFactory.decodeFile(picturePath));
         playerBg.setScaleType(ScaleType.CENTER_CROP);
     }
}

but when you 'back' out of the app, the background reverts back to default. 但是当您“退出”该应用程序时,背景会恢复为默认值。 how do I get that background choice to stick? 我如何坚持这一背景选择?

I've already look at this: Save bitmap to location 我已经看过了: 将位图保存到位置

But I have a hard time understand it. 但是我很难理解。
I've also tried getting it to save in shared preferences but learned that that's not what they're for. 我还尝试过将其保存在共享的首选项中,但了解到这并不是他们的目的。 I'm still a beginner that this. 我还是这个初学者。 Thanks in advance. 提前致谢。

IMO, you can have flag(boolean var) in sharedpreference which indicates that user set any background or not? IMO,您可以在sharedpreference中有flag(boolean var)来指示用户是否设置了背景? And also, if user set any background then you can save the picture path in sharedpreference. 而且,如果用户设置了任何背景,则可以将图片路径保存在sharedpreference中。 So, every time when your activity starts which onCreate(). 因此,每次您的活动开始时,哪个onCreate()都会生效。 You check the flag from sharedpref and set the background using the picture path saved in shared pref. 您从sharedpref检查标志,并使用保存在共享pref中的图片路径设置背景。

Also, I agree with matheszabi, you need to know more about android lifecycle. 另外,我同意matheszabi,您需要进一步了解android生命周期。

If you are a beginner, than you wouldn't appreciate the "know how", but maybe based on this somebody will write for you a 100% working code, which is ready for copy-paste. 如果您是初学者,那么您将不会喜欢“知道如何”,但是也许有人会以此为基础为您编写100%有效的代码,可以将其粘贴粘贴。

but when you 'back' out of the app, the background reverts back to default. 但是当您“退出”该应用程序时,背景会恢复为默认值。

That is because you don't know the Activity Lifecycle methods. 那是因为您不知道Activity Lifecycle方法。 You should load the saved bacground again in OnCreate. 您应该在OnCreate中再次加载保存的免费。 Please take a look here for Activity lifecycle methods. 请在此处查看“活动”生命周期方法。

Hope it helps somebody. 希望它能帮助到别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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