Graphics.java
Upload User: xmjingguan
Upload Date: 2009-07-06
Package Size: 2054k
Code Size: 2k
Category:

android

Development Platform:

Java

  1. /***
  2.  * Excerpted from "Hello, Android!",
  3.  * published by The Pragmatic Bookshelf.
  4.  * Copyrights apply to this code. It may not be used to create training material, 
  5.  * courses, books, articles, and the like. Contact us if you are in doubt.
  6.  * We make no guarantees that this code is fit for any purpose. 
  7.  * Visit http://www.pragmaticprogrammer.com/titles/eband for more book information.
  8. ***/
  9. package org.example.graphics;
  10. import android.app.Activity;
  11. import android.content.Context;
  12. import android.graphics.Canvas;
  13. import android.graphics.Color;
  14. import android.graphics.Paint;
  15. import android.graphics.Path;
  16. import android.graphics.Path.Direction;
  17. import android.os.Bundle;
  18. import android.view.View;
  19. public class Graphics extends Activity {
  20.    @Override
  21.    public void onCreate(Bundle savedInstanceState) {
  22.       super.onCreate(savedInstanceState);
  23.       setContentView(new GraphicsView(this));
  24.    }
  25.    static public class GraphicsView extends View {
  26.       
  27.       
  28.       private static final String QUOTE = "Now is the time for all " +
  29.        "good men to come to the aid of their country.";
  30.       
  31.       private final Path circle;
  32.       private final Paint cPaint;
  33.       private final Paint tPaint;
  34.       
  35.       public GraphicsView(Context context) {
  36.          super(context);
  37.          
  38.          // Color examples
  39.          
  40.          int color = Color.BLUE; // solid blue
  41.          
  42.          
  43.          // Translucent purple
  44.          color = Color.argb(127, 255, 0, 255);
  45.          
  46.          
  47.          color = getResources().getColor(R.color.mycolor);
  48.          
  49.          
  50.          circle = new Path();
  51.          circle.addCircle(150, 150, 100, Direction.CW);
  52.          
  53.          cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  54.          cPaint.setStyle(Paint.Style.STROKE);
  55.          
  56.          cPaint.setColor(Color.LTGRAY);
  57.          
  58.          cPaint.setStrokeWidth(3);
  59.          tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  60.          tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
  61.          tPaint.setColor(Color.BLACK);
  62.          tPaint.setTextSize(20f);
  63.          // setBackgroundColor(Color.WHITE);
  64.          
  65.          setBackgroundResource(R.drawable.background);
  66.          
  67.          
  68.       }
  69.       
  70.       
  71.       @Override
  72.       protected void onDraw(Canvas canvas) {
  73.          // Drawing commands go here
  74.          
  75.          
  76.          canvas.drawPath(circle, cPaint);
  77.          canvas.drawTextOnPath(QUOTE, circle, 0, 20, tPaint);
  78.          
  79.          
  80.       }
  81.    }
  82.    
  83. }