gpt4 book ai didi

Android invalidate(Rect) 使整个区域无效

转载 作者:行者123 更新时间:2023-11-29 13:59:10 24 4
gpt4 key购买 nike

我不明白为什么 invalidate(Rect) 会使整个区域无效。该区域分为 5 个部分,每个部分都绘制了一个折线图。每个部分包含 100 分。当数据到达时间 tn 到 tn+100 时,我调用 invalidate(new Rect(left, top, right bottom)) ,其中 top 是屏幕顶部的高度(但数值低于底部)。这会调用 onDraw() 方法。从 tn 到 tn+100 的区域被绘制,但是之前在区域 tn-100 到 tn 中绘制的线段被删除。它永远这样下去。每个 invalidate 只绘制正确的区域(因为这是我仅有的数据),但所有先前绘制的数据都被删除。所以我得到了行进 fragment !

换句话说,如果我调用 invalidate() 或 invalidate(Rect),我会得到相同的行为。

我假设 Rect() 的参数是像素,并且根据绘制它的 AlertDialog 窗口的高度和宽度获取值。

希望最终减少“Rect()”的区域,这样我就可以模拟实时绘图,并且只使时间步长 t 到 t+1 而不是区域无效。

我一定是在做傻事。

我希望它是在 AlertDialog 中完成的事实不是问题。

这部分是为了帮助“android 开发者”帮助像我这样的菜鸟做对。首先是事件的顺序:
1.回调中通过蓝牙接收数据
2. 如果是正确的数据类型,主 Activity (UI 线程)中的 BroadcastReceiver 会发出信号,并从那里调用一个例程来设置 WaveFormView 扩展 View 类的参数,然后调用 ShowDialog(id) 调用主 Activity 中的 onCreateDialog(id) 回调。
3. 然后调用 invalidate()。
4. 弹出对话框,绘制图形。
5. 所有后续调用 ShowDialog(id) 绕过 onCreateDialog(id)

这可行,但无论参数如何,整个区域总是无效的。这里也没有用户事件。从你的例子中我能想到的最好的是下面我在 onShow() 中放置 invalidate 而不是在 showDialog(id) 之后调用自己

@Override
protected Dialog onCreateDialog(int id)
{
Log.d(TAG, "Alert Dialog 'onCreateDialog' method has been called with id " + id);
Builder bldr = new AlertDialog.Builder(this);
AlertDialog alert = bldr.setView(waveForm).setNegativeButton("Dismiss " + id,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
}).create();
// I tried adding this and invalidating here worked only first pass
alert.setOnShowListener(
new DialogInterface.OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
// call to invalidate
waveForm.drawArea();
}
});

//alert.getWindow().setLayout(alert.getWindow().getAttributes().width, alert.getWindow().getAttributes().height / 2);
alert.getWindow().setLayout(waveForm.getCurrentWidth(), waveForm.getCurrentHeight());

return alert;
}

但是不会调用 onShow()。

主 Activity 中调用showDialog的方法是

private void displayRtsa(int[] rtsaReceived)
{
// rtsaReceived[0] has the agentsink hash code
int agent = rtsaReceived[0];
// rtsaReceived[1] has the index of the RtsaData object updated
int index = rtsaReceived[1];
TreeMap<Integer, RtsaData[]> agentRtsa = BluetoothPanService.getAgentRtsaMap();
RtsaData[] rtsaDataValues = agentRtsa.get(agent);

int dialogId = 0;
synchronized(dialogIds)
{
int i = 0;
if(dialogIds.containsKey(agent + index) == false)
{
for(i = 0; i < dialogIds.size(); i++)
{
if(dialogIds.containsValue(i) == false)
{
break;
}
}
dialogIds.put(agent + index, i);
}
dialogId = dialogIds.get(agent + index);
}
final int id = dialogId;
Log.d(TAG, "Dialog id being shown = " + dialogId);
waveForm.setPeriod(rtsaDataValues[index].getPeriod());
waveForm.setMaxMin(rtsaDataValues[index].getMinValue(), rtsaDataValues[index].getMaxValue());
waveForm.setColor(Color.argb(255, 255, 200, 0));
waveForm.setData(rtsaDataValues[index].getData());
waveForm.setTitle(rtsaDataValues[index].getType());
showDialog(id);
// invalidate
// waveForm.drawArea(); (try to do in onCreateDialog callback)
}

这可能是一个完全错误的方法。可能 openGl 是唯一的方法。

顺便说一句,谢谢你容忍我!

最佳答案

我认为这取决于您对 .如果您调用失效的 View 没有处理矩形失效,则会发生默认失效。

无论如何,如果您想改变行为,您可以自己改变。对于“onDraw”方法,使用下一个代码来获取无效的矩形:

public class InvalidateTestActivity extends Activity
{
static class CustomView extends ImageView

{

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

@Override
protected void onDraw(Canvas canvas)
{
final Rect r = canvas.getClipBounds();
Log.d("DEBUG", "rectangle of invalidation:" + r);
super.onDraw(canvas);
}

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
CustomView customView = new CustomView(this);
customView.setLayoutParams(new FrameLayout.LayoutParams(200, 200));
customView.setBackgroundColor(0xffff0000);
customView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{
v.invalidate(new Rect(0, 0, 49, 49));
}
});
setContentView(customView);
}
}

关于Android invalidate(Rect) 使整个区域无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10423035/

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