gpt4 book ai didi

android - 如何使android中的多个 View 移动..?

转载 作者:行者123 更新时间:2023-11-29 21:31:01 26 4
gpt4 key购买 nike

当我实现触摸时我有两个 ImageView 只有一个 View 能够移动整个布局但其他 ImageView 保持在固定位置我们如何让其他 ImageView 从一个地方移动到其他如果它没有放置多 View 移动的代码请帮助plz.. friend

最佳答案

我很久以前写过这段代码,看看它是否适合你

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DragAndDropActivity extends Activity {
public FrameLayout board;

int dropZone1_X, dropZone2_X, dropZone3_X, dropZone1_Y, dropZone2_Y,
dropZone3_Y, movingCoordinateLeft = 0, movingCoordinateTop = 0;

int windowHeight, windowWidth, defaultMargin = 150;
ImageView answerOption1, answerOption2, answerOption3, dropZone1,
dropZone2, dropZone3;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
board = new FrameLayout(this);
setContentView(R.layout.layout);

/*
* set id's of view objects
*/
setIds();

/*
* set on touch listener
*/
setOnTouchListener();
/*
* get window dimensions
*/
getWindowDimensions();

}

private void setOnTouchListener() {
// TODO Auto-generated method stub
answerOption1.setOnTouchListener(dragt);
answerOption2.setOnTouchListener(dragt);
answerOption3.setOnTouchListener(dragt);

}

private void setIds() {
// TODO Auto-generated method stub

board = (FrameLayout) findViewById(R.id.Board);
// ids for answer options
answerOption1 = (ImageView) findViewById(R.id.answer_option_1);
answerOption2 = (ImageView) findViewById(R.id.answer_option_2);
answerOption3 = (ImageView) findViewById(R.id.answer_option_3);

// ids for drop zones
dropZone1 = (ImageView) findViewById(R.id.frame1);
dropZone2 = (ImageView) findViewById(R.id.frame2);
dropZone3 = (ImageView) findViewById(R.id.frame3);

}

/*
*
* Get default view dimensions at run time
*/

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {

System.out.println("Method--onWindowFocusChanged");

System.out.println("\n\nFirst drop zone dimensions");
System.out.println("left margin-->" + dropZone1.getLeft());
System.out.println("top margin-->" + dropZone1.getTop());

System.out.println("\n\nSecond drop zone dimensions");
System.out.println("left margin-->" + dropZone2.getLeft());
System.out.println("top margin-->" + dropZone2.getTop());

System.out.println("\n\nThird drop zone dimensions");
System.out.println("left margin-->" + dropZone3.getLeft());
System.out.println("top margin-->" + dropZone3.getTop());

}
}

private void getWindowDimensions() {
// TODO Auto-generated method stub
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
windowHeight = metrics.heightPixels;
System.out.println("window height" + windowHeight);
windowWidth = metrics.widthPixels;
System.out.println("window width" + windowWidth);

}

// onCreate
OnTouchListener dragt = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
switch (v.getId()) {// What is being touched
/***
*
* Answer option 1
*
* ***/
case R.id.answer_option_1: {
// Which action is being taken
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
par.topMargin = (int) event.getRawY()
- (v.getHeight() + 22);
par.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2 + 150);

movingCoordinateLeft = (int) event.getRawX()
- (v.getWidth() / 2 + 0);
movingCoordinateTop = par.topMargin;

System.out.println("Answer 1 --- left"
+ movingCoordinateLeft + "---top"
+ movingCoordinateTop);

v.setLayoutParams(par);

break;
}// inner case MOVE
case MotionEvent.ACTION_UP: {
par.height = 40;
par.width = 40;
/*
* par.topMargin = (int) event.getRawY() - (v.getHeight() +
* 15); par.leftMargin = (int) event.getRawX() -
* (v.getWidth() / 2 + 90);
*/

if (windowHeight < 460) {
par.topMargin = 109;
par.leftMargin = 0;
par.height = 22;
par.width = 105;

} else {
par.topMargin = defaultMargin;
par.leftMargin = 0;
}

// check if co-ordinates matched and drop answer in drop
// zone
if ((movingCoordinateLeft > 10 && movingCoordinateLeft < 80)
&& (movingCoordinateTop > 10 && movingCoordinateTop < 100)) {

System.out.println("left " + movingCoordinateLeft
+ "top " + movingCoordinateTop);

dropZone1.setImageDrawable(getResources().getDrawable(
R.drawable.duck));
answerOption1.setVisibility(View.INVISIBLE);

}

v.setLayoutParams(par);
break;
}// inner case UP
case MotionEvent.ACTION_DOWN: {

System.out.println("left" + event.getRawX());
System.out.println("top" + event.getRawY());

if (windowHeight < 460) {

par.height = 40;
par.width = 40;

} else {
par.height = 40;
par.width = 40;
}

v.setLayoutParams(par);
break;
}// inner case UP
}// inner switch
break;
}// case pawn

