通过创建一个位图的XY Chart来学习Android绘图类Rect,Paint,Bitmap,Canvas(附源码)
图形界面我们通过Xml定义。
1.XML的GUI布局?
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
??? xmlns:android="http://schemas.android.com/apk/res/android"
??? android:layout_height="fill_parent"
??? android:layout_width="fill_parent"
??? android:background="#4B088A">
?
??? <TableRow android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
???? android:padding="20px">
??
???? <TextView?
android:id="@+id/some"
??? android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
??? android:text="Some layout? items here"
??? android:layout_marginLeft="10px"
??? android:layout_marginRight="10px"
??? android:textColor="#ff8000"
??? android:textStyle="bold"
??? />
??? </TableRow>
<View????
? android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FFE6E6E6"
/>
???
??? <TableRow>
??? <ImageView?
android:id="@+id/testy_img"
android:layout_marginLeft="20px"
android:padding="20px"
??? />
?? </TableRow>
<View????
? android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FFE6E6E6" />
??? <TableRow android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
???? android:padding="20px">
???? <TextView?
android:id="@+id/more"
??? android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
??? android:text="More layout items here"
??? android:layout_marginLeft="10px"
??? android:layout_marginRight="10px"
?? android:textColor="#ff8000"
??? android:textStyle="bold"
??? />
??? </TableRow>
</TableLayout>
?
这个布局文件是TableLayout布局,它定义了三行,行之间通过一条线割开
?
2.图表的实现
为了实现我们的图表,我们首先创建一个位图,然后关联到我们的布局文件,有了位图,我们就可以绘制图表,做缩放,色彩和数据显示?等效果。
?
2.1绘制位图
首先我们使布局连接到XML对象的,那么我们创建位图。我们通过quicky_XY方法来实现所有的绘制,最后显示在屏幕上。
public void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.main);
??ImageView image = (ImageView) findViewById(R.id.testy_img);
??Bitmap emptyBmap = Bitmap.createBitmap(250, 200, Config.ARGB_8888);
??int width = emptyBmap.getWidth();
??int height = emptyBmap.getHeight();
??Bitmap charty = Bitmap.createBitmap(width, height,
????Bitmap.Config.ARGB_8888);
??charty = quicky_XY(emptyBmap);
??image.setImageBitmap(charty);
?}
?
2.2绘制网格
有了位图后,将它与Canvas相关联
Canvas canvas =
?????x_guide - 2, drawSizes[1] - 15, paint);
???break;
??default:
???thisDrawingArea.drawText("? " + cur_label + " - " + cur_units,
?????x_guide - 2 - 30, drawSizes[1] - 15, paint);
???break;
??}
?}
?private static int getCurTextLengthInPixels(Paint this_paint,
???String this_text) {
??FontMetrics fp = this_paint.getFontMetrics();
??Rect rect = new Rect();
??this_paint.getTextBounds(this_text, 0, this_text.length(), rect);
??return rect.width();
?}
?public static double get_ceiling_or_floor(double this_val, boolean is_max) {
??double this_min_tmp;
??int this_sign;
??int this_10_factor = 0;
??double this_rounded;
??if (this_val == 0.0) {
???this_rounded = 0.0;
???return this_rounded;
??}
??this_min_tmp = Math.abs(this_val);
??if (this_min_tmp >= 1.0 && this_min_tmp < 10.0)
???this_10_factor = 1;
??else if (this_min_tmp >= 10.0 && this_min_tmp < 100.0)
???this_10_factor = 10;
??else if (this_min_tmp >= 100.0 && this_min_tmp < 1000.0)
???this_10_factor = 100;
??else if (this_min_tmp >= 1000.0 && this_min_tmp < 10000.0)
???this_10_factor = 1000;
??else if (this_min_tmp >= 10000.0 && this_min_tmp < 100000.0)
???this_10_factor = 10000;
??// 'cover when min is pos and neg
??if (is_max) {
???if (this_val > 0.0)
????this_sign = 1;
???else
????this_sign = -1;
??} else {
???if (this_val > 0.0)
????this_sign = -1;
???else
????this_sign = 1;
??}
??if (this_min_tmp > 1)
???this_rounded = (double) (((int) (this_min_tmp / this_10_factor) + this_sign) * this_10_factor);
??else {
???this_rounded = (int) (this_min_tmp * 100.0);
???// ' cover same as above bfir number up to .001 less than tha it
???// will skip
???if (this_rounded >= 1 && this_rounded < 9)
????this_10_factor = 1;
???else if (this_rounded >= 10 && this_rounded < 99)
????this_10_factor = 10;
???else if (this_rounded >= 100 && this_rounded < 999)
????this_10_factor = 100;
???this_rounded = (double) (((int) ((this_rounded) / this_10_factor) + this_sign) * this_10_factor);
???this_rounded = (int) (this_rounded) / 100.0;
??}
??if (this_val < 0)
???this_rounded = -this_rounded;
??return this_rounded;
?}
}
?
?