【问题标题】:How do I draw certain views after others?我如何在其他人之后绘制某些观点?
【发布时间】:2013-02-22 13:18:57
【问题描述】:

我有一个简单的LinearLayout,只包含一个扩展TextView 的自定义视图(我将开始调用它“IdiomView”)和一个ListViewIdiomView 与普通 TextView 的唯一区别是我重写了 onDraw() 方法以迭代地减小文本大小,直到文本长度小于 3 行。我的问题是,当绘制视图时,用户会看到:

 ______________
|__ACTION_BAR__|
|  IdiomView   |
|______________|
|              |
|   ListView   |
|              |
|              |
|______________|

很快就变成了:

 ______________
|__ACTION_BAR__|
|__IdiomView __|
|              |
|   ListView   |
|              |
|              |
|              |
|______________|

即ListView 被绘制,然后在IdiomView 整理好它的大小后跳起来。

我想要的是等到我的IdiomView 完全绘制后,再绘制 ListView。这个帖子What event is fired after all views are fully drawn? 解释了如何在绘图完成后通过调用View.post(Runnable) 来排列线程。问题是我重写的onDraw() 方法多次调用onDraw() 以计算较小的文本是否覆盖少于3 行,所以这个元素可能在我希望ListView 出现之前多次“完成绘图” .

感谢所有 cmets 和答案。这是我当前的代码:

布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@color/off_white"
    android:orientation="vertical" >

    <carter.cwords.idioms.IdiomView
        android:id="@+id/idiom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:textColor="@color/transparent"
        android:textSize="28sp"
        android:textStyle="italic"
        android:visibility="invisible" />

    <ListView
        android:id="@+id/quote_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:choiceMode="none"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:visibility="invisible" />

</LinearLayout>

活动:

private IdiomView mIdiomTextView;
private ListView mQuoteList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.idiom_of_the_day);
    mIdiomTextView = (IdiomView) findViewById(R.id.idiom);
    mQuoteList = (ListView) findViewById(R.id.quote_list);      

    // Populate page data onResume()
}

@Override
protected void onResume() {
    super.onResume();

    sendRequest(R.string.url_idiom_of_the_day, new AfterRequest(){
        @Override
        public void useResults(Document resultXml) {
            if(resultXml != null){

                Log.i(getClass().getSimpleName(), "useResults()");

                String idiomString = XmlUtilities.getTextValue(resultXml, NetworkHelper.XML_TAG_IDIOM_CONTENT);

                logDebug("idiomString: " + idiomString);
                mIdiomTextView.setText("\"" + idiomString + "\"");
                mQuoteList.setAdapter(new ContentAdapter(mContext, resultXml));
                mIdiomTextView.setVisibility(View.VISIBLE);

                mIdiomTextView.post(new Runnable(){
                    @Override
                    public void run() {
                        mQuoteList.setVisibility(View.VISIBLE);
                    }
                });
            }

        }
    });

}

IdiomView:

public class IdiomView extends TextView {

    public IdiomView(Context context) {     
        super(context);
    }

    public IdiomView(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(getClass().getSimpleName(), "onDraw(): " + this.getLineCount());
        if(this.getLineCount() > 2){
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.getTextSize()-1);
        }
        else{
            this.setTextColor(getResources().getColor(R.color.text));
        }
        Log.i(getClass().getSimpleName(), "onDraw(): " + this.getLineCount());

    }


}

非常感谢。

【问题讨论】:

  • 您可以通过使用getPaint() 方法来避免重复设置文本大小(并同时重绘小部件)。使用 Paint 在循环中测量文本的宽度(具有不同的文本大小),直到您看到哪个值适合可用的最大宽度(TextView)两次,并将该直接值用于文本尺寸
  • 感谢 Luksprog。这就是我应该解决问题的方式。 This thread 提供有关测量 TextViews 的详细信息。

标签: java android android-layout android-custom-view


【解决方案1】:

