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的()和塞蒂(添加值),它不会显示在正确的图像点。此外,我希望图像应保持相同的位置,即使在不同的设备,这就是为什么我根据屏幕密度计算位置。
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;
}
});
}
}
你是什么意思时,你说的“每个设备使用相同的”你与拥有完全相同的屏幕尺寸和密度工作的所有设备都? –
我认为你需要展示你已经完成了什么,并且包含你期望发生的事情的图形。 LinearLayout应该做你想做的。如果使用权重,则不需要计算任何实际屏幕位置。 –
这是我的代码。请看看我希望你会帮我 – user3314144