/***
*
* Answer option 2
*
* ***/

case R.id.answer_option_2: {// Which action is being taken
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {

par.topMargin = (int) event.getRawY()
- (v.getHeight() + 22);
par.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2 + 150);

movingCoordinateLeft = (int) event.getRawX()
- (v.getWidth() / 2 + 0);
movingCoordinateTop = par.topMargin;

v.setLayoutParams(par);

break;
}// inner case MOVE
case MotionEvent.ACTION_UP: {
par.height = 40;
par.width = 40;
/*
* par.topMargin = (int) event.getRawY() - (v.getHeight() +
* 15); par.leftMargin = (int) event.getRawX() -
* (v.getWidth() / 2 + 90);
*/

if (windowHeight < 460) {
par.topMargin = 150;
par.leftMargin = 0;
par.height = 40;
par.width = 40;

} else {
par.topMargin = 200;
par.leftMargin = 0;
}

// check if co-ordinates matched and drop answer in drop
// zone
if ((movingCoordinateLeft > 120 && movingCoordinateLeft < 200)
&& (movingCoordinateTop > 10 && movingCoordinateTop < 100)) {

System.out.println("left " + movingCoordinateLeft
+ "top " + movingCoordinateTop);

dropZone2.setImageDrawable(getResources().getDrawable(
R.drawable.hen));
answerOption2.setVisibility(View.INVISIBLE);
}

v.setLayoutParams(par);

break;
}// inner case UP
case MotionEvent.ACTION_DOWN: {

if (windowHeight < 460) {

par.height = 40;
par.width = 40;

} else {
par.height = 40;
par.width = 40;
}

v.setLayoutParams(par);
break;
}// inner case UP
}// inner switch
break;
}// case pawn2

/***
*
* Answer option 3
*
* ***/

case R.id.answer_option_3: {// Which action is being taken
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {

par.topMargin = (int) event.getRawY()
- (v.getHeight() + 22);
par.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2 + 150);

movingCoordinateLeft = (int) event.getRawX()
- (v.getWidth() / 2 + 0);
movingCoordinateTop = par.topMargin;

v.setLayoutParams(par);

break;
}// inner case MOVE
case MotionEvent.ACTION_UP: {
par.height = 40;
par.width = 40;
/*
* par.topMargin = (int) event.getRawY() - (v.getHeight() +
* 15); par.leftMargin = (int) event.getRawX() -
* (v.getWidth() / 2 + 90);
*/

if (windowHeight < 460) {
par.topMargin = 191;
par.leftMargin = 0;
par.height = 40;
par.width = 40;

} else {
par.topMargin = 250;
par.leftMargin = 0;
}

// check if co-ordinates matched and drop answer in drop
// zone
if ((movingCoordinateLeft > 220 && movingCoordinateLeft < 310)
&& (movingCoordinateTop > 10 && movingCoordinateTop < 100)) {

System.out.println("left " + movingCoordinateLeft
+ "top " + movingCoordinateTop);

dropZone3.setImageDrawable(getResources().getDrawable(
R.drawable.queen));
answerOption3.setVisibility(View.INVISIBLE);
}
v.setLayoutParams(par);

break;
}// inner case UP
case MotionEvent.ACTION_DOWN: {
System.out.println("down");
if (windowHeight < 460) {

par.height = 40;
par.width = 40;

} else {
par.height = 40;
par.width = 40;
}
v.setLayoutParams(par);
break;
}// inner case UP
}// inner switch
break;
}// case pawn2

}// switch
return true;
}// onTouch

};// dragt

}

和layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal" >

<ImageView
android:id="@+id/frame3"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_alignParentRight="true"
android:src="@drawable/drag_drop_button" />

<ImageView
android:id="@+id/frame2"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_toLeftOf="@+id/frame3"
android:src="@drawable/drag_drop_button" />

<ImageView
android:id="@+id/frame1"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_toLeftOf="@+id/frame2"
android:src="@drawable/drag_drop_button" />
</RelativeLayout>

<FrameLayout
android:id="@+id/Board"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >

<ImageView
android:id="@+id/answer_option_1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="150dp"
android:background="@drawable/duck" >
</ImageView>

<ImageView
android:id="@+id/answer_option_2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@+id/answer_option_1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
android:background="@drawable/hen" >
</ImageView>

<ImageView
android:id="@+id/answer_option_3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="250dp"
android:background="@drawable/queen" >
</ImageView>
</FrameLayout>

</RelativeLayout>

关于android - 如何使android中的多个 View 移动..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19515313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com