From Canvas to Bitmap to improve rendering
I'm going crazy around this problem about days.
I've created a custom View that in onDraw() method draws some shapes and text. Because the drawn dimension is bigger than the custom view, I had to override onMeasure() method to implement scrolling.
Activity works fine, scrolling is OK, but because in onDraw() method some big operations are executed, navigation in Activity is very slow. These operations must be executed in on Draw() method. Scroll causes the redrawn of the View so onDraw() method is called frequently.
The content of custom View changes dynamically, it depends by some selections made by the user through precedent Activity.
So I've thought to convert my Canvas into a Bitmap but there are some problem.
1) I can't know width and height parameters for Bitmap.createBitmap() before creating the Canvas. I can know them after onDraw() method. So I can't associate a Bitmap to the Canvas.
2) If I try to set the dimension of the bitmap manually it throws an OutOfMemory exception: my Canvas is very bigger due by the size of the shapes.
So, what can I have to do?
The onDraw() method:
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if(!listaTl.isEmpty())
{
canvas.drawColor(Color.WHITE);
p.setStyle(Paint.Style.FILL_AND_STROKE);
p.setAntiAlias(true);
p.setStrokeWidth(1);
for(TimelineGrafica t : listaTl)
{
if(inseritaLaPrima)
y = ySalvata + this.yAngoloDestroGiu + DISTANZA_FRA_TIMELINE;
p.setColor(t.getColor());
disegnaPunta(canvas,p,t);
disegnaRettangolo(canvas,p,t);
disegnaGrain(canvas,p,t);
disegnaFatti(canvas,p,t);
inseritaLaPrima = true;
}
y = ySalvata;
inseritaLaPrima = false;
}
requestLayout();
}
Methods "disegna*" contain some canvas.draw*() statements. Here the XML file associates to the Activity that contains the Custom View.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:overScrollMode="always"
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:overScrollMode="always"
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"
>
<AppAndroid.RappresentazioneTimeline.TimelineView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical|horizontal"
/>
</HorizontalScrollView>
</ScrollView>
链接地址: http://www.djcxy.com/p/67626.html
上一篇: 在Android Studio中通过onDraw方法在第一个位图上绘制第二个位图
下一篇: 从画布到位图以改善渲染