我正在制作一个屏幕,允许用户将一个图像拖放到另一个图像上。之后,代码应该能够重新显示生成的图像。我正在努力使reddit图像正常大小。有关它的更多详细信息,请在旁边的屏幕3旁边以粗体显示。
注意:到目前为止我的所有图片都在res / drawable文件夹中。
屏幕3:这是我创建一个2位图组合的叠加位图后屏幕的样子(注意现在小于原始尺寸的reddit图像)。注意:如果我单独为reddit图像创建一个临时位图(而不是将其与另一个位图组合成一个叠加层),则reddit图标具有正确的宽度和高度。此问题仅与reddit的尺寸有关,而与图标在叠加层中的位置无关。
这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1"
android:orientation="vertical"
tools:context=".LobbyActivity">
<FrameLayout
android:id="@+id/source_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5">
<ImageView
android:id="@+id/flatStanleyImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reddit" />
<ImageView
android:id="@+id/postcardImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:visibility="gone" />
</FrameLayout>
<FrameLayout
android:id="@+id/target_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5">
<ImageView
android:id="@+id/attractionImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/attraction"
android:layout_marginBottom="50dp" />
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reset"
android:layout_gravity="bottom|left"/>
<Button
android:id="@+id/doneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="done"
android:layout_gravity="bottom|right"/>
</FrameLayout>
</LinearLayout>
这是Java代码:
public class LobbyActivity extends AppCompatActivity {
private static final String TAG = "LobbyActivity";
@BindView(R.id.flatStanleyImage)
protected ImageView flatStanleyImageView;
@BindView(R.id.postcardImage)
protected ImageView postcardImageView;
@BindView(R.id.target_layout)
protected FrameLayout targetLayout;
@BindView(R.id.source_layout)
protected FrameLayout sourceLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lobby);
ButterKnife.bind(this);
View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData clipData = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(clipData, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE); // we are dragging the shadow
return true;
}
};
flatStanleyImageView.setOnLongClickListener(longClickListener);
View.OnDragListener dragListener = new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_STARTED");
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENTERED");
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_EXITED");
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_LOCATION");
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENDED. DragEvent.getResult() " + event.getResult());
if (!event.getResult()) {
Log.d(TAG, "Drag was not successful.");
flatStanleyImageView.setVisibility(View.VISIBLE);
}
return true;
case DragEvent.ACTION_DROP:
Log.d(TAG, "Action is DragEvent.ACTION_DROP");
FrameLayout draggedImageParentViewLayout = (FrameLayout) flatStanleyImageView.getParent();
Log.d(TAG, "draggedImageParentViewLayout: " + draggedImageParentViewLayout.getId());
Log.d(TAG, "targetLayout: " + targetLayout.getId());
View view = (View) event.getLocalState();
float x = event.getX();
float y = event.getY();
view.setX(x-(view.getWidth()/2));
view.setY(y-(view.getWidth()/2));
if (draggedImageParentViewLayout != targetLayout) {
draggedImageParentViewLayout.removeView(flatStanleyImageView);
targetLayout.addView(flatStanleyImageView);
}
sourceLayout.invalidate();
targetLayout.invalidate();
return true;
default:
break;
}
return false;
}
};
targetLayout.setOnDragListener(dragListener);
}
@OnClick(R.id.resetButton)
protected void handleResetButtonClick() {
Log.d(TAG, "Begin handleResetButtonClick");
Intent intent = getIntent();
finish();
startActivity(intent);
Log.d(TAG, "End handleResetButtonClick");
}
@OnClick(R.id.doneButton)
protected void handleDoneButtonClick() {
Log.d(TAG, "Begin handleDoneButtonClick");
Bitmap attractionBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.attraction);
Bitmap flatStanleyBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.reddit);
Bitmap bitmapOverlay = Bitmap.createBitmap(attractionBitmap.getWidth(), attractionBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapOverlay);
canvas.drawBitmap(attractionBitmap, new Matrix(), null);
canvas.drawBitmap(flatStanleyBitmap, new Matrix(), null);
postcardImageView.setImageBitmap(bitmapOverlay);
postcardImageView.setVisibility(View.VISIBLE);
Log.d(TAG, "End handleDoneButtonClick");
}
}