Androidplot - 何时测量 gridRect?
Androidplot - when does the gridRect is measured?
我正在使用 androidplot 来显示一个固定大小的图形,我已经设置了一个 BitmapShader 来匹配每个范围间隔,并且我需要将此着色器设置为始终显示在图形上,因为它是开始的。
我的问题是:
我无法使用此着色器初始化图形(我已经使用 DemoApp 作为基础对其进行了测试,并且着色器工作正常)。每次我尝试使用方法 getGridRect() 获取 GridRect 时,它 returns null,无论我在 activity 创建期间在哪里调用此函数。我只能在 activity 创建后设置 shadder,但只能使用按钮或类似的东西。
我搜索了整个源代码,但找不到此措施在 activity 的生命周期中发生的位置。
不幸的是,我无法在创建过程中使用正确的方法来测量 gridrect,但我找到了解决方法。
我能够通过获取 XYPlot 视图高度并减去图形顶部和底部填充来定义图形高度。
我的代码片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mPlot.getGraphWidget().setPaddingBottom(20);
mPlot.getGraphWidget().setPaddingTop(20);
//Added a ViewTreeObserver to be sure when the Plot was measured.
mPlot.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mPlot.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//Avoid the factory trying to scale bitmap according to Display density
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.chartbackground, options), 1, (int) mPlot.getHeight() - 40, false);
BitmapShader backgroundShader = new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Matrix shaderMatrix = new Matrix();
//This matrix references always stayed the same even if the plot changed its size.
shaderMatrix.setTranslate(49.6875f, 30.75f);
backgroundShader.setLocalMatrix(shaderMatrix);
mPlot.getGraphWidget().getGridBackgroundPaint().setShader(backgroundShader);
//Setting the background to be a little bit translucid.
mPlot.getGraphWidget().getGridBackgroundPaint().setAlpha(90);
}
});
...
}
我正在使用 androidplot 来显示一个固定大小的图形,我已经设置了一个 BitmapShader 来匹配每个范围间隔,并且我需要将此着色器设置为始终显示在图形上,因为它是开始的。
我的问题是:
我无法使用此着色器初始化图形(我已经使用 DemoApp 作为基础对其进行了测试,并且着色器工作正常)。每次我尝试使用方法 getGridRect() 获取 GridRect 时,它 returns null,无论我在 activity 创建期间在哪里调用此函数。我只能在 activity 创建后设置 shadder,但只能使用按钮或类似的东西。
我搜索了整个源代码,但找不到此措施在 activity 的生命周期中发生的位置。
不幸的是,我无法在创建过程中使用正确的方法来测量 gridrect,但我找到了解决方法。
我能够通过获取 XYPlot 视图高度并减去图形顶部和底部填充来定义图形高度。
我的代码片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mPlot.getGraphWidget().setPaddingBottom(20);
mPlot.getGraphWidget().setPaddingTop(20);
//Added a ViewTreeObserver to be sure when the Plot was measured.
mPlot.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mPlot.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//Avoid the factory trying to scale bitmap according to Display density
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.chartbackground, options), 1, (int) mPlot.getHeight() - 40, false);
BitmapShader backgroundShader = new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Matrix shaderMatrix = new Matrix();
//This matrix references always stayed the same even if the plot changed its size.
shaderMatrix.setTranslate(49.6875f, 30.75f);
backgroundShader.setLocalMatrix(shaderMatrix);
mPlot.getGraphWidget().getGridBackgroundPaint().setShader(backgroundShader);
//Setting the background to be a little bit translucid.
mPlot.getGraphWidget().getGridBackgroundPaint().setAlpha(90);
}
});
...
}