如何传递列表列表意味着列表<列表<Bitmap>>通过捆绑到Android中的片段



基本上我处理片段,并通过创建构造函数将列表传递给片段,但是当我尝试生成签名的apk文件时,我得到了这样的错误(这个片段应该有一个默认的构造函数)搜索后,我找到了创建 newInstance 的方法,以将数据按捆绑包传递给另一个片段喜欢这个。。。

public static Fragment newInstance(String arg1, int arg2) {
    Fragment result = new MyFragment();
    Bundle args = new Bundle();
    args.putString("arg1_key", arg1);
    args.putInt("arg2_key", arg2);
    result.setArguments(args); 
    return result;
}

我的代码在这里,请提出任何建议......

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.design.internal.ParcelableSparseArray;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.google.android.gms.ads.AdView;
import com.nhaarman.listviewanimations.appearance.simple.AlphaInAnimationAdapter;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by waqar on 12/01/2017.
 */
public class splashFragment extends Fragment {
    private int[] HandImages = {R.drawable.hands_1, R.drawable.hands_2, R.drawable.hands_3, R.drawable.hands_4, R.drawable.hands_5,
            R.drawable.hands_6, R.drawable.hands_7, R.drawable.hands_8, R.drawable.hands_9, R.drawable.hands_10,
            R.drawable.hands_11, R.drawable.hands_12, R.drawable.hands_13, R.drawable.hands_14, R.drawable.hands_15,
            R.drawable.foot_15};

    private int[] FootImages = {R.drawable.foot_1, R.drawable.foot_2, R.drawable.foot_3, R.drawable.foot_4, R.drawable.foot_5,
            R.drawable.foot_6, R.drawable.foot_7, R.drawable.foot_8, R.drawable.foot_9, R.drawable.foot_10,
            R.drawable.foot_11, R.drawable.foot_12, R.drawable.foot_13, R.drawable.foot_14,
            R.drawable.foot_15};

    private int[] TatooImages = {R.drawable.tatoos_1, R.drawable.tatoos_2, R.drawable.tatoos_3, R.drawable.tatoos_4, R.drawable.tatoos_5,
            R.drawable.tatoos_6, R.drawable.tatoos_7, R.drawable.tatoos_8, R.drawable.tatoos_9, R.drawable.tatoos_10, R.drawable.tatoos_11};

    private int[] NailImages = {R.drawable.nail_1, R.drawable.nail_2, R.drawable.nail_3, R.drawable.nail_4, R.drawable.nail_5,
            R.drawable.nail_6, R.drawable.nail_7, R.drawable.nail_8, R.drawable.nail_9, R.drawable.nail_10, R.drawable.nail_11};

