加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_常德站长网 (https://www.0736zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 移动互联 > 应用 > 正文

Java 加载位图时,应用程序UI将变得无响应

发布时间:2022-12-06 15:28:20 所属栏目:应用 来源:
导读:  Java 加载位图时,应用程序UI将变得无响应

  javaandroid

  Java 加载位图时,应用程序UI将变得无响应,java,android,bitmap,android-asynctask,wallpaper,Java,Android,Bitmap,Android Asynctask,

  Java 加载位图时,应用程序UI将变得无响应
 
  javaandroid
 
  Java 加载位图时,应用程序UI将变得无响应,java,android,bitmap,android-asynctask,wallpaper,Java,Android,Bitmap,Android Asynctask,Wallpaper,我有一个倒计时,每15秒调用一次ChangeWallpaper方法。墙纸应该会改变,但当我尝试打开应用程序时,它会使应用程序抽屉屏幕在几秒钟内无响应。当应用程序最终打开时,我选择的所有内容都需要5-10秒才能响应。我在Android Developer中读到AsyncTask,它应该在UI线程之外加载位图,防止应用程序挂起,但它似乎不起作用以下代码在我的活动类中:/** changeWallpaper() **/ - called by CountDownTimer every 15 sec
 
  我有一个倒计时,每15秒调用一次ChangeWallpaper方法。墙纸应该会改变,但当我尝试打开应用程序时,它会使应用程序抽屉屏幕在几秒钟内无响应。当应用程序最终打开时,我选择的所有内容都需要5-10秒才能响应。我在Android Developer中读到AsyncTask,它应该在UI线程之外加载位图,防止应用程序挂起,但它似乎不起作用
 
  以下代码在我的活动类中:
 
  /** changeWallpaper() **/ - called by CountDownTimer every 15 seconds
  protected void changeWallpaper() throws IOException {
      Integer totalimages = finallist.size();
      if (lastloopcount == totalimages) { // if end of the list of images is reached, it resets and goes back to top.
          loopcount = 0;
          lastloopcount = 0;
      }
      for (String imagepath : finallist) { // "finallist" is global variable with all the image's paths in an array list. The Loop count is to select the next image in the array list every 15 seconds.
          loopcount++;
          if (loopcount > lastloopcount) {
              lastloopcount = loopcount;
              loopcount = 0;
              WallpaperManager wm = WallpaperManager.getInstance(this);
              wm.setBitmap(decodeImageFromPath(imagepath));
              break;
          }
      }
  }
  /** AsyncTask Wallpaper Load **/
  class BitmapWorkerTask extends AsyncTask {
      public BitmapWorkerTask(ImageView imageView) {
          new WeakReference(imageView);
      }
      @Override
      protected Bitmap doInBackground(Integer... params) {
          return null;
      }
  }
  /** decodeImageFromPath() **/
  public Bitmap decodeImageFromPath(String imagepath) {
      DisplayMetrics displayMetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
      int height = displayMetrics.heightPixels;
      int width = displayMetrics.widthPixels << 2;
      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(imagepath, options);
      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, width, height);
      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeFile(imagepath, options);
  }
  /** WallpaperManager (Method) **/
  public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) {
      // ... Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      int stretch_width = Math.round((float)width / (float)reqWidth);
      int stretch_height = Math.round((float)height / (float)reqHeight);
      if (stretch_width <= stretch_height) return stretch_height;
      else return stretch_width;
  }
  然后:
 
  处理位图的主代码应该从doInBackground方法中调用。否则它与这里的同步调用相同
 
  @Overrideprotected Bitmap doInBackground(String... params) {Bitmap bmp = decodeImageFromPath(params[0]);return bmp;}protected void onPostExecute(Bitmap bmp) {wm.setBitmap(bmp)}您可以参考此链接上的示例。
 
  您甚至不使用BitmapWorkerTask!当您在代码中写入一些异步任务时,在后台处理任意部分的代码是不可能的。你知道,你也得用它
 
  将代码的持久部分移到AsyncTask的doInBackground方法中,并像这样调用它:new BitmapWorkerTask.execute
 
  编辑
 
  要传递图像路径,请将BitmapWorkerTask like的定义更改为以下内容。。。扩展异步任务。。。注意字符串而不是整数,并将图像路径作为参数传递给execute方法
 
  新建BitmapWorkerTask.executeimagePath
 
  请注意,现在这是异步运行的应用程序无效,因此execute调用会立即返回,但加载映像仍需要一些时间
 
  也请阅读文章。
 
  Hi Kumar,因此我需要将doInBackground的返回更改为DecodeMagefromPathImagePath;但是如何从ChangeWallpaper中获取Imagepath变量呢?谢谢!我也使用了您的想法:您好,我已经实现了您的方法,还没有运行它来检查它是否有效。但是修改我的代码带来了另一个完全无关的错误。在我的微调器中,onItemSelected the parseInt开发了此错误:此方法parseIntString对于字符串类型未定义此代码中的所有parseInt都有相同的错误:请参阅上面的主要帖子此错误是方法parseIntString对于字符串类型未定义您甚至不使用BitmapWorkerTask!当您在代码中写入一些异步任务时,在后台处理任意部分的代码是不可能的。你也必须使用它,你知道。如果我将decodeMageFromPath添加到doInBackground,我如何才能将imagepath变量传递到decodeMageFromPath?imagepath需要在decodeMageFromPath中定义,但我需要使用更改壁纸中确定的imagepath的内容,而不是定义它,请参见对我答案的编辑,请,更改代码。但UI的行为仍然相同。没反应谢谢!我已经用你的想法解决了。
 
  new BitmapWorkerTask(null).execute(imagepath);
      /** AsyncTask Wallpaper Load **/
  class BitmapWorkerTask extends AsyncTask {
      public BitmapWorkerTask(ImageView imageView) {
          new WeakReference(imageView);
      }
      @Override
      protected Bitmap doInBackground(String... params) {
          DisplayMetrics displayMetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
          int height = displayMetrics.heightPixels;
          int width = displayMetrics.widthPixels << 2;
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(params[0], options);
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, width, height);
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          Bitmap bmp = BitmapFactory.decodeFile(params[0], options);
          return bmp;
      }
      protected void onPostExecute(Bitmap bmp) {
          Context context = getApplicationContext();
          WallpaperManager wm = WallpaperManager.getInstance(context);
          try {
              wm.setBitmap(bmp);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
      /** WallpaperManager (Method) **/
  public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) {
      // ... Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      int stretch_width = Math.round((float)width / (float)reqWidth);
      int stretch_height = Math.round((float)height / (float)reqHeight);
      if (stretch_width <= stretch_height) return stretch_height;
      else return stretch_width;
  }
  @Overrideprotected Bitmap doInBackground(String... params) {Bitmap bmp = decodeImageFromPath(params[0]);return bmp;}protected void onPostExecute(Bitmap bmp) {wm.setBitmap(bmp)}new BitmapWorkerTask ().execute(imagePath);
 

(编辑:应用网_常德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!