片段切换会降低Android中的应用速度

时间:2016-07-07 13:50:03

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

我正在开发一款Android应用程序,该应用程序有三种Fragments支付三种付款方式,即 CardFragment NetBankingFragment & WalletFragment

NetBankingFragment 中,我有多个ImageViews,基本上代表不同的银行。

以下是代表网上银行用户界面的 fragment_nb_view 的XML代码。

<ScrollView
    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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_nb_button"
        android:padding="5dp"
        android:gravity="center">


        <TableRow
            android:padding="5dp"
            android:gravity="center"
            android:layout_marginBottom="10dp">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:id="@+id/sbiButton"
                android:src="@drawable/sbilogo"
                android:layout_marginRight="10dp"/>

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:id="@+id/axisButton"
                android:src="@drawable/axisbank"/>

        </TableRow>


        <TableRow
            android:padding="5dp"
            android:gravity="center"
            android:layout_marginBottom="10dp">

         <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:id="@+id/citiButton"
                android:src="@drawable/citilogo"
                android:layout_marginRight="10dp"/>


            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:id="@+id/hdfcButton"
                android:src="@drawable/hdfclogo"/>
        </TableRow>
    </TableLayout>
    </RelativeLayout>

</ScrollView>

关注我的 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.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.Button;
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 int NUM_COL = 2;
    private int NUM_ROW = 3;

    private Button btn;

    private ImageView sbilogo, axislogo, citilogo, hdfclogo;

    private View FragView;

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

        init();
        grayout();

        sbilogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                setParticularGray((ImageView) v);
            }
        });

        axislogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                setParticularGray((ImageView) v);
            }
        });

        citilogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                setParticularGray((ImageView) v);
            }
        });

       hdfclogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                setParticularGray((ImageView) v);
            }
        });




        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 init() {
        sbilogo = (ImageView) FragView.findViewById(R.id.sbiButton);
        axislogo = (ImageView) FragView.findViewById(R.id.axisButton);
        citilogo = (ImageView) FragView.findViewById(R.id.citiButton);
        hdfclogo = (ImageView) FragView.findViewById(R.id.hdfcButton);
    }


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

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

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

现在我不知道问题是什么,但是当我添加更多ImageViews时,从一个片段切换到另一个片段变得非常慢。

例如,

以下GIF显示从一个Fragment到另一个

的几乎平滑切换

enter image description here

但是当我再添加两个ImageViews时,它变得极其缓慢

enter image description here

我不知道是不是因为我使用的图片大小(他们有点大)或其他东西,但我得到了我第一次尝试滑动时在logcat上关注消息

07-07 19:00:30.087 3453-3453/payment.ec.juspay.in.demoapp I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
07-07 19:00:30.399 3453-3453/payment.ec.juspay.in.demoapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9ac1206 time:39534547
07-07 19:00:30.399 3453-3453/payment.ec.juspay.in.demoapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@40c0291 time:39534547
07-07 19:00:35.423 3453-3453/payment.ec.juspay.in.demoapp I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
07-07 19:12:47.792 3453-3453/payment.ec.juspay.in.demoapp I/art: Explicit concurrent mark sweep GC freed 9617(452KB) AllocSpace objects, 0(0B) LOS objects, 8% free, 82MB/90MB, paused 1.928ms total 78.116ms

似乎表明 mainthread 本身在处理过程中做得太多,但我不知道该怎么做。

请帮忙。

0 个答案:

没有答案