什么是在TypedArray使用再循环()方法 [英] what is the use of recycle() method in TypedArray

查看:171
本文介绍了什么是在TypedArray使用再循环()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个GalleryView和ImageView的,其显示的图像越大的项目点击在画廊的时候。我已经使用了低于code来实现ImageAdapter:

I have created a GalleryView and ImageView which displays the Image bigger when an item is clicked in the gallery. I have used the below code to implement the ImageAdapter:

public ImageAdapter(Context c)
{
    context = c;
    TypedArray a = obtainStyledAttributes(R.styleable.gallery1);
    itemBackground = a.getResourceId(R.styleable.gallery1_android_galleryItemBackground, 0);    
    a.recycle();    
}

当我删除的声明 a.recycle()没有任何改变和应用程序正常运行和以前一样,但我到处看,这是强制性的,以回收typedArray。当在我的应用程序运行的方式没有改变什么用的再循环()方法。

When I removed the statement a.recycle() there is no change and the app is running normally as before, but everywhere I read that it is compulsory to recycle the typedArray. When there is no change in the way my app is running what is the use of the recycle() method.

任何人能请解释一下我详细。谢谢你。

Can anyone please explain me in detail. Thank you.

推荐答案

关键是类似于C语言清除指针的想法(如果你熟悉)。它是用来使与一个准备好垃圾收集相关的数据,以便存储/数据不低效绑定到一个时,它并不需要如此。了解更多这里。重要的是要注意,这是不是真的有必要,除非你实际上重用一个。 GC会自动清理这些数据,你如果对象不被再次使用。之所以TypedArray是不同的,但是,是因为一个TypedArray具有购买重用必须返回其它内部数据(称为StyledAttributes)到TypedArray。阅读关于<一个href="http://developer.android.com/reference/android/content/res/TypedArray.html#recycle%28%29">here.

The point is similar to the idea of clearing a pointer in a C-language (if you're familiar with that). It is used to make the data associated with "a" ready for garbage collection so memory/data is not inefficiently bound to "a" when it doesn't need to be. Read more here. It's important to note that this isn't really necessary unless you're actually reusing "a". GC should automatically clear up this data for you if the object is not used again. The reason why a TypedArray is different, however, is because a TypedArray has other internal data that must be returned (known as StyledAttributes) to the TypedArray for later reuse. Read about that here.

这篇关于什么是在TypedArray使用再循环()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