我使用Glide库为Android.我想在我的自定义文件夹中设置缓存,因此标准缓存文件夹可以是干净的(例如,使用Master Clean).出于这个原因,我使用手册中的代码,但这对我不起作用.
我的代码:
DiskCache.Factory diskCacheFactory = new DiskCache.Factory() {
@Override
public DiskCache build() {
DiskCache diskCache = DiskLruCacheWrapper.get(getFilesDir(), 1024*1024*100);
return diskCache;
}
};
new GlideBuilder(this).setDiskCache(diskCacheFactory);
Glide.with(this)
.load("http://www.website.com/1.jpg")
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
运行此应用程序后,Glide将图像保存在默认文件夹中.
我一直在使用Picasso的库在我的应用程序中将图像加载到我的gridview中,它的工作原理和我想要的完全一样.但是用户告诉我图像加载速度非常慢.我知道这是因为网络速度不佳而Picasso正在加载我的完整图像,这些图像非常大,然后调整它们以适合我的图像视图.所以我尝试使用滑动,它以几乎两倍的速度加载图像,但是在某些图像上它不像毕加索那样保持结构.例如毕加索加载图像看起来像
虽然滑行加载它们有不同的状态,这是它最初加载的
滚动后看起来像
然后最终经过大量滚动后看起来像
我非常有信心这是因为我的图像尺寸都不同而且似乎使我的占位符图像不同大小有效但我想知道的是我如何滑动以保持其初始状态,或者如何我能让毕加索加载得更快吗?我听说将颜色格式从888降低到565有戏剧性的效果,任何人都可以借给我两美分吗?感谢您提出的所有建议
编辑
这是我的imageview
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="@+id/imageView"/>
Run Code Online (Sandbox Code Playgroud)
这就是我给毕加索打电话的方式
Picasso.with(getActivity())
.load(mThumbIds[position])
.placeholder(R.drawable.placeholder)
.fit()
.into(imageView);
return imageView;
Run Code Online (Sandbox Code Playgroud)
这就是我现在称之为滑行的方式
Glide.with(getActivity())
.load(mThumbIds[position])
.asBitmap()
.placeholder(R.drawable.placeholder)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.error(R.drawable.ic_photos)
.into(imageView);
return imageView;
Run Code Online (Sandbox Code Playgroud)
如果重要,这是我的gridview
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:id="@+id/gridview"
android:numColumns="2"
android:gravity="center"
android:drawSelectorOnTop="true"
android:listSelector="@drawable/ripple"
android:stretchMode="columnWidth"
>
Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法来衡量一个ImageView图像后使用Glide或Picasso(或任何真正的东西)加载到它.基本上,我试图在某些位置在图像顶部布局其他视图,但需要最终的ImageViews尺寸才能准确地完成.
我不知道用于尝试这样做的最佳布局是什么,但我目前正在使用这个:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/viewingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
并将图像加载到viewingImageView.这些都在根FrameLayout内部,但我不认为这很重要.
这是我最近的尝试,但是作为评论,使用.getWidth()和.getHeight()在ImageView上返回0.资源的宽度/高度返回原始图像的大小.
滑行
.with(this)
.load(entry.getImageUrl())
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
mImageView.setImageBitmap(resource);
int width = mImageView.getMaxWidth(); //prints 0
int height = mImageView.getMaxHeight(); //prints 0
int resw = resource.getWidth(); //returns original image width
}
});
Run Code Online (Sandbox Code Playgroud)
那么如何加载图像然后在加载图像后测量ImageView(或其包装FrameLayout)?或者如果可能的话,测量最终布局图像的尺寸会更好,因为我知道图像并不总是根据比例类型填充整个ImageView.我对任何解决方案持开放态度,以上只是我迄今为止所尝试的内容.
通常如果我想用Glide加载图像,我会写下面的内容:
Glide.with(context)
.load(theURLOftheImage)
.error(R.drawable.ic_error_image)
.into(theImageView);
Run Code Online (Sandbox Code Playgroud)
但是如果我需要将该URL的图像加载到必须实时更改的MenuItem中呢?
以下是不可能的,因为该方法into不接受参数:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.actionbar_menu_profile_actions);
if (changeImage) {
Glide.with(this).load(theURLOftheImage).error(R.drawable.ic_error_image).into(settingsItem);
}
return super.onPrepareOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Glide和RecyclerView来加载许多具有不同尺寸和尺寸的图像(大约200个).
我有三个问题:
图像有时加载成功,有时应用程序崩溃给OOM.
如果图像成功加载,则滚动是滞后的.
如果我刷新布局并重新加载相同的图像,那么它会尝试加载图像但在完成之前失败并给出OOM.
以下是相关代码:
public static class MyViewHolder extends RecyclerView.ViewHolder {
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
ImageView imageView = holder.imageView;
Glide.with(imageView.getContext())
.load(tweetMapArray.get(listPosition).get("imageURL").toString())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.crossFade()
.into(imageView);
// imageURL is a valid URL for each item
}
@Override
public void onViewDetachedFromWindow(MyViewHolder holder) {
super.onViewDetachedFromWindow(holder);
Glide.clear(holder.imageView);
}
}
Run Code Online (Sandbox Code Playgroud)
请指导我做错了什么.我之前没有使用过如此多的图像.
我正在尝试使用Glide将图像加载到推送通知中,但它说:
FATAL EXCEPTION: Thread-9730
Process: com.monkingme.monkingmeapp, PID: 24226
java.lang.IllegalArgumentException: You must call this method on the main thread at com.bumptech.glide.util.Util.assertMainThread(Util.java:135)
Run Code Online (Sandbox Code Playgroud)
并使用的代码:
NotificationTarget notificationTarget = new NotificationTarget(
context,
rv,
R.id.remoteview_notification_icon,
notification,
NOTIFICATION_ID);
Glide.with(context.getApplicationContext())
.load(item.getString("cover_img"))
.asBitmap()
.placeholder(placeholder)
.error(placeholder)
.into(notificationTarget);
Run Code Online (Sandbox Code Playgroud)
我正在使用Aerogear的MessageHandler - > https://aerogear.org/docs/guides/aerogear-android/push/
问题是在推送通知中应用程序没有运行,因此没有主线程.有什么建议吗?
我正在使用glide v4和okhttp3与glide集成。我想更改它的超时时间。怎么做?通过扩展AppGlideModule还是还有其他方法?我已经搜索了正确的文档,但没有找到任何地方。
在应用程序中,我使用以下示例将SVG图像加载到ImageView中:
Glide.with(context)
.using(Glide.buildStreamModelLoader(Uri.class, context), InputStream.class)
.from(Uri.class)
.as(SVG.class)
.transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
.sourceEncoder(new StreamEncoder())
.cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder()))
.decoder(new SvgDecoder())
.listener(new SvgSoftwareLayerSetter())
.diskCacheStrategy(DiskCacheStrategy.NONE)
.load(uri)
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
xml中的ImageView看起来像这样:
<ImageView
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginTop="25dp"
android:layout_gravity="center"
/>
Run Code Online (Sandbox Code Playgroud)
因此,该问题可以在下图看到。右边的图标是从应用程序的资源设置的,而左边的图标是使用Glide从服务器加载的。由于某些原因,图像缩放不正确,并且看起来模糊。
我已经尝试过以下答案的解决方案: 用Glide加载SVG时,图像尺寸太小,但是不起作用。
我正在使用Recycle Adapter类,并使用它来用博客图像和描述填充片段。但是,当我关闭 BlogActivity并移至“下一个活动”时,有时应用突然崩溃,并出现以下错误:
java.lang.IllegalArgumentException:You cannot start a load for a destryoed activity at com.bumptech.glide.manager.RequestManagerRetriever.asseertNotDestroyed(RequestManagerRetriver.java:312)
我的回收适配器类代码是
package com.nepalpolice.cdp;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import …Run Code Online (Sandbox Code Playgroud) java android android-fragments android-fragmentactivity android-glide
我在build.gradle中有这个
dependencies {
...
...
implementation 'com.github.bumptech.glide:glide:3.8.0'
//getting conflict after adding this library
implementation 'com.google.android.libraries.places:places:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么Cannot resolve com.bumptech.glide.request.animation.GlideAnimation 从Google添加位置库后出现此错误。当我删除此没有问题。
android ×10
android-glide ×10
java ×2
picasso ×2
gridview ×1
image ×1
imageview ×1
okhttp3 ×1
push ×1
remoteview ×1
svg ×1
uimenuitem ×1