我有一个带有此项目布局的GridView:
<?xml version="1.0" encoding="utf-8"?>
<include
android:id="@+id/grid_item_library_cover"
layout="@layout/view_document_cover" />
<include
android:id="@+id/grid_item_library_cover_lbw"
layout="@layout/view_document_cover_lbw"
android:visibility="gone" />
<include
android:id="@+id/grid_item_library_cover_journal"
layout="@layout/view_document_cover_journal"
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_small"
android:orientation="horizontal" >
<ImageView
android:id="@+id/grid_item_library_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="@dimen/margin_small"
android:src="@drawable/btn_info_blue" />
</LinearLayout>
view_document_cover.xml
<ImageView
android:id="@+id/document_cover_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/bg_cover_normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/document_cover_bg"
android:layout_alignLeft="@+id/document_cover_bg"
android:layout_alignRight="@+id/document_cover_bg"
android:layout_alignTop="@+id/document_cover_bg"
android:orientation="horizontal" >
<Space
android:id="@+id/document_cover_space_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="280"
android:orientation="vertical" >
<Space
android:id="@+id/document_cover_space_top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11" />
<ImageView
android:id="@+id/document_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="389"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</LinearLayout>
<Space
android:id="@+id/document_cover_space_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10" />
</LinearLayout>
<ImageView
android:id="@+id/document_cover_fg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/fg_cover_normal" />
view_document_cover_lbw.xml
<ImageView
android:id="@+id/document_cover_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/bg_cover_lbw" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/document_cover_bg"
android:layout_alignLeft="@+id/document_cover_bg"
android:layout_alignRight="@+id/document_cover_bg"
android:layout_alignTop="@+id/document_cover_bg"
android:orientation="horizontal" >
<Space
android:id="@+id/document_cover_space_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="280"
android:orientation="vertical" >
<Space
android:id="@+id/document_cover_space_top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15" />
<ImageView
android:id="@+id/document_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="385"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</LinearLayout>
<Space
android:id="@+id/document_cover_space_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="26" />
</LinearLayout>
<ImageView
android:id="@+id/document_cover_fg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/fg_cover_lbw" />
view_document_cover_journal.xml
<ImageView
android:id="@+id/document_cover_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/bg_cover_normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/document_cover_bg"
android:layout_alignLeft="@+id/document_cover_bg"
android:layout_alignRight="@+id/document_cover_bg"
android:layout_alignTop="@+id/document_cover_bg"
android:orientation="horizontal" >
<Space
android:id="@+id/document_cover_space_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="280"
android:orientation="vertical" >
<Space
android:id="@+id/document_cover_space_top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11" />
<ImageView
android:id="@+id/document_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="389"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</LinearLayout>
<Space
android:id="@+id/document_cover_space_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10" />
</LinearLayout>
<ImageView
android:id="@+id/document_cover_fg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/fg_cover_normal" />
我知道使用嵌套权重很糟糕,但需要进一步阅读。
这是我的getView()
方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Document item = (Document) getItem(position);
if (convertView == null)
convertView = mInflater.inflate(RESOURCE_ID, parent, false);
View normalCover = ViewHolder.get(convertView,
R.id.grid_item_library_cover);
View lbwCover = ViewHolder.get(convertView,
R.id.grid_item_library_cover_lbw);
View journalCover = ViewHolder.get(convertView,
R.id.grid_item_library_cover_journal);
ImageView coverView;
switch (item.getDocumentType()) {
case 1:
lbwCover.setVisibility(View.VISIBLE);
normalCover.setVisibility(View.GONE);
journalCover.setVisibility(View.GONE);
coverView = ViewHolder.get(lbwCover, R.id.document_cover);
break;
case 3:
lbwCover.setVisibility(View.GONE);
normalCover.setVisibility(View.GONE);
journalCover.setVisibility(View.VISIBLE);
coverView = ViewHolder.get(journalCover, R.id.document_cover);
break;
default:
lbwCover.setVisibility(View.GONE);
normalCover.setVisibility(View.VISIBLE);
journalCover.setVisibility(View.GONE);
coverView = ViewHolder.get(normalCover, R.id.document_cover);
}
if (item.isCoverDownloaded()) {
ImageManager.displayCover(
"file://" + mContext.getExternalFilesDir(null)
+ File.separator + "documents" + File.separator
+ item.getDocumentId() + File.separator
+ "cover.png", coverView, item.getDocumentType());
}
return convertView;
}
我正在使用Universal Image Loader从文件加载图像。这是设置:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context).defaultDisplayImageOptions(options).build();
public static void displayCover(String uri, ImageView view, int type) {
DisplayImageOptions.Builder optionsBuilder = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(true).cacheInMemory(true)
.displayer(new FadeInBitmapDisplayer(500, true, true, false))
.delayBeforeLoading(200);
switch (type) {
case 1:
optionsBuilder.preProcessor(new MaskedPreProcessor(
R.drawable.mask_cover_lbw));
optionsBuilder.showImageOnLoading(R.drawable.mask_cover_lbw);
break;
case 3:
optionsBuilder.preProcessor(new MaskedPreProcessor(
R.drawable.mask_cover_journal));
optionsBuilder.showImageOnLoading(R.drawable.mask_cover_journal);
break;
default:
optionsBuilder.preProcessor(new MaskedPreProcessor(
R.drawable.mask_cover_normal));
optionsBuilder.showImageOnLoading(R.drawable.mask_cover_normal);
}
sLoader.displayImage(uri, view, optionsBuilder.build());
}
我需要预处理位图,因为我需要在其上应用alpha掩码。
问题是它在摩托罗拉XOOM上运行正常,但如果在GridView
中显示任何图像,则滚动时三星Galaxy S4(i9505,Android 4.3)难以置信。即使我删除嵌套的权重并仅使用顶级加权LinearLayout,它也会滞后。即使我在xml中设置Image而不是通过UIL,它也会滞后。但如果没有图像 - 它会顺利滚动。同样使用UIL我经常收到警告:
02-25 15:39:55.280:W / OpenGLRenderer(23029):位图太大而无法上传到纹理中(4608x321,max = 4096x4096)
虽然我的图像是372 * 528像素。
我使用嵌套权重因为我需要将我的图像相对于布局中的其他两个图像独立地定位到像素密度和分辨率。虽然我在GridView
中有固定的列数(它设置为3)但我不能在这里使用dp值,因为图像大小因设备而异。我需要在任何一个上都有完全相同的布局。
那我怎么能实现呢?