【问题标题】:Why are my ImageViews being distorted on scrolling down?为什么我的 ImageView 在向下滚动时会变形?
【发布时间】:2015-08-17 17:41:29
【问题描述】:

我面临一个特殊的问题。我有一个音乐流派的网格视图。在每个网格项中,我们可以以级联方式显示最多 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>

请帮忙。

【问题讨论】:

  • 我猜你正面临这个问题,因为图像有不同的分辨率,所以你可以使用 android:scaleType=""
  • getAlbumart 是做什么的?
  • @MidasLefko 它从媒体文件的 ALBUM_ID 获取专辑封面。我在其他视图中使用了完全相同的方法,并得到了完美的结果。
  • 它使用线程还是AsyncTask?如果是这样,那可能会在其他地方工作,并弄乱像 gridview 这样的适配器视图,其中视图被回收......
  • @MidasLefko 不,我不知道如何使用它们。我是新手。不知道 AsyncTask。它只是同一个类中的一个方法。

标签: android android-layout android-imageview android-gridview


【解决方案1】:

我意识到这与 ImageViews 未初始化有关。我注意到在所有的网格项目中,所有 ImageView 都被初始化的项目并没有受到洗牌效果的影响。一旦我使用默认图像初始化所有图像视图,它就可以完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    相关资源
    最近更新 更多