android.apis.graphics 画图(引)

package com.example.android.apis.graphics;

23.TextAlign:

设置Path路径,贝赛尔曲线

  //设置Path路径
      private static void makePath(Path p) {
          p.moveTo(10, 0);
          p.cubicTo(100, -50, 200, 50, 300, 0);//贝赛尔曲线
     }

//mPos存的是字符串中每个字符的位置坐标

  private float[] buildTextPositions(String text, float y, Paint paint) {
          float[] widths = new float[text.length()];
          // initially get the widths for each char
          int n = paint.getTextWidths(text, widths);
          // now popuplate the array, interleaving spaces for the Y values X值为字符的宽
          float[] pos = new float[n * 2];
          float accumulatedX = 0;
          for (int i = 0; i < n; i++) {
              pos[i * 2 + 0] = accumulatedX;
              pos[i * 2 + 1] = y;
              accumulatedX += widths[i];
          }
          return pos;
      }

onDraw()方法中:

  p.setColor(0x80FF0000);
              canvas.drawLine(x, y, x, y + DY * 3, p);//(x,y)为基准线
              p.setColor(Color.BLACK);

              canvas.translate(0, DY);
              p.setTextAlign(Paint.Align.LEFT);
              canvas.drawText(TEXT_L, x, y, p);//以(x,y)点Paint.Align.LEFT,

  p.setColor(0xBB00FF00);
      for (int i = 0; i < pos.length / 2; i++) {
          canvas.drawLine(pos[i * 2 + 0], pos[i * 2 + 1] - DY, pos[i * 2 + 0],
              pos[i * 2 + 1] + DY * 2, p);
      }
      p.setColor(Color.BLACK);

      p.setTextAlign(Paint.Align.LEFT);
      canvas.drawPosText(POSTEXT, pos, p);

  canvas.drawPath(mPath, mPathPaint);
              p.setTextAlign(Paint.Align.LEFT);
              canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);

             canvas.translate(0, DY * 1.5f);
               canvas.drawPath(mPath, mPathPaint);
              p.setTextAlign(Paint.Align.CENTER);
             canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);

              canvas.translate(0, DY * 1.5f);
              canvas.drawPath(mPath, mPathPaint);
              p.setTextAlign(Paint.Align.RIGHT);
              canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);

24.Typefaces:

获得assets/fonts下的字体

  mFace = Typeface.createFromAsset(getContext().getAssets(),
              "fonts/samplefont.ttf");

onDraw()方法

  @Override
         protected void onDraw(Canvas canvas) {
              canvas.drawColor(Color.WHITE);

              mPaint.setTypeface(null);
              canvas.drawText("Default", 10, 100, mPaint);
              mPaint.setTypeface(mFace);
             canvas.drawText("Custom", 10, 200, mPaint);
          }

25.UnicodeChart:(没看)

26.Vertices:(没看) 触摸一下,图片走形

27.Xfermodes:

圆形画法:

  static Bitmap makeDst(int w, int h) {
          Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
          Canvas c = new Canvas(bm);
          Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
          p.setColor(0xFFFFCC44);
          c.drawOval(new RectF(0, 0, w * 3 / 4, h * 3 / 4), p);
          return bm;
      }

正方形的画法:

  static Bitmap makeSrc(int w, int h) {
          Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
          Canvas c = new Canvas(bm);
          Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

          p.setColor(0xFF66AAFF);
          c.drawRect(w / 3, h / 3, w * 19 / 20, h * 19 / 20, p);
          return bm;
     }

背景马塞克画法:

  Bitmap bm = Bitmap.createBitmap(new int[] { 0xFFFFFFFF, 0xFFCCCCCC,
                  0xFFCCCCCC, 0xFFFFFFFF }, 2, 2, Bitmap.Config.RGB_565);
              mBG = new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
              Matrix m = new Matrix();
              m.setScale(6, 6);// 放大6倍
              mBG.setLocalMatrix(m);

Xfermode数组:

  private static final Xfermode[] sModes = {
              new PorterDuffXfermode(PorterDuff.Mode.CLEAR),
              new PorterDuffXfermode(PorterDuff.Mode.SRC),
              new PorterDuffXfermode(PorterDuff.Mode.DST),
              new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER),
              new PorterDuffXfermode(PorterDuff.Mode.DST_OVER),
              new PorterDuffXfermode(PorterDuff.Mode.SRC_IN),
              new PorterDuffXfermode(PorterDuff.Mode.DST_IN),
              new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT),
              new PorterDuffXfermode(PorterDuff.Mode.DST_OUT),
              new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP),
              new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP),
              new PorterDuffXfermode(PorterDuff.Mode.XOR),
              new PorterDuffXfermode(PorterDuff.Mode.DARKEN),
              new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN),
              new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY),
              new PorterDuffXfermode(PorterDuff.Mode.SCREEN) };

