如何使用Android的Invalidate() [英] How to use Android Invalidate()

查看:288
本文介绍了如何使用Android的Invalidate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动我已经定义了两个视图和一个菜单:

浏览次数:
1.自定义视图游戏
2.按钮BTN

菜单:
1.打开一个文件打开项目

的菜单布局在不同的活性定义

基本上,主要活动开始时,它绘制不有任何自定义视图,和按钮。然后,我使用加载菜单中打开该文件。它带来了在/ SD卡目录的文件列表,我挑选列表中的文件。然后onListItemClick()解析文件和自定义视图更新数据结构。然后我preSS回返回到主活动。现在,我希望看到pressing按钮后重绘,但它似乎并不做任何事情。我已经实现了OnClickListener()的按钮,并调用game.invalidate()。但它似乎并没有在点击按钮立即重绘自定义视图。我不得不退出程序(返回键),然后重新打开应用程序看到重绘。我读过关于无效(其他职位)不会立即重绘视图。它只是它推到一个队列,一旦它是可以这样做Android将会采取行动。我想我不明白的是,在我的应用程序是什么,从采取行动持的Andr​​oid。

code的片段

  //主要活动
公共类someActivity延伸活动{someView MyView的;@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
     super.onCreate(savedInstanceState);
     的LinearLayout布局=新的LinearLayout(本);
     layout.setOrientation(LinearLayout.VERTICAL);
     按钮BTN1 =新按钮(本);
     btn1.setOnClickListener(向前);
     MyView的=新someView(本);
     layout.addView(MyView的);
     layout.addView(BTN1);
     的setContentView(布局);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.menu,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项)
{
    开关(item.getItemId())
    {
    案例R.id.open:
        意图myIntent =新的Intent();
        myIntent.setClass(这一点,DirectoryBrowser.class);
        startActivityForResult(myIntent,0);
        返回true;
    案例R.id.save:
        返回true;
    默认:
        返回super.onOptionsItemSelected(项目);    }
}私人OnClickListener前锋=新OnClickListener()
{
    公共无效的onClick(视图v)
    {
        myView.invalidate();    }
};// DirectoryBrowser活动
公共类DirectoryBrowser扩展ListActivity
{
私人列表<串GT;项=无效;@覆盖
公共无效的onCreate(捆绑冰柱){
    super.onCreate(冰柱);
    的setContentView(R.layout.open);
    的GetFiles(新文件(/ SD卡)listFiles());
}@覆盖
保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    INT selectedRow =(int)的ID;
    如果(selectedRow == 0){
        的GetFiles(新文件(/ SD卡)listFiles());
    }其他{
        档案文件=新的文件(items.get(selectedRow));
        如果(file.isDirectory()){
            的GetFiles(file.listFiles());
        }其他{
                尝试
                {
                                         //解析文件和更新myView.data
                                    }
                赶上(FileNotFoundException异常E)
                {
                    Log.e(ERROR,找不到文件);
                }
        }
    }
}//自定义视图someView
// ...@覆盖
公共无效的onDraw(帆布油画)
{
         //此处查看重绘}


解决方案

我见过2的活动,所以我猜 postInvalidate()可能是你在找什么

<一个href=\"http://developer.android.com/reference/android/view/View.html#invalidate%28%29\">http://developer.android.com/reference/android/view/View.html#invalidate%28%29
<一href=\"http://developer.android.com/reference/android/view/View.html#postInvalidate%28%29\">http://developer.android.com/reference/android/view/View.html#postInvalidate%28%29

编辑:
<一href=\"http://stackoverflow.com/questions/1458047/why-isnt-view-invalidate-immediately-redrawing-the-screen-in-my-android-game\">Why是不是view.invalidate立即刷新屏幕在我的Andr​​oid游戏
这里是一个intresting线程,你见过吗?

In my main activity I have defined two views and a menu:

Views: 1. Custom view game 2. Button btn

Menu: 1. Open item for opening a file

The menu layout is defined in a different activity

Basically, when the main activity starts, it draws the custom view that doesn't have anything, and the button. Then I load the file using the open in the menu. It brings up a list of files in the /sdcard dir and I pick the file from the list. Then the onListItemClick() parses the file and updates the data structure in the custom view. Then I press back to return to the main activity. Now I'd expect to see the redraw after pressing the button but it doesn't seems to do anything. I've implemented the OnClickListener() for the button and call the game.invalidate(). But it doesn't seem to redraw the custom view immediately upon button click. I have to exit the app( back button) then reopen the app to see the redraw. I've read other posts regarding the invalidate() not redrawing the view immediately. It only pushes it to a queue and Android will take action once it's free to do so. I guess thing I don't understand is that what in my app is holding Android from taking action.

Snippets of code

//Main Activity
public class someActivity extends Activity {

someView myView;

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     LinearLayout layout = new LinearLayout(this);
     layout.setOrientation(LinearLayout.VERTICAL);
     Button btn1 = new Button(this);
     btn1.setOnClickListener(forward);
     myView = new someView(this);
     layout.addView(myView);
     layout.addView(btn1);
     setContentView(layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.open:
        Intent myIntent = new Intent();
        myIntent.setClass(this, DirectoryBrowser.class);
        startActivityForResult(myIntent,0);
        return true;
    case R.id.save:
        return true;
    default:
        return super.onOptionsItemSelected(item);

    }
}

private OnClickListener forward = new OnClickListener()
{
    public void onClick(View v)
    {
        myView.invalidate();

    }
};

//DirectoryBrowser Activity
public class DirectoryBrowser extends ListActivity 
{
private List<String> items = null;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.open);
    getFiles(new File("/sdcard").listFiles());
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id){
    int selectedRow = (int)id;
    if(selectedRow == 0){
        getFiles(new File("/sdcard").listFiles());
    }else{
        File file = new File(items.get(selectedRow));
        if(file.isDirectory()){
            getFiles(file.listFiles());
        }else{
                try
                {
                                         //Parse file and update myView.data
                                    }
                catch (FileNotFoundException e) 
                {
                    Log.e("ERROR","File Not Found");
                }
        }
    }
}

//Custom View someView
//...

@Override
public void onDraw(Canvas canvas) 
{
         //Redraw view here

}

解决方案

I've seen 2 activities, so I guess postInvalidate() may is what you are looking for.

http://developer.android.com/reference/android/view/View.html#invalidate%28%29 http://developer.android.com/reference/android/view/View.html#postInvalidate%28%29

Edit: Why isn't view.invalidate immediately redrawing the screen in my android game here is an intresting thread, have you seen it?

这篇关于如何使用Android的Invalidate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