编辑 3:
出于历史原因,我保留下面的原始问题。但是,我发现这个问题并不是孤立的FrameLayout
。因此,更新了标题。
我没有用更多代码过度拥挤这篇文章,而是创建了一个示例项目来演示这个问题;并将其上传到 Google Project Hosting。
这个问题的总结是这样的:
具有特定边界的纵向可绘制对象,在改变方向并返回纵向时,不保留设置的边界。相反,它恢复到原来的界限。尽管强行设置了方向变化的明确界限,但这仍然存在。请注意,您稍后在 Click 等上设置的任何界限都会被遵守。
我还上传了一个版本的应用程序,其中包含 2 个单独的活动来说明该问题。
- 简单的普通活动只是说明了这个问题。
- 使用自定义
BitmapDrawable
的活动ImageView
- 每当更改边界时,都会打印以记录。
第二个版本清楚地表明,即使我在 Drawable 上设置了界限,这些界限在onBoundsChange()
.
原始问题:
我正在使用一个FrameLayout
2 ImageView
s 堆叠在另一个之上来显示“电池状态”图形。这是纵向模式。在横向模式下,我显示不同的布局(图表)。
我的问题是——假设正在显示电池状态——比如 30%。现在,我旋转屏幕并显示图表。当我回到纵向时,电池图形会回到原来的位置(即“满”)。
我已经尝试了各种各样的事情来试图弄清楚发生了什么。调试显示“顶部”图形的边界确实按预期设置。所以这似乎是一个失效问题。我正在展示 2 个类和 2 个布局 XML(均已简化)的代码,这有助于重现该问题。还附加了用于 ImageViews 的占位符 PNG。
任何人都可以发现错误吗?要重新创建问题,请运行应用程序,然后单击“更新”按钮。图形将被“填充”到一定程度。然后,切换到横向,然后再回到纵向。该图形不记得其先前的值。
活动:
public class RotateActivity extends Activity {
private View portraitView, landscapeView;
private LayoutInflater li;
private Configuration mConfig;
private ValueIndicator indicator;
private Button btn;
private Random random = new java.util.Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
li = LayoutInflater.from(this);
portraitView = li.inflate(R.layout.portrait, null);
landscapeView = li.inflate(R.layout.landscape, null);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mConfig = newConfig;
initialize();
}
@Override
protected void onResume() {
mConfig = getResources().getConfiguration();
initialize();
super.onResume();
}
private void initialize(){
if(mConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
displayLandscape();
} else {
displayPortrait();
}
}
private void displayLandscape() {
setContentView(landscapeView);
}
private void displayPortrait() {
setContentView(portraitView);
btn = (Button)portraitView.findViewById(R.id.button1);
indicator = (ValueIndicator)portraitView.findViewById(R.id.valueIndicator1);
/*
* Forcing the graphic to perform redraw to its known state when we return to portrait view.
*/
indicator.updateIndicatorUi();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateIndicator(random.nextInt(100));
}
});
}
private void updateIndicator(int newValue){
indicator.setPercent(newValue);
}
}
肖像.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.gs.kiran.trial.inval.ValueIndicator
android:id="@+id/valueIndicator1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update" />
</LinearLayout>
值指示符.java
public class ValueIndicator extends FrameLayout {
private ImageView ivFrame, ivContent;
private Drawable drFrame, drContent;
private Rect mBounds;
private int currentTop;
private int mPercent;
public ValueIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View root = li.inflate(R.layout.indicator, this, true);
ivFrame = (ImageView) root.findViewById(R.id.ivFrame);
ivContent = (ImageView) root.findViewById(R.id.ivContent);
drFrame = ivFrame.getDrawable();
drContent = ivContent.getDrawable();
mBounds = drFrame.getBounds();
Log.d(Constants.LOG_TAG, "Constructor of ValueIndicator");
}
public void setPercent(int newPercent){
this.mPercent = newPercent;
updateIndicatorUi();
}
public void updateIndicatorUi(){
Rect newBounds = new Rect(mBounds);
newBounds.top = mBounds.bottom - (int)(this.mPercent * mBounds.height() / 100);
currentTop = newBounds.top;
Log.d(Constants.LOG_TAG, "currentTop = "+currentTop);
drContent.setBounds(newBounds);
//invalidateDrawable(drContent);
invalidate();
}
}
indicator.xml(自定义视图中使用的 FrameLayout 的 XML)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ivFrame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/frame" />
<ImageView
android:id="@+id/ivContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/content" />
</FrameLayout>
Landscape.xml(虚拟占位符 - 足以重新创建问题)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Landscape" />
</LinearLayout>
AndroidManifest.xml 片段:
<activity
android:label="@string/app_name"
android:name=".RotateActivity"
android:configChanges="orientation|keyboardHidden">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
编辑
我还尝试调用setPercent()
并传入保存的值displayPortraitView()
- 基本上每当我们回到纵向模式时强制更新到已知状态。仍然没有运气。请注意,日志告诉我可绘制对象的边界是正确的。我无法弄清楚为什么没有发生失效。
编辑2:
- 在 ValueIndicator.java 中,我引入了一个成员变量
mPercent
,它始终存储最后一个已知的百分比值。 - 更新了
setPercent()
代码以更新成员变量mPercent
;然后调用updateIndicateUi()
方法。 updateIndicatorUi()
(现在是一种public
方法)现在使用状态(即mPercent
)来完成它的工作。- 每当我们回到纵向模式时,我都会调用
updateIndicatorUi()
. 这会强制电池图形自行更新。
我还更新了代码以反映这些更改。
这个想法是每当我们从横向模式返回纵向模式时强制重绘和无效。再次 - 我确实看到根据需要设置了可绘制的电池“内容”的边界,但 UI 拒绝跟上步伐。