UI重载+动画

时间:2013-01-15 14:15:07

标签: android listview animation user-interface adapter

所以,我正在做一个应该像这张照片的应用程序:

infocardapp

它有一个列表视图,用户选择要显示的内容,以及选择内容时将动画(滑入/滑出)的内容区域。

内容区域必须以dinamically方式构建,其布局根据JSON请求中包含的信息而变化,它由一个巨大的列表视图组成,每个项目都是一个信息部分(见图片)。

问题是动画内容区域,而在UI线程中进行大量视图加载会导致动画中的大量延迟和闪烁,并且在UI线程以外的线程中无法进行视图加载。

我尝试在适配器中使用AsyncTask来在后台完成一些UI加载,但只有ViewHolder模式实例化和findviewById调用才能进入,所以我没有获得大的性能提升。实际问题是.setText().setImageBitmap()来电。

最后真正的问题是:为了获得流畅的动画效果,我可以做些什么才能在此UI加载中获得更好的性能?

注意: 考虑用于填充内容区域的数据正在进行硬编码以进行测试。


修改 我的内容区域适配器中使用的代码:

public class InfoAdapter extends ArrayAdapter<Info> {

    private final LayoutInflater layoutInflater;
    private final Typeface typeface;

    public InfoAdapter(final Context context, final List<Info> list) {
        super(context, 0, list);
        this.layoutInflater = LayoutInflater.from(context);
        this.typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"
                + Constants.DEFAULT_FONTNAME);
    }

    @Override
    public View getView(final int position, final View convertView,
            final ViewGroup parent) {

        View infoView = convertView;

        ViewHolder holder;
        if (infoView == null) {
            infoView = (RelativeLayout) layoutInflater.inflate(getItem(position).getLayout(), null);
            holder = new ViewHolder(
                    (TextView) infoView.findViewById(R.id.highlight_title),
                    (TextView) infoView.findViewById(R.id.info_title),
                    (TextView) infoView.findViewById(R.id.info_extra_info01),
                    (TextView) infoView.findViewById(R.id.info_extra_info02),
                    (TextView) infoView.findViewById(R.id.info_description),
                    (ImageView) infoView.findViewById(R.id.info_image),
                    (TextView) infoView.findViewById(R.id.info_subtitle),
                    (TextView) infoView.findViewById(R.id.info_subdescription),
                    (ImageView) infoView.findViewById(R.id.info_subimage));
            infoView.setTag(holder);
        } else {
            holder = (ViewHolder) infoView.getTag();
        }

        final String extraInfo01 = String.valueOf((int) (getItem(position).getValue() / 1));
        final String extraInfo02 = String.valueOf((int) ((getItem(position).getValue() % 1) * 100));

        setDataInView(holder.highlight, getItem(position).getFavorite());

        holder.title.setText(getItem(position).getName());
        holder.title.setTypeface(typeface);

        holder.extraInfo01.setText(extraInfo01);
        holder.extraInfo01.setTypeface(typeface);

        holder.extraInfo02.setText("," + extraInfo02);
        holder.extraInfo02.setTypeface(typeface);

        holder.description.setText(getItem(position).getDescription());
        holder.description.setTypeface(typeface);


        if (holder.image != null) {
            if (getItem(position).getImage() == 0) {
                holder.image.setVisibility(View.GONE);
            } else {
                holder.image.setVisibility(View.VISIBLE);
                holder.image.setImageResource(getItem(position).getImage());
            }
        }


        setDataInView(holder.subTitle, getItem(position).getHistoryTitle());
        setDataInView(holder.subDescription, getItem(position).getHistory());

        if (holder.subImage != null) {
            if (getItem(position).getUri() == null) {
                holder.subImage.setVisibility(View.GONE);
            } else {
                final Drawable subDrawable = Drawable.createFromPath(getItem(
                        position).getUri());
                holder.subImage.setVisibility(View.VISIBLE);
                holder.subImage.setImageDrawable(subDrawable);
            }
        }

        return infoView;
    }

    private void setDataInView(final TextView textView, final String data) {
        if (textView != null) {
            if (data.isEmpty()) {
                textView.setVisibility(View.GONE);
            } else {
                textView.setVisibility(View.VISIBLE);
                textView.setTypeface(typeface);
                textView.setText(data);
            }
        }
    }

    private static class ViewHolder {
        private final TextView highlight;
        private final TextView title;
        private final TextView extraInfo01;
        private final TextView extraInfo02;
        private final TextView description;
        private final ImageView image;
        private final TextView subTitle;
        private final TextView subDescription;
        private final ImageView subImage;

        public ViewHolder(final TextView highlight, final TextView title,
                final TextView extraInfo01, final TextView extraInfo02,
                final TextView description, final ImageView image,
                final TextView subTitle, final TextView subDescription,
                final ImageView subImage) {
            this.highlight = highlight;
            this.title = title;
            this.extraInfo01 = extraInfo01;
            this.extraInfo02 = extraInfo02;
            this.description = description;
            this.image = image;
            this.subTitle = subTitle;
            this.subDescription = subDescription;
            this.subImage = subImage;
        }

    }

}

1 个答案:

答案 0 :(得分:2)

我认为这一行可能是一个问题:

holder.image.setImageResource(getItem(position).getImage());

查看文档中有关setImageResource的内容:

  

这会在UI线程上进行Bitmap读取和解码,这会导致延迟打嗝。如果这是一个问题,请考虑使用setImageDrawable(android.graphics.drawable.Drawable)或setImageBitmap(android.graphics.Bitmap)和BitmapFactory。

对于延迟加载图片,您应该检查this link。您应该更改声明public void download(String url, ImageView imageView)

public void download(int id, ImageView imageView)

id

getItem(position).getImage()