My android plotting learning notes (1)

Source: Internet
Author: User
Tags drawtext

2010-11-04

I plan to draw a coordinate axis, draw a line chart, and draw a coordinate axis with a straight line at the beginning, but it is not very nice.

First paste the code

/** Testpaint. Java **/

package com.test;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Bundle;import android.view.View;public class testPaint extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  MyView myview = new MyView(this);  setContentView(myview); } private class MyView extends View {  @Override  protected void onDraw(Canvas canvas) {   super.onDraw(canvas);   canvas.drawColor(Color.WHITE);   Paint paint =new Paint();   paint.setAntiAlias(true);   paint.setColor(Color.BLACK);   Paint paint1=new Paint();   paint1.setColor(Color.RED);   canvas.drawLine(30, 160, 300, 160, paint);   canvas.drawLine(30, 160, 30, 30, paint);   canvas.drawLine(30, 30, 24, 36, paint);   canvas.drawLine(30, 30, 36, 36, paint);   canvas.drawLine(300, 160, 294, 154, paint);   canvas.drawLine(300, 160,294, 166, paint);   paint.setTextSize(12);   canvas.drawText(getResources().getString(R.string.date), 280, 180,     paint);   canvas.drawText(getResources().getString(R.string.product), 1, 40, paint);  }  public MyView(Context context) {   super(context);  } }}

/** Strings. xml */

<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, testpaint! </String> <string name = "app_name"> testpaint </string> <string name = "date"> day </string> <string name = "product"> output </string> </resources>

 

Running Effect

 

Then I found a polygon Filling Method to draw arrows.

 

/** Testpaint. Java */

package com.test;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.os.Bundle;import android.view.View;public class testPaint extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  MyView myview = new MyView(this);  setContentView(myview); } private class MyView extends View {  @Override  protected void onDraw(Canvas canvas) {   super.onDraw(canvas);   canvas.drawColor(Color.WHITE);   Paint paint =new Paint();   paint.setAntiAlias(true);   paint.setColor(Color.BLACK);   Paint paint1=new Paint();   paint1.setColor(Color.RED);   canvas.drawLine(30, 160, 300, 160, paint);   canvas.drawLine(30, 160, 30, 30, paint);   Path path=new Path();   path.moveTo(30, 30);   path.lineTo(25,35);   path.lineTo(30, 25);   path.lineTo(35, 35);   canvas.drawPath(path, paint);      Path path1=new Path();   path1.moveTo(300, 160);   path1.lineTo(295,155);   path1.lineTo(305, 160);   path1.lineTo(295, 165);   canvas.drawPath(path1, paint);      paint.setTextSize(12);   canvas.drawText(getResources().getString(R.string.date), 280, 180,     paint);   canvas.drawText(getResources().getString(R.string.product), 1, 40, paint);  }  public MyView(Context context) {   super(context);  } }}

 

Running Effect

 

This looks much better, haha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.