I am developing an android board game in which images can be dragged. I want to put images on particular position when the game starts and position should remain same on each device. I am using Linear layout and absolute layout. I have calculated screen width and apply formula,position of image and use set X() and set Y() but still it is not working. Position changed when i run application. need help please.
他是xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bk"
android:id="@+id/LLayout"
>
<AbsoluteLayout
android:id="@+id/absLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:src="@drawable/dice" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:src="@drawable/dice" />
</AbsoluteLayout>
</LinearLayout>
这是.java: 当我在setX()和setY()中添加值时,它不会在正确的点上显示图像。此外,我希望图像应该在不同的设备上保持相同的位置,这就是我根据屏幕密度计算位置的原因。
public class MoveImagesOnTouchActivity extends Activity{
ImageView img=null;
ImageView img2=null;
AbsoluteLayout aLayout;
Point point;
int status=0;
int width;
int height;
int x,y,x2,y2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
width = this.getResources().getDisplayMetrics().widthPixels;
height = this.getResources().getDisplayMetrics().heightPixels;
//Toast.makeText(MoveImagesOnTouchActivity.this, " width is : "+width+ "height is :"+height , 50000).show();
img=(ImageView)findViewById(R.id.imageView);
img2=(ImageView)findViewById(R.id.imageView2);
aLayout= (AbsoluteLayout)findViewById(R.id.absLayout);
x= (9*width)/100;
y= (80*height)/100;
img.setX(x);
img.setY(y);
x2=(143*width)/100;
y2= (80*height)/100;
img2.setX(x2);
img2.setY(y2);
img.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
status=1;
Log.i("ImageStatus",""+status);
int[] values= new int[2];
img.getLocationOnScreen(values);
//img.getLocationInWindow(values);
int x = values[0];
int y = values[1] ;
//Toast.makeText(MoveImagesOnTouchActivity.this, " x is : "+x+ "y :"+y , 500).show();
return false;
}
});
img2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
status=2;
Log.i("ImageStatus",""+status);
int[] values= new int[2];
img2.getLocationOnScreen(values);
//img.getLocationInWindow(values);
int x2 = values[0];
int y2 = values[1] ;
Toast.makeText(MoveImagesOnTouchActivity.this, " x2 is : "+x2+ "y2 :"+y2 , 5000).show();
return false;
}
});
aLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("touch",""+event);
if(status==1) // any event from down and move
{
//img.setX((9*width)/100);
//img.setY((80*height)/100);
//img.setY((80*height)/100);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
img.setLayoutParams(lp);
}
if(status==2) // any event from down and move
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img2.getWidth()/2,(int)event.getY()-img2.getHeight()/2);
img2.setLayoutParams(lp);
}
if(event.getAction()==MotionEvent.ACTION_UP){
status=0;
img.setBackgroundColor(Color.TRANSPARENT);
img2.setBackgroundColor(Color.TRANSPARENT);
}
return true;
}
});
}
}
答案 0 :(得分:0)
要设置图像位置,您可以使用
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB ) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 50; // use your value
params.topMargin = (int) 50;
mImageView.setLayoutParams(params);
}else{
// your setX() and setY()
}
您需要指定比 HONEYCOMB
更低的版本在我的情况下,我正在尝试使用下面的
设置布局位置 /**
* @param Relative-Layout layout for set position
* @param height [preous layout height
*/
@SuppressLint("NewApi")
public void setLayoutXY(LinearLayout linearLayout, int height) {
// TODO Auto-generated method stub
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB ) {
// only for lower version than honeycomb
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.topMargin = height;
linearLayout.setLayoutParams(params);
}
else{
linearLayout.setY(height);
}
}
它可能对您有所帮助
检查已编辑的代码
package com.example.stacktest;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
public class MoveImagesOnTouchActivity extends Activity {
ImageView img = null;
ImageView img2 = null;
AbsoluteLayout aLayout;
Point point;
int status = 0;
int width;
int height;
int x, y, x2, y2;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
width = this.getResources().getDisplayMetrics().widthPixels;
height = this.getResources().getDisplayMetrics().heightPixels;
// Toast.makeText(MoveImagesOnTouchActivity.this, " width is : "+width+
// "height is :"+height , 50000).show();
img = (ImageView) findViewById(R.id.imageView);
img2 = (ImageView) findViewById(R.id.imageView2);
aLayout = (AbsoluteLayout) findViewById(R.id.absLayout);
x = (9 * width) / 100;
y = (80 * height) / 100;
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, x, y);
img.setLayoutParams(params);
// img.setX(x);
// img.setY(y);
x2 = (143 * width) / 100;
y2 = (80 * height) / 100;
AbsoluteLayout.LayoutParams params1 = new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, x2, y2);
img2.setLayoutParams(params1);
// img2.setX(x2);
// img2.setY(y2);
img.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
status = 1;
Log.i("ImageStatus", "" + status);
int[] values = new int[2];
img.getLocationOnScreen(values);
// img.getLocationInWindow(values);
int x = values[0];
int y = values[1];
// Toast.makeText(MoveImagesOnTouchActivity.this, " x is : "+x+
// "y :"+y , 500).show();
return false;
}
});
img2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
status = 2;
Log.i("ImageStatus", "" + status);
int[] values = new int[2];
img2.getLocationOnScreen(values);
// img.getLocationInWindow(values);
int x2 = values[0];
int y2 = values[1];
Toast.makeText(MoveImagesOnTouchActivity.this,
" x2 is : " + x2 + "y2 :" + y2, 5000).show();
return false;
}
});
aLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("touch", "" + event);
if (status == 1) // any event from down and move
{
// img.setX((9*width)/100);
// img.setY((80*height)/100);
// img.setY((80*height)/100);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (int)event.getY()-img.getHeight()/2, (int) event.getX() - img.getWidth() / 2);
// params.topMargin = (int)event.getY()-img.getHeight()/2;
// params.leftMargin = (int) event.getX() - img.getWidth() / 2;
img.setLayoutParams(params);
// LayoutParams lp = new
// LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
// img.setLayoutParams(lp);
}
if (status == 2) // any event from down and move
{
// LayoutParams lp = new LayoutParams(
// LayoutParams.WRAP_CONTENT,
// LayoutParams.WRAP_CONTENT, (int) event.getX()
// - img2.getWidth() / 2, (int) event.getY()
// - img2.getHeight() / 2);
// img2.setLayoutParams(lp);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (int)event.getY()-img.getHeight()/2, (int) event.getX() - img.getWidth() / 2);
// params.topMargin = (int)event.getY()-img.getHeight()/2;
// params.leftMargin = (int) event.getX() - img.getWidth() / 2;
img2.setLayoutParams(params);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
status = 0;
img.setBackgroundColor(Color.TRANSPARENT);
img2.setBackgroundColor(Color.TRANSPARENT);
}
return true;
}
});
}
}