【问题标题】:Taking time to load background images in android花时间在android中加载背景图像
【发布时间】:2016-07-28 05:21:09
【问题描述】:

我正在使用 Picasso 加载背景图像,并且我正在使用相对布局。背景图像需要时间来加载,有些甚至没有加载。我的一段代码是,

        final int k = random.nextInt(20);
        ImageView imageView= (ImageView)findViewById(R.id.backgrd);
        TypedArray imgb = getResources().obtainTypedArray(R.array.backg);
        int resourceId = imgb.getResourceId(k, 0);

 Picasso.with(getApplicationContext()).
                load(resourceId).
                fit().
                noPlaceholder().
                into(imageView);

我也尝试使用 resize() Picasso Taking time to load images,但它不起作用。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative1"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


        <ImageView
            android:id="@+id/backgrd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            />

     <ImageView
        android:id="@+id/img1"
        android:paddingTop="90dp"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        />

【问题讨论】:

  • 您的简洁措辞问题是什么?
  • 检查带宽或者图像是否存储在本地,然后使用 UniversalImageLoader
  • @RabidMutant 我没有收到你的问题。
  • 我不确定您的问题是否是 (a) 为什么 picasa 不起作用? (b) 谁慢? (c) 我怎样才能做得更快? (d) 我如何在后台线程中执行此操作?或者 (e) 当 picasa 无法加载图片时我该怎么办?
  • 让您的问题更加清晰具体。所以,你会得到更精确的解决方案。

标签: android image picasso


【解决方案1】:

检查您用于加载的可绘制图像的大小和分辨率。

xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
Generalised Dpi values for screens:

ldpi Resources for low-density (ldpi) screens (~120dpi)
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
Therefore generalised size of your resources (assuming they are full screen):

ldpi
Vertical = 426 * 120 / 160 = 319.5px
Horizontal = 320 * 120 / 160 = 240px
mdpi
Vertical = 470 * 160 / 160 = 470px
Horizontal = 320 * 160 / 160 = 320px
hdpi
Vertical = 640 * 240 / 160 = 960px
Horizontal = 480 * 240 / 160 = 720px
xhdpi
Vertical = 960 * 320 / 160 = 1920px
Horizontal = 720 * 320 / 160 = 1440px

px = dp*dpi/160

https://stackoverflow.com/a/16352208/5567283

【讨论】:

  • 我只有 19 张背景图片,总大小为 106 kB。每个图像的大小为 1 kB 到 10 kB。我需要再减少一些吗?
  • 连同 .xml 文件?
【解决方案2】:

从您上面的评论中可以清楚地看出您正在使用 Picasso 加载图像并且您的图像存储在本地

所以根据上述情况,这里是解决方案

首先停止使用毕加索,b'se 只要图像是本地的,就不需要使用 3rd 方库来加载它们。

其次,在你的代码中使用 CardView 作为主布局而不是相对布局,就像 --

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/VechicleCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="@dimen/margin_small_five"
    app:cardElevation="@dimen/card_elevation_two"
    app:cardUseCompatPadding="true">


</android.support.v7.widget.CardView>

cardview 之后把你的 imageVIew "@+id/backgrd" 作为第一个孩子如下

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="0dp">

    <ImageView
        android:src="@drawable/your_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>

    <... your layout

    .../>


</android.support.v7.widget.CardView>

现在从 Class 文件中设置 imageView 的背景

希望这对你有用!!

快乐编码 ;)

【讨论】:

  • 其实我试过用Bitmap代替Picasso,用完后回收。因此,OOM 问题得到了解决,并且针对该背景,我稍微减小了它的大小。它工作正常。谢谢大家。我的位图代码
  • if (imageView != null) imageView.setImageBitmap(null); if (bitmap != null) { bitmap.recycle(); }位图=空;位图位图= BitmapFactory.decodeStream(getResources().openRawResource(resourceId)); imageView.setImageBitmap(位图);
猜你喜欢
  • 2015-02-28
  • 2011-03-09
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多