gpt4 book ai didi

java - UIL LinearBackground 图像不起作用

转载 作者:行者123 更新时间:2023-12-01 13:10:56 25 4
gpt4 key购买 nike

我花了半天时间阅读文档和示例。我找不到解决办法。我在 Activity 中有一个 ListView,每个项目都有 LinearLayout 和由 nostr13 使用 Android-Universal-Image-Loader 加载的背景图像。

我使用这个库将 LinearBackground 图像作为背景。在我的 Activity 中,我下载了一个 JSON,解析并使用 URI 创建一个字符串数组。

问题是图书馆的打印是错误的!它与 ImageView 配合使用效果很好,但如果我将 LinearBackground 与 loadImage() 一起使用而不是 displayImage() ,则它不会正确放置背景。慢动作看到的过程如下:创建正确数量的元素 (4),将第一个加载的图像放置为一行的背景,然后一旦加载另一个图像,库就会用这个不同的新图像替换旧背景,然后它放置另一个图像并停止。这是该错误的一个示例。总是不一样!!!其中一些是空的,但 onLoadingComplete 事件被调用。我认为这是缓存的问题,并且我尝试过 UIL 的选项,但没有成功。图片只有 4 个,而且非常小(每个 <10kb)。

我在onPostExecute事件中设置了适配器,可以吗?

可能是什么问题?

这里是重要的代码:

Activity 代码(我删除了不相关的代码以帮助查找问题):

    public class myActivity extends Activity {
Context context;

// Output Vars
String mStrings[];
ListView listView;

// UIL
DisplayImageOptions options;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_stagioni);

listView = (ListView) findViewById(R.id.listView);

options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)
.showImageForEmptyUri(R.drawable.fail)
.showImageOnFail(R.drawable.fail)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.build();


startAsynkTask(context);
}
public void startAsynkTask(final Context context) {

AsyncTask<String, Void, Void> task = new AsyncTask<String, Void, Void>() {
private ProgressDialog pd;
@Override
protected void onPreExecute() {
pd = new ProgressDialog(ActivityStagioni.this);
pd.setTitle(getResources().getString(R.string.dialog_loading));
pd.setMessage(getResources().getString(R.string.dialog_loading_stagioni));
pd.setCancelable(true);
pd.setCanceledOnTouchOutside(true);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected Void doInBackground(String... arg0) {
//... Not relevant code ...
}
@Override
protected void onPostExecute(Void result) {
pd.dismiss();

((ListView) listView).setAdapter(new ImageAdapter());
listView.setOnItemClickListener(new OnItemClickListener()
{
//... Not relevant code ...
});
}
};
task.execute();
}



public class ImageAdapter extends BaseAdapter {
ImageLoader imageLoader;

public ImageAdapter() {

imageLoader = ImageLoader.getInstance();
}

@Override
public int getCount() {
return mStrings.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();


final ViewHolder holder;
View view = convertView;
if (view == null) {
view = getLayoutInflater().inflate(R.layout.stagioni_item, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (LinearLayout) view.findViewById(R.id.linearLayoutLocandina);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}

imageLoader.loadImage(mStrings[position], new SimpleImageLoadingListener() {
//final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
//if (loadedImage != null) {
// boolean firstDisplay = !displayedImages.contains(imageUri);
// if (firstDisplay) {
BitmapDrawable background = new BitmapDrawable(loadedImage);
holder.imageView.setBackgroundDrawable(background);
//}
// }

}
});

return view;
}

ListView 项目是:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="horizontal"
android:weightSum="5"
android:baselineAligned="false">

<LinearLayout
android:id="@+id/linearLayoutLocandina"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="25dp"
android:layout_weight="5" >

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="25dp"
android:layout_weight="5"
android:orientation="vertical" >

</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:alpha="1"
android:background="#80000000" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >

<TextView
android:id="@+id/textView_NumeroStagione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|center_horizontal"
android:text="30"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />

</LinearLayout>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
android:id="@+id/textView_NomeStagione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_weight="0"
android:text="TextView"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_NumeroEpisodi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="@color/white" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >

<TextView
android:id="@+id/textView_objectID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:layout_gravity="bottom"
style="@style/ProgressBarStyle" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

申请

    public class MyAppName extends Application {
@Override
public void onCreate() {
super.onCreate();

initImageLoader(getApplicationContext());
}

public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.writeDebugLogs() // Remove for release app
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
}
}

日志猫 http://pastebin.com/JejB8miC

感谢支持

最佳答案

将此 ImageAware 添加到您的项目中:

public class BgImageAware implements ImageAware {

public static final String WARN_CANT_SET_DRAWABLE = "Can't set a drawable into view. You should call ImageLoader on UI thread for it.";
public static final String WARN_CANT_SET_BITMAP = "Can't set a bitmap into view. You should call ImageLoader on UI thread for it.";

protected Reference<View> viewRef;
protected boolean checkActualViewSize;

public BgImageAware(View view) {
this(view, true);
}

public BgImageAware(View view, boolean checkActualViewSize) {
this.viewRef = new WeakReference<View>(view);
this.checkActualViewSize = checkActualViewSize;
}

@Override
public int getWidth() {
View view = viewRef.get();
if (view != null) {
final ViewGroup.LayoutParams params = view.getLayoutParams();
int width = 0;
if (checkActualViewSize && params != null && params.width != ViewGroup.LayoutParams.WRAP_CONTENT) {
width = view.getWidth(); // Get actual image width
}
if (width <= 0 && params != null) width = params.width; // Get layout width parameter
return width;
}
return 0;
}

@Override
public int getHeight() {
View view = viewRef.get();
if (view != null) {
final ViewGroup.LayoutParams params = view.getLayoutParams();
int height = 0;
if (checkActualViewSize && params != null && params.height != ViewGroup.LayoutParams.WRAP_CONTENT) {
height = view.getHeight(); // Get actual image height
}
if (height <= 0 && params != null) height = params.height; // Get layout height parameter
return height;
}
return 0;
}

@Override
public ViewScaleType getScaleType() {
return ViewScaleType.CROP;
}

@Override
public View getWrappedView() {
return viewRef.get();
}

@Override
public boolean isCollected() {
return viewRef.get() == null;
}

@Override
public int getId() {
View view = viewRef.get();
return view == null ? super.hashCode() : view.hashCode();
}

@Override
public boolean setImageDrawable(Drawable drawable) {
if (Looper.myLooper() == Looper.getMainLooper()) {
View view = viewRef.get();
if (view != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
return true;
}
} else {
L.w(WARN_CANT_SET_DRAWABLE);
}
return false;
}

@Override
public boolean setImageBitmap(Bitmap bitmap) {
if (Looper.myLooper() == Looper.getMainLooper()) {
View view = viewRef.get();
if (view != null) {
Drawable drawable = bitmap == null ? null : new BitmapDrawable(view.getResources(), bitmap);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
return true;
}
} else {
L.w(WARN_CANT_SET_BITMAP);
}
return false;
}
}

然后将您的 LinearLayout 包装到其中并传递给 displayImage(...) 方法:

ImageAware imageAware = new BgImageAware(holder.imageView);
imageLoader.displayImage(mStrings[position], imageAware);

关于java - UIL LinearBackground 图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22875437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com