Java 在android中使用浮点值设置布局边线

Java 在android中使用浮点值设置布局边线,java,android,android-layout,mobile,Java,Android,Android Layout,Mobile,我想设置Imageview的边距。我想要实现的是将一个图像向上移动一段距离。屏幕的内容是相对的,左边是ImageView,右边是按钮对齐。现在我想在100次点击按钮后,在图像和按钮之间的间隙内从左向右移动图像。我下面的代码如下: private static int count= 1 ; public void onClick(View view) { RelativeLayout lay = (RelativeLayout)findViewById(R.id.layout1);

我想设置Imageview的边距。我想要实现的是将一个图像向上移动一段距离。屏幕的内容是相对的,左边是ImageView,右边是按钮对齐。现在我想在100次点击按钮后,在图像和按钮之间的间隙内从左向右移动图像。我下面的代码如下:

private static int count= 1 ;

public void onClick(View view) {


    RelativeLayout lay = (RelativeLayout)findViewById(R.id.layout1);
    ImageView i1= (ImageView)findViewById(R.id.img1);
    ImageButton btn= (ImageButton)findViewById(R.id.iBtn);

    int i1Width = i1.getWidth();
    int btnWidth = btn.getWidth();
    int totalMragine = lay.getWidth() - i1Width - btnWidth ; //the total margine image will move.
    int stepSize =  (lay.getWidth() - i1Width - btnWidth ) / 100;   
    int step = stepSize * count++;  

    Log.i("i1Width ", ""+i1Width );
    Log.i("btnWidth ", ""+btnWidth );
    Log.i("lay.getWidth()", ""+lay.getWidth());     
    Log.i("stepSize", ""+stepSize);     
    Log.i("count", ""+count);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(step, 0, 0, 0);
    i1.setLayoutParams(lp);     

    LinearLayout.LayoutParams lp1 = (LinearLayout.LayoutParams) horse.getLayoutParams();
    Log.i("currentMargine", ""+lp1.leftMargin);

    if(lp1.leftMargin >= totalMragine ){
        Log.i("Result", "maringe over");
    }
}

我想知道的是,我设置的边距是整数,但步长的值是浮点值。那么如何在LayoutParams中设置边距浮动?或者有其他方法可以做到这一点吗?

你可以做一件事,计算每次移动的距离,就像要移动一样

移动距离=剩余长度/剩余点击次数

在最后一步(第100步)中,它将以错误5px结束,并且5px太小)

喜欢

1st click  distance_to_move = 370/100;   as round off come 4 

2nd click  distance_to_move = 366/99;    as round off come 4 

3rd click  distance_to_move = 362/98;    as round off come 4 

所以movemnt将与px4444…3444…3444…3444…3444…一样。

小数点后的值重要吗?四舍五入是否不好?否。例如,btn和img之间的间隙为370。所以步长是370/100。它应该是3.7,但由于它是整数,所以它的步长是3。实际上它每一步应该移动3.7px。gr8…现在很有趣。。。。。。。