C# Android:如何使用C代码中的位图刷新屏幕?

C# Android:如何使用C代码中的位图刷新屏幕?,c#,android,bitmap,android-imageview,C#,Android,Bitmap,Android Imageview,如何使用C代码中的位图刷新屏幕 我希望动态显示一个不断变化的位图,但我只看到最终的图像 我已经彻底搜索过了,但没有找到答案 为了了解情况,我在Visual Studio 2017中编写了一个noddy程序,该程序首先显示红色,然后显示蓝色,如下所示 protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout

如何使用C代码中的位图刷新屏幕

我希望动态显示一个不断变化的位图,但我只看到最终的图像

我已经彻底搜索过了,但没有找到答案

为了了解情况,我在Visual Studio 2017中编写了一个noddy程序,该程序首先显示红色,然后显示蓝色,如下所示

  protected override void OnCreate(Bundle bundle) {
      base.OnCreate(bundle);
      // Set our view from the "main" layout resource
      SetContentView (Resource.Layout.Main);

      // The imageView is needed to contain the drawing.
      ImageView image_view = FindViewById<ImageView>(Resource.Id.imageView1);

      // Create a new image bitmap
      Bitmap drawBitmap = Bitmap.CreateBitmap(50, 100, Bitmap.Config.Rgb565);
      drawBitmap.EraseColor(Color.Red);

      // Attach the bitmap to the ImageView
      image_view.SetImageBitmap(drawBitmap);

      // Wait 2 seconds before changing the colour,
      // to give time to see it.
      TimeSpan delayTime = new TimeSpan(0, 0, 2);  // 2 seconds
      Task.Delay(delayTime).Wait();

      // Change the color
      drawBitmap.EraseColor(Color.Blue);
      image_view.SetImageBitmap(drawBitmap);

      int k = 0;
  }
当我运行它时,我仍然看到一个蓝色的屏幕。调试输出报告(除其他外)

线程已启动:#8 InspectorDebugSession(7):HandletTargetEvent:线程启动 07-17 11:52:44.260 D/Mono(4323):[0x9d77e930]工人启动 07-17 11:52:44.283 D/Mono(4323):[0x9daff930]爬山,更改最大线程数2 将颜色索引设置为0 07-17 11:52:45.403单声道标准输出(4323):将颜色索引设置为0 07-17 11:52:45.406 D/Mono(4323):DllImport在以下位置搜索:“uu Internal”(“(null)”)。 07-17 11:52:45.406 D/Mono(4323):搜索“java_interop_jnienv_call_void_method_a”。 07-17 11:52:45.406 D/Mono(4323):探测“java互操作调用方法”。 07-17 11:52:45.406 D/Mono(4323):被发现为“java_interop_jnienv_call_void_method_a”。 将颜色索引设置为1 07-17 11:52:45.409单声道标准输出(4323):将颜色索引设置为1 将颜色索引设置为2 07-17 11:52:45.410单声道标准输出(4323):将颜色索引设置为2 07-17 11:52:51.285 D/Mono(4323):[0x9d9fe930]爬山,更改最大线程数3 07-17 11:53:19.300 D/Mono(4323):[0x9d9fe930]爬山,更改最大螺纹数2 07-17 11:53:33.307 D/Mono(4323):[0x9d9fe930]爬山,更改最大线程数3 螺纹加工:#4 InspectorDebugSession(7):手持目标事件:线程停止 线程已启动:#9 InspectorDebugSession(7):HandletTargetEvent:线程启动 线程“未知”(0x4)已退出,代码为0(0x0)。 07-17 11:54:01.323 D/Mono(4323):[0x9d9fe930]爬山,更改最大线程数2 07-17 11:54:15.330 D/Mono(4323):[0x9daff930]爬山,更改最大螺纹数3 检查者任务(7):处理目标事件:目标停止 视察员布格会议(7):已处理 07-17 11:54:46.701 D/Mono(4323):[0x9d9fe930]爬山,更改最大螺纹数2 螺纹加工:#9 线程已启动:#10 线程“未知”(0x9)已退出,代码为0(0x0)。 消息“将颜色索引设置为…”之间的时间小于0.01秒,因此延迟不起作用

关于这一切花了多长时间的信息从未出现

如果我在最后一行“That take…”处放置一个断点,然后重新运行,我会看到经过的
drawTime
只有16毫秒


有谁能告诉我需要做些什么才能使这项工作正常进行,这样我才能在预期的延迟时间内看到连续的颜色吗?

