GridView图像缩放会降低Fragment切换时的应用程序速度

时间:2016-07-08 09:38:01

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

我的应用有三个Freagments。其中一个FragmentsGridView用于显示图片。

我在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);

当我不使用它们时,我得到一个几乎平滑的片段滚动像这样

enter image description here

当我使用它们来扩展&amp;适合我的屏幕,我明白了

enter image description here

你可以看到它滞后多少。但在视频教程中,它从未落后。是因为我正在使用Fragment吗?有解决方法吗?

1 个答案:

答案 0 :(得分:0)

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

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

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

相关问题