【问题标题】:Android: black screen to load imageAndroid:黑屏加载图像
【发布时间】:2014-06-15 01:34:12
【问题描述】:

我正在尝试在我的应用程序中实现 Universal Image LoaderTouchImageView(带有放大图像的 ViewPager)。但是,当我尝试加载它时,我的屏幕上出现了一个黑色图像

ImagePagerActivity

ExtendedViewPager pager;
TouchImageView imageView;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_fullscreen_view);

    Bundle bundle = getIntent().getExtras();
    assert bundle != null;

    String[] imageUrls = bundle.getStringArray("urls");
    int pagerPosition = bundle.getInt("pos", 0);
    tipo = bundle.getString("tipo");
    monumento = bundle.getString("monumento");
    num = bundle.getInt("num");

    if (savedInstanceState != null) {
        pagerPosition = savedInstanceState.getInt(STATE_POSITION);
    }

    options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.imagen)
        .showImageOnFail(R.drawable.imagen)
        .resetViewBeforeLoading(true)
        .cacheOnDisc(true)
        .imageScaleType(ImageScaleType.EXACTLY)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .considerExifParams(true)
        .displayer(new FadeInBitmapDisplayer(300))
        .build();

    pager = (ExtendedViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(imageUrls));
    pager.setCurrentItem(pagerPosition);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt(STATE_POSITION, pager.getCurrentItem());
}

private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {

        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

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

    @Override
    public Object instantiateItem(ViewGroup view, int position) {

        final View imageLayout = inflater.inflate(R.layout.ampliar_imagen, view, false);
        assert imageLayout != null;

        imageView = (TouchImageView) imageLayout.findViewById(R.id.imagenFullScreen);

        infoImagen = (LinearLayout)imageLayout.findViewById(R.id.textoInfoImagen);
        info = (TextView) imageLayout.findViewById(R.id.textoInfoImagenDesc);

        info.setText("Image");

        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                LinearLayout infoImagen = (LinearLayout)imageLayout.findViewById(R.id.textoInfoImagen);

                if(infoImagen.getVisibility() == View.GONE) {
                    infoImagen.setVisibility(View.VISIBLE);
                } else {
                    infoImagen.setVisibility(View.GONE);
                }
            }
        });

        imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {

            @Override
            public void onLoadingStarted(String imageUri, View view) {
                spinner.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                Modulo.toast(ImagePagerActivity.this, 
                        ImagePagerActivity.this, 
                        "Error al cargar la imagen", 
                        perfil.getColor(), 
                        R.drawable.iconoerrores, 
                        Toast.LENGTH_SHORT);

                spinner.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                spinner.setVisibility(View.GONE);
            }
        });

        view.addView(imageLayout, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

activity_full_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<imagenes.ExtendedViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

imagenfullscreen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/negro" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <imagenes.TouchImageView
        android:id="@+id/imagenFullScreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:adjustViewBounds="true" />

    <ProgressBar
        android:id="@+id/cargandoFoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />
</FrameLayout>

<LinearLayout
    android:id="@+id/textoInfoImagen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/grisTransparente"
    android:orientation="horizontal"
    android:visibility="visible" >

    <TextView
        android:id="@+id/textoInfoImagenDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:paddingBottom="3dp"
        android:paddingTop="3dp"
        android:textColor="@color/blanco"
        android:textSize="13sp" />
</LinearLayout>

</RelativeLayout>

但如果我评论 imageLoader.displayImage... 然后我放了

`int[] images2 = { R.drawable.bus_fondo };
imageView.setImageResource(images2[0]);`

效果很好。

【问题讨论】:

标签: android imageview screen zooming


【解决方案1】:

虽然它与问题无关,但如果有人在使用 Glide 将图像加载到 TouchImageView 时寻找问题,则会显示黑屏。 我找到了一个像这样工作的修复程序。在 Glide 覆盖方法中传递宽度和高度,并在设置位图后调用 TouchImageView 的 setzoom 值为 1。

完整代码如下:

Glide.with('context')
                    .asBitmap()
                    .load('url')
                    .apply(new RequestOptions().override(1600, 1600)) //This is important 
                    .into(new BitmapImageViewTarget('imageview') {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            super.onResourceReady(resource, transition);
                            'imageview'.setImageBitmap(resource);
                            'imageview'.setZoom(1); //This is important
                        }
                    });

【讨论】:

    【解决方案2】:

    因为您在主 UI 线程中从图库中创建加载图片!只是让它异步。 例如,我在加载图片时也出现了黑屏,我的解决方案是制作异步任务:

         public static void onSelectFromGalleryResult(Intent data, Model model, IAddItemView mIVew) {
            mIVew.showProgressDialog();
            new AsynOperation(data,model, mIVew).execute();
        }
    
    
        private static class AsynOperation extends AsyncTask<Void, Void, Bitmap> {
            Intent data;
            Model model;
            IAddItemView mIVew;
    
            AsynOperation (Intent data, Model model,IAddItemView  mIVew){
                this.data = data;
                this.model = model;
                this.mIVew = mIVew;
            }
    
            @Override
            protected Bitmap doInBackground(Void...params) {
    
                String picturePath = "";
                Bitmap bitmap;
                if (data != null) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = model.getApplication().getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    picturePath = cursor.getString(columnIndex);
                    cursor.close();
                }
                bitmap = BitmapFactory.decodeFile(picturePath);         
                ImgBase64.encodeTobase64(bitmap, model);
                return  bitmap ;
    
            }
    
            @Override
            protected void onPostExecute(Bitmap result) {
               mIVew.setImageBitmap(result);
               mIVew.dismissProgressDialog();
            }
    
    
    
            @Override
            protected void onPreExecute() {}
    
            @Override
            protected void onProgressUpdate(Void... values) {}
        }
    
    }
    

    希望,对某人有所帮助!

    【讨论】:

      【解决方案3】:

      试试这个库来加载图片 http://square.github.io/picasso/

      【讨论】:

      • 我要使用 TouchImageView
      猜你喜欢
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 2011-09-23
      • 2021-12-29
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多