我转动屏幕时为什么图像会消失?

时间:2014-05-20 10:06:45

标签: android xamarin

我理解一个非常简单的例子 - 在android中使用sd卡在imageview中显示单个图像。我正在使用Xamarin和C#,并通过USB在真实设备上调试应用程序。

组件“ImageView”正确显示图像,但在旋转屏幕时,图像将不会显示。

Activity1.cs

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;
using Android.Graphics;

namespace DisplayImageFromSDCard
{
    [Activity(Label = "DisplayImageFromSDCard", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        ImageView imageView;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            imageView = FindViewById<ImageView>(Resource.Id.imageView1);
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += button_Click;
        }

        void button_Click(object sender, EventArgs e)
        {
            var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
            var imageFilePath = System.IO.Path.Combine(sdCardPath, "SampleImageFile.jpg");

            if (System.IO.File.Exists(imageFilePath))
            {
                var imageFile = new Java.IO.File(imageFilePath);
                Bitmap bitmap = BitmapFactory.DecodeFile(imageFile.AbsolutePath);
                imageView.SetImageBitmap(bitmap); 
            }
            else
            {
                //Display No Image Found
            }
        }
    }
}

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/MyButtonText" />
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView1" />
</LinearLayout>

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

在以下情况下重新创建视图: - 屏幕关闭并重新打开, - 屏幕旋转

所以当视图创建为默认视图时

 <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView1" />

你那里没有任何形象......这就是为什么它的消失。

您需要保存当前状态,以便在重新创建视图时,所有内容都将重新开启

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Read values from the "savedInstanceState"-object and put them in your textview
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // Save the values you need from your textview into "outState"-object
    super.onSaveInstanceState(outState);
}

答案 1 :(得分:0)

正如laymelek所说,屏幕旋转时图像消失的原因是活动被破坏并重新创建。 在此过程中,不会保留所选图像或其他应用程序特定状态等内容。

通常使用活动的InstanceState保留自定义应用程序状态信息,如laymelek所述。 但这不是Bitmaps或大型对象的建议行为。

在这种情况下,你应该将图像保留在内存中,你可以使用自定义片段来完成这项工作,如official documentation

所示

您可以查看我对其他question

的回答