您可以调用
image\u view.SetImageBitmap
两次,而不给
image\u view
响应的机会(重新绘制本身),它将不起作用,例如使用
Handler
s,请参阅您提供的链接中与Java相关的信息,而不是与C#。我不知道如何重写C#中类
Runnable
中的
Run
函数,因为这个类是密封的。请告知如何将要运行的代码传递到处理程序中!我从未使用过xamarin,但希望这能有所帮助:您调用
image\u view.SetImageBitmap
两次,但没有给
image\u view
一个响应的机会(重新绘制本身),它将不起作用,例如使用
Handler
s,请查看您提供的与Java相关的链接中的信息,而不是C。我不知道如何重写C#中类
Runnable
中的
Run
函数,因为这个类是密封的。请告知如何将要运行的代码传递到处理程序中!我从未使用过xamarin,但希望这能有所帮助:
public class MainActivity : Activity {
    private Bitmap m_drawBitmap;
    private ImageView m_image_view;
    private int m_count = 0;

    private static readonly Color[] colors = new Color[3] {Color.Red, Color.Green, Color.Blue };

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // The imageView is needed to contain the drawing.
        m_image_view = FindViewById<ImageView>(Resource.Id.imageView1);

        // Create a new image bitmap
        m_drawBitmap = Bitmap.CreateBitmap(50, 100, Bitmap.Config.Rgb565);

        long frameStartTime = SystemClock.ElapsedRealtime();

        Handler h = new Handler();
        Action myAction = () =>
        {
            Console.WriteLine("Setting color index to {0}", m_count);

            // the code that I want to delay goes here
            m_drawBitmap.EraseColor(colors[(m_count++) % 3]);

            // Attach the bitmap to the ImageView
            m_image_view.SetImageBitmap(m_drawBitmap);
        };

        h.PostDelayed(myAction, 2000);

        h.PostDelayed(myAction, 2000);

        h.PostDelayed(myAction, 2000);

        // time taken to draw it all in milli-secs
        long drawTime = SystemClock.ElapsedRealtime() - frameStartTime;

        Console.WriteLine("That took {0} ms", drawTime);
    }
}
Thread started: #8 InspectorDebugSession(7): HandleTargetEvent: ThreadStarted 07-17 11:52:44.260 D/Mono ( 4323): [0x9d77e930] worker starting 07-17 11:52:44.283 D/Mono ( 4323): [0x9daff930] hill climbing, change max number of threads 2 Setting color index to 0 07-17 11:52:45.403 I/mono-stdout( 4323): Setting color index to 0 07-17 11:52:45.406 D/Mono ( 4323): DllImport searching in: '__Internal' ('(null)'). 07-17 11:52:45.406 D/Mono ( 4323): Searching for 'java_interop_jnienv_call_void_method_a'. 07-17 11:52:45.406 D/Mono ( 4323): Probing 'java_interop_jnienv_call_void_method_a'. 07-17 11:52:45.406 D/Mono ( 4323): Found as 'java_interop_jnienv_call_void_method_a'. Setting color index to 1 07-17 11:52:45.409 I/mono-stdout( 4323): Setting color index to 1 Setting color index to 2 07-17 11:52:45.410 I/mono-stdout( 4323): Setting color index to 2 07-17 11:52:51.285 D/Mono ( 4323): [0x9d9fe930] hill climbing, change max number of threads 3 07-17 11:53:19.300 D/Mono ( 4323): [0x9d9fe930] hill climbing, change max number of threads 2 07-17 11:53:33.307 D/Mono ( 4323): [0x9d9fe930] hill climbing, change max number of threads 3 Thread finished: #4 InspectorDebugSession(7): HandleTargetEvent: ThreadStopped Thread started: #9 InspectorDebugSession(7): HandleTargetEvent: ThreadStarted The thread 'Unknown' (0x4) has exited with code 0 (0x0). 07-17 11:54:01.323 D/Mono ( 4323): [0x9d9fe930] hill climbing, change max number of threads 2 07-17 11:54:15.330 D/Mono ( 4323): [0x9daff930] hill climbing, change max number of threads 3 InspectorDebugSession(7): HandleTargetEvent: TargetStopped InspectorDebugSession(7): Disposed 07-17 11:54:46.701 D/Mono ( 4323): [0x9d9fe930] hill climbing, change max number of threads 2 Thread finished: #9 Thread started: #10 The thread 'Unknown' (0x9) has exited with code 0 (0x0).