EX07_02.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:5k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex07_02;
  2. /* import相关class */
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.LinearGradient;
  8. import android.graphics.Paint;
  9. import android.graphics.Path;
  10. import android.graphics.RectF;
  11. import android.graphics.Shader;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. public class EX07_02 extends Activity
  15. {
  16.   @Override
  17.   public void onCreate(Bundle savedInstanceState)
  18.   {
  19.     super.onCreate(savedInstanceState);
  20.     /* 设置ContentView为自定义的MyView */
  21.     MyView myView = new MyView(this);
  22.     setContentView(myView);
  23.   }
  24.   
  25.   /* 自定义继承View的MyView */
  26.   private class MyView extends View
  27.   {
  28.     public MyView(Context context)
  29.     {
  30.       super(context);
  31.     }
  32.     /* 覆盖onDraw() */
  33.     @Override
  34.     protected void onDraw(Canvas canvas)
  35.     {
  36.       super.onDraw(canvas);
  37.       /* 设置背景为白色 */
  38.       canvas.drawColor(Color.WHITE);
  39.       
  40.       Paint paint = new Paint();
  41.       /* 去锯齿 */
  42.       paint.setAntiAlias(true);
  43.       /* 设置paint的颜色 */
  44.       paint.setColor(Color.RED);
  45.       /* 设置paint的style为STROKE:空心的 */
  46.       paint.setStyle(Paint.Style.STROKE);
  47.       /* 设置paint的外框宽度 */
  48.       paint.setStrokeWidth(3);
  49.       
  50.       /* 画一个空心圆形 */
  51.       canvas.drawCircle(40,40,30, paint);
  52.       /* 画一个空心正方形 */
  53.       canvas.drawRect(10,90,70,150,paint);
  54.       /* 画一个空心长方形 */
  55.       canvas.drawRect(10,170,70,200,paint);
  56.       /* 画一个空心椭圆形 */
  57.       RectF re=new RectF(10,220,70,250);
  58.       canvas.drawOval(re, paint);
  59.       /* 画一个空心三角形 */
  60.       Path path = new Path();
  61.       path.moveTo(10,330);
  62.       path.lineTo(70,330);
  63.       path.lineTo(40,270);
  64.       path.close();
  65.       canvas.drawPath(path, paint);
  66.       /* 画一个空心梯形 */
  67.       Path path1 = new Path();
  68.       path1.moveTo(10,410);
  69.       path1.lineTo(70,410);
  70.       path1.lineTo(55,350);
  71.       path1.lineTo(25,350);
  72.       path1.close();
  73.       canvas.drawPath(path1, paint);
  74.       
  75.       /* 设置paint的style为FILL:实心 */
  76.       paint.setStyle(Paint.Style.FILL);
  77.       /* 设置paint的颜色 */
  78.       paint.setColor(Color.BLUE);
  79.       
  80.       /* 画一个实心圆 */
  81.       canvas.drawCircle(120, 40, 30, paint);
  82.       /* 画一个实心正方形 */
  83.       canvas.drawRect(90,90,150,150,paint);
  84.       /* 画一个实心长方形 */
  85.       canvas.drawRect(90,170,150,200,paint);
  86.       /* 画一个实心椭圆形 */
  87.       RectF re2=new RectF(90,220,150,250);
  88.       canvas.drawOval(re2, paint);
  89.       /* 画一个实心三角形 */
  90.       Path path2 = new Path();
  91.       path2.moveTo(90,330);
  92.       path2.lineTo(150,330);
  93.       path2.lineTo(120,270);
  94.       path2.close();
  95.       canvas.drawPath(path2, paint);
  96.       /* 画一个实心梯形 */
  97.       Path path3 = new Path();
  98.       path3.moveTo(90,410);
  99.       path3.lineTo(150,410);
  100.       path3.lineTo(135,350);
  101.       path3.lineTo(105,350);
  102.       path3.close();
  103.       canvas.drawPath(path3, paint);
  104.       /* 设置渐变色 */
  105.       Shader mShader=new LinearGradient(0, 0,100,100,
  106.           new int[]{Color.RED, Color.GREEN,Color.BLUE,Color.YELLOW},
  107.           null, Shader.TileMode.REPEAT);
  108.       paint.setShader(mShader);
  109.       
  110.       /* 画一个渐变色的圆形 */
  111.       canvas.drawCircle(200,40, 30, paint);
  112.       /* 画一个渐变色的正方形 */
  113.       canvas.drawRect(170,90,230,150,paint);
  114.       /* 画一个渐变色的长方形 */
  115.       canvas.drawRect(170,170,230,200,paint);
  116.       /* 画一个渐变色的椭圆形 */
  117.       RectF re3=new RectF(170,220,230,250);
  118.       canvas.drawOval(re3, paint);
  119.       /* 画一个渐变色的三角形 */
  120.       Path path4 = new Path();
  121.       path4.moveTo(170,330);
  122.       path4.lineTo(230,330);
  123.       path4.lineTo(200,270);
  124.       path4.close();
  125.       canvas.drawPath(path4, paint);
  126.       /* 画一个渐变色的梯形 */
  127.       Path path5 = new Path();
  128.       path5.moveTo(170,410);
  129.       path5.lineTo(230,410);
  130.       path5.lineTo(215,350);
  131.       path5.lineTo(185,350);
  132.       path5.close();
  133.       canvas.drawPath(path5, paint);
  134.       
  135.       /* 写字 */
  136.       paint.setTextSize(24);
  137.       canvas.drawText(getResources().getString(R.string.str_text1),
  138.                       240,50,paint);
  139.       canvas.drawText(getResources().getString(R.string.str_text2),
  140.                       240,120,paint);
  141.       canvas.drawText(getResources().getString(R.string.str_text3),
  142.                       240,190,paint);
  143.       canvas.drawText(getResources().getString(R.string.str_text4),
  144.                       240,250,paint);
  145.       canvas.drawText(getResources().getString(R.string.str_text5),
  146.                       240,320,paint);
  147.       canvas.drawText(getResources().getString(R.string.str_text6),
  148.                       240,390,paint);
  149.     }
  150.   }
  151. }