这个类有三个按钮添加表,添加椅子和重置添加表将添加tablein第二个布局添加椅子将在第二个布局添加椅子图像。两个按钮工作,但我想移动椅子和表(imageView
)在屏幕上。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<String> mSelecetdItems = new ArrayList<>();
public static final String SELECETD_ITEMS = "SELECETD_ITEMS";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addtable(View v){
this.mSelecetdItems.add("table");
showSecondActivity();
}
public void addchair(View v){
this.mSelecetdItems.add("chair");
showSecondActivity();
}
private void showSecondActivity() {
Intent i = new Intent(this,Second.class);
i.putStringArrayListExtra(SELECETD_ITEMS,this.mSelecetdItems);
startActivity(i);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList(SELECETD_ITEMS , mSelecetdItems);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
this.mSelecetdItems = savedInstanceState.getStringArrayList(SELECETD_ITEMS);
}
}
这个类有三个按钮添加表,添加椅子和重置添加表将添加tablein第二个布局添加椅子将添加椅子图像在第二个布局。我想在布局图像中移动桌椅。
Second.java
public class Second extends AppCompatActivity {
ImageView iv;
ImageView iv2;
private int xDelta;
private int yDelta;
LinearLayoutCompat hall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.second_toolbar);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
hall = (LinearLayoutCompat) findViewById(R.id.hall_layout);
Intent i = getIntent();
ArrayList<String> selecetdItems = i.getStringArrayListExtra(MainActivity.SELECETD_ITEMS);
for (String selecetdItem : selecetdItems) {
if (selecetdItem.equals("table")) {
addImageView(R.drawable.table, hall);
} else if (selecetdItem.equals("chair")) {
addImageView2(R.drawable.chair2, hall);
}
}
}
private void addImageView(int imageRes, LinearLayoutCompat hall) {
iv = new ImageView(this);
iv.setImageResource(imageRes);
iv.setLayoutParams(new android.view.ViewGroup.LayoutParams(250, 250));
iv.setMaxHeight(180);
iv.setMaxWidth(120);
iv.setPadding(15,15,0,0);
hall.addView(iv);
}
private void addImageView2(int imageRes, LinearLayoutCompat hall) {
iv2 = new ImageView(this);
iv2.setImageResource(imageRes);
iv2.setLayoutParams(new android.view.ViewGroup.LayoutParams(150, 150));
iv2.setMaxHeight(30);
iv2.setMaxWidth(30);
iv2.setPadding(25,0,0,0);
hall.addView(iv2);
}
}
activity_main.xml
此布局文件在Root中有ConstraintLayout
。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.andiroot.restaurantbook.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#ff1e8622"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="64dp">
<Button
android:id="@+id/btn_addtbl"
style="@style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="Add Table"
android:onClick="addtable"/>
<Button
android:id="@+id/btn_addchairs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addchair"
android:layout_marginTop="27dp"
android:text="Add Chair"
android:layout_below="@+id/btn_addtbl"
android:layout_alignEnd="@+id/btn_addtbl" />
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/btn_addchairs"
android:layout_below="@+id/btn_addchairs"
android:layout_marginTop="51dp"
android:text="Reset" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
activity_second.xml
此布局文件在Root
LinearLayoutCompat
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/hall_layout"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.andiroot.restaurantbook.Second">
<android.support.v7.widget.Toolbar
android:id="@+id/second_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#ff1e8622"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<!--
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hall_layout2">
</RelativeLayout>
-->
</android.support.v7.widget.LinearLayoutCompat>
</pre>
答案 0 :(得分:0)
试试这个
ImageView dishaIMG;
RelativeLayout cropperLayout;
float dX, dY;
float windowWidth, windowHeight;
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
windowHeight = displayMetrics.heightPixels;
windowWidth = displayMetrics.widthPixels;
cropperLayout = (RelativeLayout) findViewById(R.id.activity_main2);
dishaIMG = (ImageView) findViewById(R.id.img_disha);
dishaIMG.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
int width = dishaIMG.getLayoutParams().width;
int height = dishaIMG.getLayoutParams().height;
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = v.getX() - motionEvent.getRawX();
dY = v.getY() - motionEvent.getRawY();
case MotionEvent.ACTION_MOVE:
if (width == windowWidth && height == windowHeight) {
} else {
v.animate()
.x(motionEvent.getRawX() + dX)
.y(motionEvent.getRawY() + dY)
.setDuration(0)
.start();
if (motionEvent.getRawX() + dX + width > windowWidth) {
v.animate()
.x(windowWidth - width)
.setDuration(0)
.start();
}
if (motionEvent.getRawX() + dX < 0) {
v.animate()
.x(0)
.setDuration(0)
.start();
}
if (motionEvent.getRawY() + dY + height > windowHeight) {
v.animate()
.y(windowHeight - height)
.setDuration(0)
.start();
}
if (motionEvent.getRawY() + dY < 0) {
v.animate()
.y(0)
.setDuration(0)
.start();
}
}
}
return true;
}
});