简体   繁体   English

如何在可运行文件中绘制 bitmap?

[英]How to drawing bitmap in runnable?

I want to draw bitmap in runnable but dont work.我想在可运行但不工作的情况下绘制 bitmap。 The code gives an exit error from the application.该代码给出了应用程序的退出错误。 I commented the non-working part in the code.我评论了代码中的非工作部分。

 private Runnable updateTimerThread = new Runnable() {

        public void run() {


                // This code block dont run. Application exit error.
                bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setColor(Color.RED);
                rect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
                canvas.drawArc(rect, 0F, 360F, true, paint);
                imageView.setImageBitmap(bitmap);
                imageView.invalidate();

                customHandler.postDelayed(this, showTime);
   

                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);
            
         }
    };

Submit your Runnable to an ExecutorService将您的Runnable提交给ExecutorService

Your question is not clear.你的问题不清楚。 But taking your question as written, I'll guess you neglected to call upon the code shown.但是按照您的书面问题,我猜您忽略了调用显示的代码。 A Runnable does not magically execute itself. Runnable不会神奇地执行自身。 You must submit an instance of your Runnable to an executor service.您必须将Runnable的实例提交给执行程序服务。

Given the variable name updateTimeThread , I am guessing you want this code to run repeatedly on a background thread.给定变量名updateTimeThread ,我猜您希望此代码在后台线程上重复运行。 So we need a scheduled executor service to run repeatedly.所以我们需要一个定时执行服务来重复运行。

That name shows some confusion on your part.这个名字显示了你的一些困惑。 A Runnable is not a thread. Runnable不是线程。 A Runnable is a task to be executed by an ExecutorService on one of its managed threads. Runnable是由ExecutorService在其托管线程之一上执行的任务。

Let's define a simpler runnable to get some rudimentary code up and running.让我们定义一个更简单的 runnable 来启动和运行一些基本代码。

Runnable task = new Runnable() {
    public void run() {
        System.out.println( "Running the run method at " + Instant.now() ) ;
    }
}

Establish your executor service.建立你的执行者服务。

ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor() ;

Submit an instance of your Runnable to be run every so often.提交您的Runnable实例以每隔一段时间运行一次。

long delay = 0 ; 
long period = 1 ;
ses.scheduleAtFixedRate( task , delay , period , TimeUnit.SECONDS ) ;

Be sure to eventually shut down your executor service.请务必最终关闭您的执行程序服务。 Otherwise its backing thread pool may continue to run indefinitely, like a zombie ♂️.否则它的后备线程池可能会无限期地继续运行,就像僵尸一样♂️。

And as with nearly all GUI toolkits, in Android never access the UI widgets from a background thread.与几乎所有 GUI 工具包一样,在 Android 中,从不从后台线程访问 UI 小部件。 Do calculations on the background thread.在后台线程上进行计算。 But when it comes time to update the UI use whatever mechanism is provided to ask the UI thread to run some code to update the UI widgets.但是当需要更新 UI 时,请使用提供的任何机制来要求 UI 线程运行一些代码来更新 UI 小部件。 (I don't know Android's mechanism.) (我不知道Android的机制。)

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

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