onDraw()方法:

   @Override
           protected void onDraw(Canvas canvas) {
               canvas.drawColor(Color.WHITE);

               Paint labelP = new Paint(Paint.ANTI_ALIAS_FLAG);
               labelP.setTextAlign(Paint.Align.CENTER);

               Paint paint = new Paint();
               paint.setFilterBitmap(false);

              canvas.translate(15, 35);

              int x = 0;
              int y = 0;
              for (int i = 0; i < sModes.length; i++) {
                  // draw the border
                  paint.setStyle(Paint.Style.STROKE);
                  paint.setShader(null);// Pass null to clear any previous shader
                  canvas.drawRect(x - 0.5f, y - 0.5f, x + W + 0.5f, y + H + 0.5f, paint);

                  // draw the checker-board pattern
                  paint.setStyle(Paint.Style.FILL);
                  paint.setShader(mBG);
                  canvas.drawRect(x, y, x + W, y + H, paint);

                  // draw the src/dst example into our offscreen bitmap
                  int sc = canvas.saveLayer(x, y, x + W, y + H, null,
                      Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
                          | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
                          | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
                          | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
                  // canvas.save();
                  canvas.translate(x, y);
                  canvas.drawBitmap(mDstB, 0, 0, paint);
                  paint.setXfermode(sModes[i]);
                  canvas.drawBitmap(mSrcB, 0, 0, paint);
padding-bottom: 0px !important; line-height: 18px; background-color: #f7f7ff !important; margin: 0em; padding-left: 12px !important; padding-right: 0px !important; color
 
时间: 2024-10-11 10:57:29

android.apis.graphics 画图(引)的相关文章

Android之自定义画图文字动画

结构: BaseView: package com.caiduping.canvas; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; public abstract class BaseView extends View{ //线程 private MyThread myThread; publi

Java SE (2)之 Graphics 画图工具

Graphics 绘图类: 提供两个方法.Paint (绘图,被系统自动调用)    repaint(重绘) Paint 调用原理(1.窗口最大化,再最小化 窗口的大小发生变化 Repaint函数被调用) package com.sunzhiyan; import java.awt.*; import javax.swing.*; public class Demo_5 extends JFrame{ /** * @param args */ public static void main(St

Android开发之画图的实现

Android开发之画图的实现    四天前上完安卓的第一节课,真的是一脸懵逼,尽管熊哥说和java是差不多的,然而这个包和那个包之间的那些转换都是些什么鬼呀!!!但是四天的学习和操作下来,我觉得安卓比java的学习要有趣一些,也更加简洁一些,它不像java要写很多类,所有的实现都在那几个类中,java的学习就像苦行僧似的,要什么自己去白手起家吧!但是安卓的学习是不同的,它更像一个富二代,父辈把基础都给你打好了,想要使用拿去就是了,为了节省你的力气,父辈更是分门别类地把框架放在那里 ,就差你把自

Android中用Bitmap画图预览,视频图像分辨率是320x240(4:3),手机分辨率是540x960(9:16),图像被拉伸怎么办?

问题描述 使用V4L2进行视频采集,采集到的视频数据分辨率为320x240(4:3),使用Canvas,drawBitmap方式在Android设备上预览图像 Android设备分辨率为 540x960(9:16),所以全屏预览时图像被拉伸,这种问题如何解决? 没思路啊,哪位大神指导指导!! 下面是画图像的代码,大神们帮忙看看 public void run() { //获取屏幕信息 DisplayMetrics dm = new DisplayMetrics(); getWindowManag

Android 2D Graphics学习 Region和Canvas裁剪

1.首先介绍Region类 Region,中文意思即区域的意思,它表示的是canvas图层上的某一块封闭的区域. [java] view plaincopyprint? /**构造方法*/ public Region()  //创建一个空的区域 public Region(Region region) //拷贝一个region的范围 public Region(Rect r)  //创建一个矩形的区域 public Region(int left, int top, int right, int

C#Graphics画图

public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics();//创建GDI对像 //创建画笔(颜色) Pen npen = new Pen(Brushes.Red); //创建两个点 Point n1 = new Point(20, 20); Point n2 = new Point(255

Java用BufferedImage和Graphics画图。

问题:网上大部分内容重复,且描述简单.注:内容:将某个字生成图片,且对图片背景色和图片上字的颜色有要求的情况. 解决:先用Graphics的方法setColor设置一下颜色,然后再用该类的fillRect填充背景色,接着再用该类的setColor设置一下颜色,再接着就是用该类的drawString画字了.ImageIO.write输出图片.最后用该类的dispose释放资源. 局部代码: int imageWidth = 200; int imageHeight = 200; BufferedI

C# System.Drawing.Graphics 画图后,如何保存一个低质量的图片,一个占用空间较小的图片

首先要控制图片保存后硬盘后的大小(即占用硬盘的空间,而非尺寸),真正要处理的是控制 System.Drawing.Bitmap.Save 方法的参数. 具体实现如下: private void ThumbPicture(Image SourceImage, int TargetWidth,string savePath) { int IntWidth; //新的图片宽 int IntHeight; //新的图片高 try { int TargetHeight = (int)Math.Round(

Android Camera探究之路——起步

Android Camera探究之路--起步 Camera在手机中有着举足轻重的地位,无论是二维码还是照片.识别.都离不开摄像头,本文将对Android中的Camera进行全面解析. 权限镇楼: <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE&