5

编辑 3:

出于历史原因,我保留下面的原始问题。但是,我发现这个问题并不是孤立的FrameLayout。因此,更新了标题。

我没有用更多代码过度拥挤这篇文章,而是创建了一个示例项目来演示这个问题;并将其上传到 Google Project Hosting。

这个问题的总结是这样的:

具有特定边界的纵向可绘制对象,在改变方向并返回纵向时,不保留设置的边界。相反,它恢复到原来的界限。尽管强行设置了方向变化的明确界限,但这仍然存在。请注意,您稍后在 Click 等上设置的任何界限都会被遵守。

我还上传了一个版本的应用程序,其中包含 2 个单独的活动来说明该问题。

  1. 简单的普通活动只是说明了这个问题。
  2. 使用自定义BitmapDrawable的活动ImageView- 每当更改边界时,都会打印以记录。

第二个版本清楚地表明,即使我在 Drawable 上设置了界限,这些界限在onBoundsChange().


原始问题:

我正在使用一个FrameLayout2 ImageViews 堆叠在另一个之上来显示“电池状态”图形。这是纵向模式。在横向模式下,我显示不同的布局(图表)。

我的问题是——假设正在显示电池状态——比如 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:

  1. 在 ValueIndicator.java 中,我引入了一个成员变量mPercent ,它始终存储最后一个已知的百分比值。
  2. 更新了setPercent()代码以更新成员变量mPercent;然后调用updateIndicateUi()方法。
  3. updateIndicatorUi()(现在是一种public方法)现在使用状态(即mPercent)来完成它的工作。
  4. 每当我们回到纵向模式时,我都会调用updateIndicatorUi(). 这会强制电池图形自行更新。

我还更新了代码以反映这些更改。

这个想法是每当我们从横向模式返回纵向模式时强制重绘和无效。再次 - 我确实看到根据需要设置了可绘制的电池“内容”的边界,但 UI 拒绝跟上步伐。

4

3 回答 3

2

我已经检查了您在 Google Code Hosting 上的代码(感谢您为如此彻底地记录代码所做的努力),我发现当您从横向返回纵向时,Drawable 上设置的边界确实再次发生了变化。

Drawable 的边界不是由您的代码更改,而是由ImageView的布局方法更改。当您放置一个新布局 ( setContentView) 时,所有布局代码都会运行,包括ImageView's. ImageView 更改它包含的可绘制对象的边界,这就是为什么您将可绘制对象的边界更改为原始边界。

导致绑定更改的堆栈跟踪是:

Thread [<1> main] (Suspended (entry into method onBoundsChange in BitmapDrawable))  
    BitmapDrawable.onBoundsChange(Rect) line: 293   
    BitmapDrawable(Drawable).setBounds(int, int, int, int) line: 131    
    ImageView.configureBounds() line: 769   
    ImageView.setFrame(int, int, int, int) line: 742    
    ImageView(View).layout(int, int, int, int) line: 7186   
    LinearLayout.setChildFrame(View, int, int, int, int) line: 1254 
    LinearLayout.layoutVertical() line: 1130    
    LinearLayout.onLayout(boolean, int, int, int, int) line: 1047   
    LinearLayout(View).layout(int, int, int, int) line: 7192    
    FrameLayout.onLayout(boolean, int, int, int, int) line: 338 
    FrameLayout(View).layout(int, int, int, int) line: 7192 
    LinearLayout.setChildFrame(View, int, int, int, int) line: 1254 
    LinearLayout.layoutVertical() line: 1130    
    LinearLayout.onLayout(boolean, int, int, int, int) line: 1047   
    LinearLayout(View).layout(int, int, int, int) line: 7192    
    PhoneWindow$DecorView(FrameLayout).onLayout(boolean, int, int, int, int) line: 338  
    PhoneWindow$DecorView(View).layout(int, int, int, int) line: 7192   
    ViewRoot.performTraversals() line: 1145 
    ViewRoot.handleMessage(Message) line: 1865  
    ViewRoot(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 130 
    ActivityThread.main(String[]) line: 3835    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 507  
    ZygoteInit$MethodAndArgsCaller.run() line: 847  
    ZygoteInit.main(String[]) line: 605 
    NativeStart.main(String[]) line: not available [native method]  

在阅读您的代码时,我发现更改边界和存储边界等只是为了画一个仪表而过大。我可以建议以下其中一项:

  1. 更改ImageView自身的大小(使用setLayoutParams)而不是其可绘制对象的边界。
  2. 不要使用 ImageView,而是创建一个扩展View和覆盖的类onDraw(Canvas),然后使用drawRect.
于 2012-02-16T05:07:02.383 回答
0

只是一个猜测,但在 RotateActivity 中,尝试将 super 调用的顺序更改为:

super.onConfigurationChanged(newConfig); // super called before initialize()
mConfig = newConfig;
initialize();
于 2012-02-11T09:37:45.033 回答
0

我通过使用一个好的旧自定义View对象解决了这个问题。在onDraw()中,我将位图绘制到Canvas,并根据电池百分比调整边界大小。在 Google Project Hosting上传了工作项目的存档这个答案帮助了我,因此我接受并授予了赏金。

尽管如此,我仍然不相信有必要为此使用自定义View。IMO,我应该可以将其ImageView放入 a 中Layout,这仍然可以工作。我只是想不出一种方法来告诉布局不要管我Drawable的 s!

此外,此电池图形最初旨在与其他图形(温度、信号强度...)一起使用 2 列表格布局。因此,该解决方案对这些情况的适用性还有待观察。

另外,我的解决方案有一个小错误——第一次电池总是空的。当我找到解决方案时,我会将解决方案上传到同一个项目。

于 2012-02-21T08:06:30.797 回答