gpt4 book ai didi

android - Android view.invalidate() 的奇怪行为

转载 作者:行者123 更新时间:2023-11-30 02:27:37 25 4
gpt4 key购买 nike

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

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

Android Invalidate() only single view

但似乎没有确切的答案。我在这里发布我的代码,欢迎任何评论。


测试 View .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");
}
}
}

主.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>

最佳答案

您不应依赖调用 onDraw() 的次数或时间来了解 View 的内部状态。将 p.setColor() 调用移至单独的公共(public)方法,并在其末尾调用 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();
...
}

关于android - Android view.invalidate() 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27729558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com