2017-01-19 23 views
0

so 1は2 ImageButtons,clothesButton1、xmlで宣言された画像を持ち、imageButton2は空白です。両方とも別々の活動に入っています。 clothesButton1をクリックすると、ビットマップを使用してclothesButton1の画像をimageButton2に移動します。その後、clothesButton1は空白になります。ここで別個のアクティビティでの画像ボタンの移動onClickビットマップ

clothesButton1のためにJavaで私のコードです:

(imageButton2用)私の第二の活動で
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); 
     } 
    }); 

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

移動機能が動作していないと私は本当にどこ見当がつかないしかし、私は間違っています。どんな助けでも大歓迎です。

答えて

0

エラーは、意図のです。アンドロイドの明示的および暗黙的なインテントには、2つのタイプのインテントがあります。コンテキストとクラスオブジェクトを持つインテントを作成するときは、明示的なインテントを作成しています。明示的なインテントを使用して、アプリケーション内のアクティビティを開始します。アプリケーション内のアクティビティが別のアプリケーション内のアクティビティを開始する場合は、暗黙のインテントを作成します。あなたのケースでは、それはExplicit Intent use Intent intent = new Intent(getApplicationContext()、secondActivityName.class)です。あなたのケース

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

で 意図read this tutorialについての詳細を理解すること。

+0

説明と助けをありがとう!しかし、私はgetContext()を使ってみましたが、 "メソッドを解決できません"。だから、私は代わりにgetApplicationContext()を使用して動作させました。うまくいけば、それはアプリのための多くの違いを意味するものではありません。 – xlayer

+0

はい、あなたの右嬉しい助け! – Yirga

0

まず、getDrawingCache()がnullを返します.2番目に、他のアクティビティを呼び出す方法が正しくありません。 これを試してみてください。

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

参考:Android View.getDrawingCache returns null, only null

+0

説明と詳しいコードをありがとう! DrawingCacheについて何か新しいことを確かに学びました。あなたのコードは私のために働くが、それは私の新しい解決策よりもわずかに長い。他の人が将来使用する必要があるかもしれない場合にupvoteします。 – xlayer

+0

これはDrawingCacheを使用する最善の方法です。また、問題の解決に役立つ参照の2番目のソリューションと同じように、キャンバス上にビューを描画することもできます。 –

0

私はあなたがActivity Transitionで簡単にこれを実現することができると思います。それを見てください:) Start an activity with a shared elementセクションは、あなたが望むものかもしれません。

関連する問題