在单独的 Activity onClick 位图中的图像按钮之间移动图像
Moving of image between ImageButtons in seperate Activities onClick Bitmap
所以 1 有 2 个 ImageButtons
,clothesButton1
带有在 xml 中声明的图像,而 imageButton2
是空白的。两者都在单独的活动中。
单击 clothesButton1
后,我想使用位图将 clothesButton1
中的图像移动到 imageButton2
。 clothesButton1 之后会变成空白。
这是我在 Java 中用于 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);
}
});
在我的第二个 activity(对于 imageButton2):
final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapClothes");
imageButton2.setImageBitmap(bitmap);
但是移动功能不起作用,我真的不知道我哪里错了。非常感谢任何帮助。
我认为问题是您没有在 clothesButton1 上启用绘图缓存。
clothesButton1.setDrawingCacheEnabled(true);
请参考:https://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29
错误是因为 Intent。 android中的Intent有显式和隐式两种。当您使用上下文和 Class 对象创建 Intent 时,您正在创建一个显式 Intent。您使用显式意图在您的 application.When 中启动活动,您的应用程序中的 activity 想要在另一个应用程序中启动 activity,您创建了一个隐式意图。在您的情况下,它是 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。
首先,getDrawingCache()
returns null;其次,您调用其他 activity 的方式不正确!
试试这个:
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
我认为您可以使用 Activity Transition 轻松实现这一目标。好好看看吧:)
Start an activity with a shared element
部分可能是您想要的。
所以 1 有 2 个 ImageButtons
,clothesButton1
带有在 xml 中声明的图像,而 imageButton2
是空白的。两者都在单独的活动中。
单击 clothesButton1
后,我想使用位图将 clothesButton1
中的图像移动到 imageButton2
。 clothesButton1 之后会变成空白。
这是我在 Java 中用于 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);
}
});
在我的第二个 activity(对于 imageButton2):
final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapClothes");
imageButton2.setImageBitmap(bitmap);
但是移动功能不起作用,我真的不知道我哪里错了。非常感谢任何帮助。
我认为问题是您没有在 clothesButton1 上启用绘图缓存。
clothesButton1.setDrawingCacheEnabled(true);
请参考:https://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29
错误是因为 Intent。 android中的Intent有显式和隐式两种。当您使用上下文和 Class 对象创建 Intent 时,您正在创建一个显式 Intent。您使用显式意图在您的 application.When 中启动活动,您的应用程序中的 activity 想要在另一个应用程序中启动 activity,您创建了一个隐式意图。在您的情况下,它是 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。
首先,getDrawingCache()
returns null;其次,您调用其他 activity 的方式不正确!
试试这个:
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
我认为您可以使用 Activity Transition 轻松实现这一目标。好好看看吧:)
Start an activity with a shared element
部分可能是您想要的。