如何在主布局中添加由另一个线程控制的 imageView?

发布于 2024-12-11 19:02:42 字数 1821 浏览 0 评论 0原文

我正在为 Android 制作一个应用程序。我通过单个主线程按下按钮就达到了我的目标! (在 ImageView 中显示保存在 SD 上的图像)但我需要使用线程来完成此操作以节省一些时间,因为我将创建其他线程。

当我在新线程上执行此操作时,会出现一个警告,告诉我:

“只有创建视图层次结构的原始线程才能触及它的 意见。”

并且图像未打开。

这是这段代码:

public class intsocketclient extends Activity implements  OnClickListener{
    public ImageView imagen;
        private Button connectPhones;              
        private Handler conectarhandler = null;
    private Runnable conectarunner = null;
public boolean condicion = true;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);    
            imagen = (ImageView) findViewById(R.id.imagen);
            connectPhones = (Button) findViewById(R.id.connect_phones);
            connectPhones.setOnClickListener(this);

            conectarhandler = new Handler();        
            conectarunner = new Runnable() { 
                public void run() {
                conectayenvia(); 
            conectarhandler.post(this);          

                }
          };                  
        }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub.
     if(v==connectPhones) {              
         new Thread (conectarunner).start();                 
     }
}

public void conectayenvia () {
    if (condicion){              
                  condicion = false;
                  Bitmap bMap = BitmapFactory.decodeFile("/sdcard/recibido.jpg");
                  imagen.setImageBitmap(bMap);                                      
     }  
    }   
}

但我真的需要它是这样的。

是否可以采用主布局(原始的“main.xml”)和某种“添加”它是另一个主文件(一个仅包含 imageView 的“threadmain.xml”),而且还具有按第一个原始“main.xml”布局的按钮和其他类型的功能??????

I'm making an app for Android. I have reached my goal with the single main thread pushing a button!!! (show an image saved on the SD in an ImageView) But I need to do it with threading to save some time and because I will make other threads.

The problem when I do this on a new thread a warning appears that tells me:

"Only the original thread that created a view hierachy can touch its
views."

And the Image is not opened.

Here is this code:

public class intsocketclient extends Activity implements  OnClickListener{
    public ImageView imagen;
        private Button connectPhones;              
        private Handler conectarhandler = null;
    private Runnable conectarunner = null;
public boolean condicion = true;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);    
            imagen = (ImageView) findViewById(R.id.imagen);
            connectPhones = (Button) findViewById(R.id.connect_phones);
            connectPhones.setOnClickListener(this);

            conectarhandler = new Handler();        
            conectarunner = new Runnable() { 
                public void run() {
                conectayenvia(); 
            conectarhandler.post(this);          

                }
          };                  
        }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub.
     if(v==connectPhones) {              
         new Thread (conectarunner).start();                 
     }
}

public void conectayenvia () {
    if (condicion){              
                  condicion = false;
                  Bitmap bMap = BitmapFactory.decodeFile("/sdcard/recibido.jpg");
                  imagen.setImageBitmap(bMap);                                      
     }  
    }   
}

But I really need it to be this way.

Is it possible to take the main layout (the original "main.xml") and some kind of "add" over it another main file (a "threadmain.xml" which only contains the imageView)but also with the capability of pushing buttons and other kind of things of the first original "main.xml" layout????????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜巴黎 2024-12-18 19:02:42

使用AsyncTask,因为它会自动处理线程,AsyncTask的preExecute和postExecute方法在UI线程上运行。

  private class DF extends AsyncTask<Void, Void, Void>{
         private Bitmap bMap;
    @Override
    protected Void doInBackground(Void... params) {
                    if (condicion){              
              condicion = false;
              bMap = BitmapFactory.decodeFile("/sdcard/recibido.jpg");

                     }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
              imagen.setImageBitmap(bMap);                                      
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
}

在 onClick 中调用 asyncTask。

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub.
     if(v==connectPhones) {              
             new DF().execute();
     }
}

use AsyncTask, as it will handle threads automatically, the preExecute and postExecute methods of AsyncTask run on UI thread.

  private class DF extends AsyncTask<Void, Void, Void>{
         private Bitmap bMap;
    @Override
    protected Void doInBackground(Void... params) {
                    if (condicion){              
              condicion = false;
              bMap = BitmapFactory.decodeFile("/sdcard/recibido.jpg");

                     }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
              imagen.setImageBitmap(bMap);                                      
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
}

call the asyncTask in onClick.

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub.
     if(v==connectPhones) {              
             new DF().execute();
     }
}
寄风 2024-12-18 19:02:42

您的问题是您尝试不在 UI 线程上运行 UI 方法。解决方案是调用 runOnUiThread 来自另一个线程。

Your problem is that you're trying to run UI methods not on the UI thread. The solution is to call runOnUiThread from the other thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文