草庐IT

关于构造函数:如何将列表列表通过bundle传递给android中的片段意味着List<List<Bitmap>>

codeneng 2023-03-28 原文

how to pass the list of lists means List<List<Bitmap>> through bundle to the fragment in android

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

1
2
3
4
5
6
7
8
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;
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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();
    }


}

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

1
2
3
4
5
6
7
8
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 类...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 中设置列??表..

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

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

1
2
3
GetterSetterForBitmaps getterSetterForBitmaps = new GetterSetterForBitmaps();

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

它返回null???

  • 也许您的列表位图太大,您不能将它们放入位图中。我认为您应该将列表可绘制资源放到 MainFragment 中。


Don't do this, you will be wasting great amount of memory in
serializing and de-serializing bitmaps as it will create two
additional copies of same object and will cost you in longer run.

我建议创建一个 DataLayer 用于在 Android Components

之间传递数据

例如

1
2
3
4
5
6
7
8
9
10
class MyImageCache
{
 static List<List<BitMap>> list;
 public static getList(){
    return list;
 }
 public static setList(List<List<BitMap>> list1){
   list=list1;
 }
}

这将使您免于在应用程序中创建同一对象的多个副本。

You can modify"ImageCache" class's behavior to have more control over data sharing.

  • 感谢您的回复
  • 这对你的案子有帮助吗?
  • 是的,我会采纳你的建议。我认为这会对我有所帮助。
  • 这里需要注意的一件微不足道但重要的事情是,如果 android 系统内存不足,您的静态变量可能会变为 null。在这种情况下,您需要设计一种机制来重新初始化缓存。看到这个
  • @Nayan Srivastava 请查看我更新的问题....我使用吸气剂二传手,但我面临问题
  • 变量和方法需要是静态的,这样你就可以在不创建多个对象的情况下共享它们
  • @Nayan Srivastava 你能回答这个问题吗?

有关关于构造函数:如何将列表列表通过bundle传递给android中的片段意味着List<List<Bitmap>>的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  3. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  4. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  5. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  6. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  7. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  8. ruby-on-rails - 没有参数的 `<<`(小于两倍)是什么意思? - 2

    我在一个我想在formtasticGem中覆盖的方法中找到了这个。该方法如下所示:defto_htmlinput_wrappingdohidden_field_html是什么意思?在第三行做什么?我知道它对数组有什么作用,但在这里我不知道。 最佳答案 你可以这样读:hidden_field_htmllabel_with_nested_checkbox是连接到hidden_​​field_html末尾的参数-为了“清晰”,他们将其分成两行 关于ruby-on-rails-没有参数的`

  9. ruby-on-rails - 找不到 gem railties (>= 0.a) (Gem::GemNotFoundException) - 2

    我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby​​2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10

  10. ruby - 如何将 Puma::Configuration 传递给 Sinatra? - 2

    这是我的网络应用:classFront我是这样开始的(请不要建议使用Rack):Front.start!这是我的Puma配置对象,我不知道如何传递给它:require'puma/configuration'Puma::Configuration.new({log_requests:true,debug:true})说真的,怎么样? 最佳答案 配置与您运行的方式紧密相关puma服务器。运行的标准方式puma-pumaCLI命令。为了配置puma配置文件config/puma.rb或config/puma/.rb应该提供(参见examp

随机推荐