我之前从未在View 上尝试过此操作,但我认为它可以帮助您在绘制IdiomView 之后引导您的应用仅使用ListView。您也许可以使用侦听器来实现这一点。但是,它需要您使用代码隐藏方法。

IdiomView中创建一个接口如

public static interface OnDrawComplete {
    public abstract void onDrawComplete();
}

private OnDrawComplete mListener;

public IdiomView(Context context) {     
    super(context);
    // this forces it to make sure it's actually an activity
    // context so you can get a listener
    if (context instanceof Activity)
        this.mListener = context;
}

现在在你的绘图结束时,调用它

if (this.mListener != null)
    this.mListener.onDrawComplete();

这将调用创建对象的 Activity 并通知它正在绘制。所以在你的 Activity 中,你实现了接口

public void onDrawComplete() {
    // After the draw completes, it calls this callback.
    // setup the rest of your layout now
    mQuoteList = (ListView) findViewById(R.id.quote_list);
    this.populateQuoteList();
}

您也可以在其他某种View 事件中调用this.mListener.onDrawComplete();,但我找不到合适的。你提到了View.post(runnable),它可能有用,但我也从未使用过那种东西。

最大的潜在问题是我不知道IdiomView 将如何接收上下文或哪个上下文。在这种情况下,您需要实现接口的Activity 上下文。

如果没有得到正确的上下文,可能需要手动实例化并将其添加到布局中才能使用此方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mIdiomTextView = new mIdiomTextView(this);
    // add to the Layout manually.  
}

这里有更多的充实

IdiomView

public class IdiomView extends TextView {
    public static interface OnDrawComplete {
        public abstract void onDrawComplete();
    }

    private OnDrawComplete mListener;

    public IdiomView(Context context) {     
        this(context, 0);
    }

    public IdiomView(Context context, AttributeSet attrs){
        super(context, attrs);
        if (context instanceof Activity)
            this.mListener = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // do your drawing code here
        // ...
        // after drawing, call the listener, which tells your
        // Activity to deal with the ListView.
        if (this.mListener != null)
            this.mListener.onDrawComplete();
    }
}

活动

public class MyActivity extends Activity implements IdiomView.OnDrawComplete {
    //...

    private IdiomView mIdiomTextView;
    private ListView mQuoteList;

    public void onDrawComplete() {
        // After the draw completes, it calls this callback.
        // setup the rest of your layout now
        mQuoteList = (ListView) findViewById(R.id.quote_list);
        this.populateQuoteList();
        // now show the ListView
        mQuoteList.setVisibility(View.Visible);
    }

    private void populateQuoteList() {
        // populate your ListView here
    }
}

【讨论】:

  • 这是一个绝妙的答案,完美地回答了我提出的问题。然而,在实现了这个答案之后,我计算了我的IdiomView 完成绘图需要多长时间,几乎是半秒。我问的问题很糟糕,其他有同样问题的人应该看看 Luksprog 的评论(上图)谁的方法大约需要 50 毫秒。
  • 很高兴您修复了它。看看 Luksprog 是否会将其添加为答案,以便您接受。
【解决方案2】:

重复设置文本大小直到找到合适的字体大小可能效率很低,因为您的TextView 每次都需要重新测量和重新绘制。您可以使用其TextView.getPaint() 方法仅测量和设置一次文本,而不是这样做。您可以在循环中使用Paint 测量文本,为其提供不同的字体大小,直到Paint.measureText() 返回的值低于小部件当前宽度的两倍。

【讨论】:

  • 循环直到宽度小于最大宽度的两倍,如果较长的单词绕到三分之一,仍然可以跨越三行,所以我只是用最大高度来测量。
  • @WilliamCarter 我忘了,你仍然可以测量宽度,但需要额外检查一半文本的位置(在一个单词或空白处)并从那里进行一些调整.
猜你喜欢
  • 2020-10-16
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
相关资源
最近更新 更多