0

So 1 have 2 ImageButtons, clothesButton1 with an image declared in xml, and imageButton2 which is blank. Both are in seperate activities. Upon clicking clothesButton1, i want to move the image in clothesButton1 to imageButton2 using Bitmap. clothesButton1 will become blank afterwards.

Here's my code in Java for clothesButton1:

final ImageButton clothesButton1 =(ImageButton)findViewById(R.id.clothesBtn1);

clothesButton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            clothesButton1.buildDrawingCache();
            Bitmap bitmapclothes = clothesButton1.getDrawingCache();
            Intent intent = new Intent();
            intent.putExtra("BitmapClothes", bitmapclothes);
        }
    });

In my second activity (for imageButton2):

 final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
 Intent intent = getIntent();
 Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapClothes");
 imageButton2.setImageBitmap(bitmap);

However the moving function isn't working and I really have no idea where I am wrong. Any help is greatly appreciated.

xlayer
  • 25
  • 4

4 Answers4

0

I think the problem is that you didn't enabled the drawing cache on clothesButton1.

clothesButton1.setDrawingCacheEnabled(true);

Please refer:https://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29

Apoorv Parmar
  • 274
  • 4
  • 9
0

The error is because of Intent. There are two types of Intent in android Explicit and implicit intents. When you create an Intent with a Context and a Class object, you are creating an explicit intent. You use explicit intents to start activities within your application.When an activity in your application wants to start an activity in another application, you create an implicit intent. In Your case it is Explicit Intent use Intent intent=new Intent(getApplicationContext(), secondActivityName.class). In your case

Intent intent=new Intent(getApplicationContext(),secondActivityName.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);

To understand more about intent read this tutorial.

Yirga
  • 881
  • 1
  • 12
  • 31
  • Thank you very much for the explaination and help! However, i tried using getContext() but "cannot resolve method". So i used getApplicationContext() instead for it to work. Hopefully it doesn't mean much difference for the app. – xlayer Jan 19 '17 at 18:26
0

First,getDrawingCache() returns null;second the way you invoke other activity is not correct! try this :

clothesButton1.setDrawingCacheEnabled(true);
clothesButton1.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
clothesButton1.layout(0, 0, clothesButton1.getMeasuredWidth(), imageButton.getMeasuredHeight());
clothesButton1.buildDrawingCache(true);
Bitmap bitmapclothes = Bitmap.createBitmap(clothesButton1.getDrawingCache());
clothesButton1.setDrawingCacheEnabled(false);
Intent intent = new Intent(NowActivity.this,SecondActivity.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);

Reference:Android View.getDrawingCache returns null, only null

Community
  • 1
  • 1
木大白易
  • 684
  • 1
  • 7
  • 16
  • Thank you for the explaination and detailed code! Certainly learned something new about DrawingCache. Your code works for me but it's slightly longer than my new solution. Will upvote in case other people might need to use it in the future – xlayer Jan 20 '17 at 03:18
  • This is the best way to use DrawingCache,you can also draw the view on canvas by yourself just like the second solution in the reference which can also solve your problem! – 木大白易 Jan 20 '17 at 03:38
0

I think that you can achieve this easily with Activity Transition. Take a good look at it :) The Start an activity with a shared element section might be what you want.

ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54