简体   繁体   English

在片段上加载图像,Android Studio

[英]Load image on fragment, android studio

I'm a very beginner as android developer and I wanted to build a mockup app for my assignment. 我是Android开发人员的初学者,我想为我的任务构建一个样机应用程序。 I was able to program the app and get it working, but when I installed it on my phone, it crashes everytime I want to load a new section/fragment. 我能够对该应用进行编程并使其正常运行,但是当我将其安装在手机上时,每次要加载新的片段/片段时它都会崩溃。

I conclude that the crash is due to excessive uses of image from drawable folder , which causes the crash due to the high resource requirement. 我得出的结论是,崩溃是由于过多使用了drawable文件夹中的图像而导致的,这是由于对资源的高要求导致的。 so I needed to load image using assets folder instead. 所以我需要使用Assets文件夹加载图像。

But until now, I haven't figured out a way to load image from assets folder on fragment. 但是直到现在,我还没有找到一种从片段上的Assets文件夹加载图像的方法。 findViewById, inputstream , all resulted in an unknown method error. findViewById,inputstream均导致未知方法错误。

Anyone's help would be gladly appreciated. 任何人的帮助将不胜感激。

Source code and APK can be found here: https://drive.google.com/drive/folders/0B1_AQxgFWCzuT0Vxdkx0cmxQMG8?usp=sharing 可以在以下位置找到源代码和APK: https //drive.google.com/drive/folders/0B1_AQxgFWCzuT0Vxdkx0cmxQMG8?usp = sharing

If you're loading large images into ImageViews, you should be using an image loading library. 如果要将大型图像加载到ImageViews中,则应使用图像加载库。 I would recommend Glide: https://github.com/bumptech/glide (there are instructions on how to include Glide in your project, and how to use it on the Github page). 我会推荐Glide: https : //github.com/bumptech/glide (在Github页面上有关于如何在项目中包含Glide以及如何使用它的说明)。

Loading your images in this way should solve the issue of poor performance, however you should also be careful about storing lots of high resolution images in your project, as it can increase the size of your app by a lot. 以这种方式加载图像应该可以解决性能不佳的问题,但是,在项目中存储大量高分辨率图像时也应格外小心,因为这可能会增加应用程序的大小。

In order to use findViewById in a fragment you should use the onCreateView method like so (I'm using an ImageView in my example): 为了在片段中使用findViewById ,您应该像这样使用onCreateView方法(我在示例中使用ImageView):

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_layout, container, false);

    ImageView imageView = (ImageView) view.findViewById(R.id.image_view); 

    return view;
}

As far as I am aware, there would be no advantage to using the assets folder over the drawable folder. 据我所知,使用资产文件夹而不是可绘制文件夹是没有优势的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM