繁体   English   中英

为什么我的ImageViews在向下滚动时会变形?

[英]Why are my ImageViews being distorted on scrolling down?

我正面临一个特殊的问题。 我对音乐流派有一个gridview。 在每个网格项目中,我们最多可以级联方式显示3张缩略图(类似于Google Play音乐的流派)。 但是,当我向下滚动页面并向后滚动时,我发现不同网格项中的所有图像都随机打乱了。

适当的布局http://i.stack.imgur.com/yc06v.png

扭曲的布局(随机图片) http://i.stack.imgur.com/DxoMC.png

这是处理图像视图的代码段:

String[] al_id ={MediaStore.Audio.Albums.ALBUM_ID};
    String orderby = MediaStore.Audio.Albums.ALBUM_ID;
    Cursor albumcursor = context.getContentResolver().query(uri, al_id,null, null, orderby);
    Set<Long> set  = new LinkedHashSet<Long>();


    if(tempcursor.moveToFirst()){

        do{
            long id = tempcursor.getLong(tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
            set.add(id);
        }while(tempcursor.moveToNext());

    }


    if(set.size()==1 ){
        Iterator<Long> it = set.iterator();

        long id = it.next();  
        ImageView albumArt = (ImageView) view.findViewById(R.id.albumart_zero);
        Bitmap img = getAlbumart(context, id);
        if(img != null)
            albumArt.setImageBitmap(img);
        else{
            Bitmap def = getDefaultAlbumArt(context);
            albumArt.setImageBitmap(def);

        }
    }
    else if(set.size()==2){
        Iterator<Long> it = set.iterator();
        long id = it.next();
        ImageView albumArt1 = (ImageView) view.findViewById(R.id.albumart_one);
        albumArt1.setScaleType(ImageView.ScaleType.FIT_XY);
        Bitmap img = getAlbumart(context, id);
        if(img != null)
            albumArt1.setImageBitmap(img);
        else{
            Bitmap def = getDefaultAlbumArt(context);
            albumArt1.setImageBitmap(def);

        }
        long id2 = it.next();
        ImageView albumArt2 = (ImageView) view.findViewById(R.id.albumart_two);
        albumArt2.setScaleType(ImageView.ScaleType.FIT_XY);
        Bitmap img2 = getAlbumart(context, id2);
        if(img2 != null)
            albumArt2.setImageBitmap(img2);
        else{
            Bitmap def = getDefaultAlbumArt(context);
            albumArt2.setImageBitmap(def);

        }
    }
    else if(set.size()>=3){
        Iterator<Long> it = set.iterator();

        long id = it.next();
        ImageView albumArt1 = (ImageView) view.findViewById(R.id.albumart_one);
        albumArt1.setScaleType(ImageView.ScaleType.FIT_XY);
        Bitmap img = getAlbumart(context, id);
        if(img != null)
            albumArt1.setImageBitmap(img);
        else{
            Bitmap def = getDefaultAlbumArt(context);
            albumArt1.setImageBitmap(def);

        }

        long id2 = it.next();
        ImageView albumArt2 = (ImageView) view.findViewById(R.id.albumart_two);
        albumArt2.setScaleType(ImageView.ScaleType.FIT_XY);
        Bitmap img2 = getAlbumart(context, id2);
        if(img2 != null)
            albumArt2.setImageBitmap(img2);
        else{
            Bitmap def = getDefaultAlbumArt(context);
            albumArt2.setImageBitmap(def);

        }

        long id3 = it.next();
        ImageView albumArt3 = (ImageView) view.findViewById(R.id.albumart_three);
        albumArt3.setScaleType(ImageView.ScaleType.FIT_XY);
        Bitmap img3 = getAlbumart(context, id3);
        if(img3 != null)
            albumArt3.setImageBitmap(img3);
        else{
            Bitmap def = getDefaultAlbumArt(context);
            albumArt3.setImageBitmap(def);

        }
    }

这是网格项目的布局:

<LinearLayout
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/metalList"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:elevation="4dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:clipToPadding="false"
    android:background="#E9E0D5">
    <ImageView
        android:id="@+id/albumart_zero"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:scaleType="centerCrop"
        android:layout_alignParentLeft="true"
        android:elevation="0dp"
        />

    <ImageView
        android:id="@+id/albumart_two"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="top"
        android:layout_alignParentRight="true"
        android:layout_margin="0dp"
        android:elevation="2dp"
        />
    <ImageView
        android:id="@+id/albumart_three"
        android:layout_width="80dp"
        android:layout_height="800dp"
        android:layout_gravity="top"
        android:layout_marginLeft="40dp"
        android:elevation="4dp"
        />
    <ImageView
        android:id="@+id/albumart_one"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="top"
        android:layout_margin="0dp"
        android:layout_alignParentLeft="true"
        android:elevation="10dp"
        />
</RelativeLayout>
<TextView
    android:id="@+id/genre_name"
    android:layout_height="25dp"
    android:layout_width="match_parent"
    android:textColor="@color/metalTextB"
    android:layout_marginTop="3dp"
    android:layout_marginLeft="10dp"
    android:textSize="16sp"
    android:typeface="sans"
    android:text="Genre"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:maxLines="1"/>
<TextView
    android:id="@+id/genre_count"
    android:layout_width="match_parent"
    android:layout_height="15dp"
    android:layout_below="@id/genre_name"
    android:layout_marginLeft="15dp"
    android:layout_marginBottom="3dp"
    android:textColor="@color/metalTextS"
    android:textSize="13sp"
    android:text="artist"
    android:typeface="sans"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:maxLines="1"/>
</LinearLayout>

请帮忙。

我意识到,这与未初始化的ImageView有关。 我注意到,在所有网格项目中,初始化所有ImageView的网格项目都不受改组效果的影响。 用默认图像初始化所有图像视图后,它就可以完美工作。

暂无
暂无

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

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