本文介绍了如何使android的多个视图移动..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个形象的看法时,我已经实现了触摸只有一个观点是能够移动整体布局,但其它图像的观点是在保持在固定的位置上,我们怎样才能使其他图像视图移动从一个地方到其他如果不把code多视图运动帮帮忙PLZ ..朋友
I have two image views when i have implemented the on touch only one view was able to move whole the layout but other image view was at the kept at the fixed position how can we make other image view to move from one place to other If it doesn't place code for movement of multiple view Help out plz.. friends
推荐答案
我写了这个code很久以前,看它是否适合你。
I have written this code long time ago, see if it works for you
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
and 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的多个视图移动..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!