Android view.invalidate()的奇怪行为

时间:2015-01-01 09:13:24

标签: android

我有3个自定义视图垂直放置在LinearLayout中,它们用于显示不同的动态信息,因此它们应该在不同时间失效并重新绘制。但我发现视图失效超出了通常的期望,即:如果您使顶视图无效,则所有3个视图同时失效,如果您使中间视图无效,则中间和底部视图无效,顶部视图不是,如果你使底部视图无效,只有底部视图本身无效,这就是我想要的,那么前两种情况会发生什么?我搜索并得到类似的问题:

https://stackoverflow.com/questions/26192491/invalidate-one-view-force-other-views-invalidate-too-how-separating-that

Android Invalidate() only single view

但似乎没有确切的答案。我在这里发布我的代码,任何评论都表示赞赏。


TestView.java

package com.vrb.myview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;


public class TestView extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onTest(View view){
        MyView1 mv1 = (MyView1)findViewById(R.id.mv1);
        MyView1 mv2 = (MyView1)findViewById(R.id.mv2);
        MyView1 mv3 = (MyView1)findViewById(R.id.mv3);

        mv1.invalidate(); // all 3 views are invalidated
    //  mv2.invalidate(); // mv2 and mv3 are invalidated
    //  mv3.invalidate(); // only mv3 is invalidated,this is what I want
    }
}

MyView1.java

package com.vrb.myview;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView1 extends View {
    Rect rc=null;
    Paint p=null;
    Random r;
    public MyView1(Context ctx){
        super(ctx);
        rc = new Rect();
        p = new Paint();
        r = new Random();
    }
    public MyView1(Context ctx, AttributeSet set){
        super(ctx, set);
        rc = new Rect();
        p = new Paint();
        r = new Random();
    }

    public void onDraw(Canvas canvas){
        if(canvas.getClipBounds(rc)){
            Log.d("MyView1","id="+getId()+" Rect: "+rc.left+","+rc.top+","+rc.right+","+rc.bottom);
            p.setColor(Color.argb(0xff, Math.abs(r.nextInt())%255, Math.abs(r.nextInt())%255, Math.abs(r.nextInt())%255));
            canvas.drawRect(rc, p);
        }else{
            Log.d("MyView1","id="+getId()+" Rect=null");        
        }       
    }
}

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root"
    android:orientation="vertical"
    tools:context="com.vrb.myview.TestView" >

    <com.vrb.myview.MyView1
        android:layout_width="match_parent"
        android:layout_height="100px"
        android:id="@+id/mv1" />

    <com.vrb.myview.MyView1
        android:layout_width="match_parent"
        android:layout_height="100px"
        android:id="@+id/mv2" />

    <com.vrb.myview.MyView1
        android:layout_width="match_parent"
        android:layout_height="100px"
        android:id="@+id/mv3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Invalidate"
        android:onClick="onTest"
        android:id="@+id/btn" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

对于视图的内部状态,您不应该依赖onDraw()的调用计数或时间。将p.setColor()调用移至单独的公共方法,并在其末尾调用invalidate()。例如:

public class MyView1 extends View {
    ...
    public void changePaint() {
        p.setColor(Color.argb(0xff, Math.abs(r.nextInt()) % 255, Math.abs(r.nextInt()) % 255, Math.abs(r.nextInt()) % 255));
        invalidate();
    }
}

然后在您的onTest()方法中:

public void onTest(View view) {
    MyView1 mv1 = (MyView1)findViewById(R.id.mv1);
    ...
    mv1.changePaint();
    ...
}