我正在尝试通过使用
在运行时更改边距来更改FrameLayout中某些TextView的位置MarginLayoutParams newMarginParams = new MarginLayoutParams(view.getLayoutParams());
newMarginParams.setMargins(x, y, 0, 0);
FrameLayout.LayoutParams newLayoutParams = new FrameLayout.LayoutParams(
newMarginParams);
view.setLayoutParams(newLayoutParams);
我检查了x和y值是否正确,但未显示TextView。我试图为每个TextView调用invalidate(),但它不会改变任何东西......
完整的代码如下所示。每个TextView的边距都在updateLayouts(...)中更新。
有人可以帮忙吗?
public class ARActivity extends Activity implements LocationListener, SensorEventListener{
FrameLayout mainLayout;
private CBCameraView cv;
private SensorManager mSensorManager;
private LocationManager mLocationManager;
private Location mDeviceLocation;
private float mDeviceAzimuth = 0;
private double mDeviceInclination = 0;
private static final float K = 0.1f; // filtering coefficient
private static final float ONE_MINUS_K = 1 - K; // filtering coefficient
private float xAngleWidth = 52.2f; // Samsung Galaxy S: 52.2
private float yAngleWidth = 39.4f; // Samsung Galaxy S: 39.4
private final float screenWidth = GlobalVars.screenWidth;
private final float screenHeight = GlobalVars.screenHeight;
private final float[] mR = new float[9];
private final float[] I = new float[9];
private final float[] orientation = new float[3];
private final float[] remappedR = new float[9];
private float[] mags;
private float[] accels;
int nARItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ar_layout);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, this);
mDeviceLocation = GlobalVars.location;
mainLayout = (FrameLayout) findViewById(R.id.ar_layout);
cv = new CBCameraView(this);
cv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mainLayout.addView(cv);
// Add ARViews
Location location = new Location("me");
location.setLatitude(41.371974);
location.setLongitude(2.166978);
ARView fs = new ARView(this);
fs.azimuth = 0;
fs.inclination = 0;
fs.location = location;
fs.setText("Bar seco");
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
fs.setLayoutParams(params);
mainLayout.addView(fs);
nARItems = mainLayout.getChildCount();
}
public void onLocationChanged(Location location)
{
mDeviceLocation = location;
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
public void onAccuracyChanged(Sensor arg0, int arg1){}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
mags = event.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER:
accels = event.values.clone();
break;
}
if (mags != null && accels != null) {
SensorManager.getRotationMatrix(mR, I, accels, mags);
SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_X, SensorManager.AXIS_Z, remappedR);
SensorManager.getOrientation(remappedR, orientation);
float tmp = (float) Math.toDegrees(orientation[0]);
if(tmp<0)
tmp += 360;
if(Math.abs(mDeviceAzimuth - tmp)<300)
mDeviceAzimuth = (float) (tmp * K) + (mDeviceAzimuth * ONE_MINUS_K);
else
mDeviceAzimuth = tmp;
tmp = (float) Math.round(Math.toDegrees(orientation[1]));
mDeviceInclination = (float)(-tmp * K) + (mDeviceInclination * ONE_MINUS_K);
updateLayouts(mDeviceAzimuth, (float)mDeviceInclination, mDeviceLocation);
}
}
public void updateLayouts(float azimuth, float zAngle, Location location)
{
float leftArm = azimuth -(xAngleWidth/2);
float rightArm = azimuth +(xAngleWidth/2);
if(leftArm < 0)
leftArm = leftArm + 360;
if(rightArm > 360)
rightArm = rightArm - 360;
float upperArm = zAngle + (yAngleWidth/2);
float lowerArm = zAngle - (yAngleWidth/2);
for(int i=1; i<nARItems; i++)
{
ARView view = (ARView)mainLayout.getChildAt(i);
if(location != null && view.location != null)
{
view.azimuth = location.bearingTo(view.location);
if(view.azimuth < 0)
view.azimuth = 360+view.azimuth;
}
int x = (int)calcXvalue(leftArm, rightArm, view.azimuth);
int y = (int)calcYvalue(lowerArm, upperArm, view.inclination);
MarginLayoutParams newMarginParams = new MarginLayoutParams(view.getLayoutParams());
newMarginParams.setMargins(x, y, 0, 0);
FrameLayout.LayoutParams newLayoutParams = new FrameLayout.LayoutParams(
newMarginParams);
view.setLayoutParams(newLayoutParams);
}
}
public class ARView extends TextView
{
public float azimuth;
public float distance;
public float inclination;
public Location location;
public ARView(Context context)
{
super(context);
}
}
}
答案 0 :(得分:0)
问题可能在于布局是framelayout。我之前在我的代码中使用过MarginLayoutParams,即使我更改边距的部分看起来与你的相同,它也能正常工作。唯一的区别是我使用的是LinearLayout。