    ArrayList<Bitmap> HandsBitmapArray = new ArrayList<Bitmap>();
     ArrayList<Bitmap> FootBitmapArray = new ArrayList<Bitmap>();
    ArrayList<Bitmap> TatooBitmapArray = new ArrayList<Bitmap>();
    ArrayList<Bitmap> NailBitmapArray = new ArrayList<Bitmap>();
    ImageView imageView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.splash_fragment, container, false);
        imageView = (ImageView) view.findViewById(R.id.imageView);

        Picasso.with(getActivity()).load(R.drawable.design_12).transform(new CircleTransform()).into(imageView);
        final AdView mAdView = (AdView) getActivity().findViewById(R.id.adView1);
        mAdView.setVisibility(View.GONE);
        if (HandImages != null && HandImages.length > 0) {
            new BackGroundTask().execute();
        }
        return view;
    }
    public class CircleTransform implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());
            float radius = size / 2f;
            final Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
            Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            canvas.drawCircle(radius, radius, radius, paint);
            if (source != output) {
                source.recycle();
            }
            return output;
        }
        @Override
        public String key() {
            return "circle";
        }
    }

    public class BackGroundTask extends AsyncTask<Object, Object, List<List<Bitmap>>> {
        ProgressDialog progressDialog;
        List<List<Bitmap>> arrayOfLists = new ArrayList<List<Bitmap>>();
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            /*progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Loading....");
            progressDialog.setCancelable(false);
            progressDialog.show();*/
        }
        /*@Override
        protected void onProgressUpdate(Integer... values) {
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(values[0]);
        }*/

        @Override
        protected List<List<Bitmap>> doInBackground(Object... params) {
            if (isAdded() == true) {
                for (int i = 0; i < HandImages.length; i++) {
                    Bitmap handsImaes = BitmapFactory.decodeResource(getActivity().getResources(), HandImages[i]);
                   /* int bytes = byteSizeOf(largeIcon);
                    ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
                    largeIcon.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
                    byte[] array = buffer.array();*/
                  /*  Bitmap b1 = BitmapFactor y.decodeByteArray(b, 0, b.length);
                    Bitmap bitmap = Bitmap.createScaledBitmap(b1, 120, 120, false);*/

                    //Bitmap bitmap = Bitmap.createScaledBitmap(largeIcon, 80, 80, true);
                    Bitmap HandBitmaps = getResizedBitmap(handsImaes, 80, 80);
                    //Bitmap bitmap = Bitmap.createScaledBitmap(largeIcon,(int)(largeIcon.getWidth()*0.1), (int)(largeIcon.getHeight()*0.1), true);
                    //Bitmap bitmap = scaleBitmap(largeIcon, 80, 80);
                   // Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), images[i], 80, 80);
                    HandsBitmapArray.add(HandBitmaps);

                }
                arrayOfLists.add(HandsBitmapArray);

                for (int i = 0; i < FootImages.length; i++){
                    Bitmap footImages = BitmapFactory.decodeResource(getActivity().getResources(), FootImages[i]);
                    Bitmap FootBitmaps = getResizedBitmap(footImages, 80, 80);
                    FootBitmapArray.add(FootBitmaps);
                }
                arrayOfLists.add(FootBitmapArray);
                for (int i = 0; i < TatooImages.length; i++){
                    Bitmap footImages = BitmapFactory.decodeResource(getActivity().getResources(), TatooImages[i]);
                    Bitmap FootBitmaps = getResizedBitmap(footImages, 80, 80);
                    TatooBitmapArray .add(FootBitmaps);
                }
                arrayOfLists.add(TatooBitmapArray);
                for (int i = 0; i < NailImages.length; i++){
                    Bitmap footImages = BitmapFactory.decodeResource(getActivity().getResources(), NailImages[i]);
                    Bitmap FootBitmaps = getResizedBitmap(footImages, 80, 80);
                    NailBitmapArray .add(FootBitmaps);
                }
                arrayOfLists.add(NailBitmapArray);
            }
            return arrayOfLists;
        }

        @Override
        protected void onPostExecute(final List<List<Bitmap>> result) {
            super.onPostExecute(result);

            //progressDialog.dismiss();
            /*new CountDownTimer(2000, 800) {
                public void onTick(long millisUntilFinished) {
                }
                public void onFinish() {*/
                    //MainFragment mainFragment = newInstance(result);
                    MainFragment mainFragment = new MainFragment(result);
                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
                    fragmentTransaction.replace(R.id.frame_lay, mainFragment, "mainFragment");
                    fragmentTransaction.commit();
                /*}
            }.start();*/
            /*CustomAdapter adapter = new CustomAdapter(getActivity(), R.layout.custom_grid_row, result);
            AlphaInAnimationAdapter swingRightInAnimationAdapter = new AlphaInAnimationAdapter(adapter);
            swingRightInAnimationAdapter.setAbsListView(grid);
            grid.setAdapter(swingRightInAnimationAdapter);*/
        }
    }

    public static MainFragment newInstance(List<List<Bitmap>> bitmapArray)
    {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("array", bitmapArray);
        fragment.setArguments(bundle);
        return fragment;
    }

    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
       /* BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inSampleSize = 6;*/
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);
        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
                bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }
    @Override
    public void onResume() {
        super.onResume();
        ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
    }
    @Override
    public void onStop() {
        super.onStop();
        ((AppCompatActivity)getActivity()).getSupportActionBar().show();
    }

}

而我在这个函数中的错误......

public static MainFragment newInstance(List<List<Bitmap>> bitmapArray)
    {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("array", bitmapArray);
        fragment.setArguments(bundle);
        return fragment;
    }

编辑我喜欢的getterSetter类...

public class GetterSetterForBitmaps {
    public List<List<Bitmap>> HandsList;
    public List<List<Bitmap>> FootsList;
    public List<List<Bitmap>> TatoosLists;
    public List<List<Bitmap>> NailsLists;
    public List<List<Bitmap>> getHandsList() {
        return HandsList;
    }
    public void setHandsList(List<List<Bitmap>> handsList) {
        HandsList = handsList;
    }
    public List<List<Bitmap>> getFootsList() {
        return FootsList;
    }
    public void setFootsList(List<List<Bitmap>> footsList) {
        FootsList = footsList;
    }
    public List<List<Bitmap>> getTatoosLists() {
        return TatoosLists;
    }
    public void setTatoosLists(List<List<Bitmap>> tatoosLists) {
        TatoosLists = tatoosLists;
    }
    public List<List<Bitmap>> getNailsLists() {
        return NailsLists;
    }
    public void setNailsLists(List<List<Bitmap>> nailsLists) {
        NailsLists = nailsLists;
    }
}

并在 GetterSetter 中像这样设置列表。

GetterSetterForBitmaps getterSetterForBitmaps = new GetterSetterForBitmaps();
getterSetterForBitmaps.setHandsList(arrayOfLists);

但是当像这样从 GetterSetter 获取列表时......

GetterSetterForBitmaps getterSetterForBitmaps = new GetterSetterForBitmaps();
        List<List<Bitmap>> list = getterSetterForBitmaps.getHandsList();

它返回空值???

不要这样做,你会浪费大量的内存 序列化和反序列化位图,因为它将创建两个 同一对象的额外副本,从长远来看会花费您。

我建议创建一个数据层,以便在Android Components之间传递数据

例如

class MyImageCache
{
 static List<List<BitMap>> list;
 public static getList(){
    return list;
 }
 public static setList(List<List<BitMap>> list1){
   list=list1;
 }
}

这样可以避免跨应用程序创建同一对象的多个副本。

您可以修改"ImageCache"类的行为,以便更好地控制数据共享。

相关内容

最新更新