【问题标题】:GridView image scaling slows down the app on Fragment switchingGridView 图像缩放会在 Fragment 切换时减慢应用程序的速度
【发布时间】:2016-11-10 19:58:51
【问题描述】:

我有一个包含三个Freagments 的应用程序。其中一个Fragments 有一个GridView 用于显示图像。

我在 YouTube 上关注了this 教程,并相应地完成了那里提到的所有事情。

以下是包含 GridView: 的 XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:padding="10dp"
    android:layout_marginTop="25dp"
    android:orientation="vertical"
    android:scrollbars="vertical"
    android:background="@drawable/new_card_form"
    tools:context=".activities.fragments.NetBankingFragment">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:numColumns="2"
        android:columnWidth="60dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:gravity="center">
    </GridView>


</RelativeLayout>

下面是生成所有图像的 Java 文件 NetBankingFragment.java

import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Telephony;
import android.support.v4.app.Fragment;
import android.support.v4.content.res.ResourcesCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;

import org.json.JSONArray;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link NetBankingFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link NetBankingFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NetBankingFragment extends Fragment implements FormFragmentBehaviour {
    private int index;

    private View FragView;
    private GridView gridView;
    private Integer[] photos = {R.drawable.sbilogo, R.drawable.axisbank, R.drawable.citilogo, R.drawable.hdfclogo};



    private OnFragmentInteractionListener mListener;

    /**
     * Factory for our Fragment.
     * */
    public static NetBankingFragment newInstance(int index) {
        Bundle fragmentArgs = new Bundle();
        fragmentArgs.putInt("index", index);

        NetBankingFragment fragment = new NetBankingFragment();
        fragment.setArguments(fragmentArgs);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.index = getArguments().getInt("index");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        FragView = inflater.inflate(R.layout.fragment_net_banking, container, false);
        gridView = (GridView) FragView.findViewById(R.id.gridView);
        gridView.setAdapter(new ImageAdapter(FragView.getContext()));

        return FragView;




    }



    public void setgray(ImageView imageView)
    {
        final ColorMatrix gray = new ColorMatrix();
        gray.setSaturation(0);

        final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(gray);
        imageView.setColorFilter(filter);
    }





    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public boolean validate() {
        return false;
    }

    @Override
    public void clearErrors() {

    }

    /**
     * This will show the various banks that are available after fetching from the EC backend.s
     * */
    public void showBanks(JSONArray banks) {

    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    //To Generate the buttons for Banks
    public void grayout()
    {
        //setgray(sbilogo);
        //setgray(axislogo);
        //setgray(citilogo);
        //setgray(hdfclogo);
    }


    public void setParticularGray(ImageView imgView)
    {
        grayout();

        final ColorMatrix color = new ColorMatrix();
        color.setSaturation(1);

        final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(color);
        imgView.setColorFilter(filter);
    }

    public class ImageAdapter extends BaseAdapter implements Runnable{

        private Context mContext;

        public ImageAdapter(Context context)
        {
            mContext = context;
        }


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

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

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

            ImageView imgView = new ImageView(mContext);
            imgView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imgView.setPadding(5,5,5,5);
            imgView.setImageResource(photos[position]);

            return imgView;


        }

        @Override
        public void run() {

        }
    }
}

问题出在只有两行代码

imgView.setLayoutParams(new GridView.LayoutParams(100, 100));
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);

当我不使用它们时,我得到了一个几乎平滑的 Fragment 滚动

但是当我使用它们缩放并适合我的屏幕时,我明白了

您可以看到它的滞后程度。但在视频教程中,它从不落后。是因为我使用的是Fragment吗?有解决办法吗?

【问题讨论】:

  • 它是否在每一帧滚动时调用这些行?我会在初始化后将它们存储为最终值,而不是重新计算每一帧?我可能是错的,只是一个想法。
  • 我该怎么做?我对 Android 很陌生

标签: android performance android-fragments gridview memory-management


【解决方案1】:

根据文档,ListView 重用视图以提高性能。您所做的是,将为每个项目创建一个图像视图,而不是重复使用它。

更好的解决方案是使用 ViewHolder 模式。示例:Optimizing your ListView with ViewHolder pattern

同时保持您的图像文件较小。并考虑使用图像加载库以获得最佳性能。

【讨论】:

  • 我的图片不超过 300KB 并且应用变慢了。我见过运行 2MB 大图像的应用程序非常流畅
  • 您建议的任何图像加载库??
  • 取决于您的用例,Picasso 或 Glide。还有几个图书馆在那里。也请搜索它们。
猜你喜欢
  • 2014-09-28
  • 2011-08-02
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多