当前位置: 代码迷 >> 综合 >> Apidemo 学习 Regions RoundRects ScaleToFit
  详细解决方案

Apidemo 学习 Regions RoundRects ScaleToFit

热度:0   发布时间:2024-01-21 15:18:50.0
import android.graphics.*;
import android.os.Bundle;
import android.view.View;
/*** 还是绘制图案的相交区域. 有四种*/
public class Regions extends GraphicsActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new SampleView(this));}private static class SampleView extends View {private final Paint mPaint = new Paint();private final Rect  mRect1 = new Rect();private final Rect  mRect2 = new Rect();public SampleView(Context context) {super(context);setFocusable(true);mPaint.setAntiAlias(true);mPaint.setTextSize(16);mPaint.setTextAlign(Paint.Align.CENTER);mRect1.set(10, 10, 100, 80);mRect2.set(50, 50, 130, 110);}private void drawOriginalRects(Canvas canvas, int alpha) {mPaint.setStyle(Paint.Style.STROKE);mPaint.setColor(Color.RED);mPaint.setAlpha(alpha);drawCentered(canvas, mRect1, mPaint);mPaint.setColor(Color.BLUE);mPaint.setAlpha(alpha);drawCentered(canvas, mRect2, mPaint);// restore stylemPaint.setStyle(Paint.Style.FILL);}private void drawRgn(Canvas canvas, int color, String str, Region.Op op) {if (str != null) {mPaint.setColor(Color.BLACK);canvas.drawText(str, 80, 24, mPaint);}Region rgn = new Region();rgn.set(mRect1);//1 和 2 以什么样的形式进行 合并  一个region可以理解为 一些 rect的集合rgn.op(mRect2, op);mPaint.setColor(color);RegionIterator iter = new RegionIterator(rgn); //迭代 region中 一共有几个 rectRect r = new Rect();canvas.translate(0, 30);mPaint.setColor(color);while (iter.next(r)) {canvas.drawRect(r, mPaint);}drawOriginalRects(canvas, 0x80);}private static void drawCentered(Canvas c, Rect r, Paint p) {float inset = p.getStrokeWidth() * 0.5f;if (inset == 0) {   // catch hairlinesinset = 0.5f;}c.drawRect(r.left + inset, r.top + inset,r.right - inset, r.bottom - inset, p);}@Overrideprotected void onDraw(Canvas canvas) {canvas.drawColor(Color.GRAY);canvas.save();canvas.translate(80, 5);drawOriginalRects(canvas, 0xFF);canvas.restore();mPaint.setStyle(Paint.Style.FILL);canvas.save();canvas.translate(0, 140);drawRgn(canvas, Color.RED, "Union", Region.Op.UNION);canvas.restore();canvas.save();canvas.translate(0, 280);drawRgn(canvas, Color.BLUE, "Xor", Region.Op.XOR);canvas.restore();canvas.save();canvas.translate(160, 140);drawRgn(canvas, Color.GREEN, "Difference", Region.Op.DIFFERENCE);canvas.restore();canvas.save();canvas.translate(160, 280);drawRgn(canvas, Color.WHITE, "Intersect", Region.Op.INTERSECT);canvas.restore();}}
}


public class RoundRects extends GraphicsActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new SampleView(this));}private static class SampleView extends View {private Path    mPath;private Paint   mPaint;private Rect    mRect;private GradientDrawable mDrawable;public SampleView(Context context) {super(context);setFocusable(true);mPath = new Path();mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mRect = new Rect(0, 0, 120, 120);mDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR,new int[] { 0xFFFF0000, 0xFF00FF00,0xFF0000FF });mDrawable.setShape(GradientDrawable.RECTANGLE);mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60));}//设置圆角的半径static void setCornerRadii(GradientDrawable drawable, float r0,float r1, float r2, float r3) {drawable.setCornerRadii(new float[] { r0, r0, r1, r1,r2, r2, r3, r3 });}@Override protected void onDraw(Canvas canvas) {mDrawable.setBounds(mRect);float r = 16;canvas.save();canvas.translate(10, 10);mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//线性渐变setCornerRadii(mDrawable, r, r, 0, 0);mDrawable.draw(canvas);canvas.restore();canvas.save();canvas.translate(10 + mRect.width() + 10, 10);mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);//从圆心放射渐变setCornerRadii(mDrawable, 0, 0, r, r);mDrawable.draw(canvas);canvas.restore();canvas.translate(0, mRect.height() + 10);canvas.save();canvas.translate(10, 10);mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);//中间有个尖 不知道怎么描述setCornerRadii(mDrawable, 0, r, r, 0);mDrawable.draw(canvas);canvas.restore();canvas.save();canvas.translate(10 + mRect.width() + 10, 10);mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//线性渐变 setCornerRadii(mDrawable, r, 0, 0, r);mDrawable.draw(canvas);canvas.restore();canvas.translate(0, mRect.height() + 10);canvas.save();canvas.translate(10, 10);mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);setCornerRadii(mDrawable, r, 0, r, 0);mDrawable.draw(canvas);canvas.restore();canvas.save();canvas.translate(10 + mRect.width() + 10, 10);mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);setCornerRadii(mDrawable, 0, r, 0, r);mDrawable.draw(canvas);canvas.restore();}}
}


import android.graphics.*;
import android.os.Bundle;
import android.view.View;
/*** 把图案 放到 一个方框中*/
public class ScaleToFit extends GraphicsActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new SampleView(this));}private static class SampleView extends View {private final Paint   mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);private final Paint   mHairPaint = new Paint(Paint.ANTI_ALIAS_FLAG);private final Paint   mLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);private final Matrix  mMatrix = new Matrix();private final RectF   mSrcR = new RectF();private static final Matrix.ScaleToFit[] sFits =new Matrix.ScaleToFit[] {Matrix.ScaleToFit.FILL,Matrix.ScaleToFit.START,Matrix.ScaleToFit.CENTER,Matrix.ScaleToFit.END};private static final String[] sFitLabels = new String[] {"FILL", "START", "CENTER", "END"};private static final int[] sSrcData = new int[] {80, 40, Color.RED,40, 80, Color.GREEN,30, 30, Color.BLUE,80, 80, Color.BLACK};private static final int N = 4;private static final int WIDTH = 52;private static final int HEIGHT = 52;private final RectF mDstR = new RectF(0, 0, WIDTH, HEIGHT);public SampleView(Context context) {super(context);mHairPaint.setStyle(Paint.Style.STROKE);mLabelPaint.setTextSize(16);}private void setSrcR(int index) {int w = sSrcData[index*3 + 0];int h = sSrcData[index*3 + 1];mSrcR.set(0, 0, w, h);}private void drawSrcR(Canvas canvas, int index) {mPaint.setColor(sSrcData[index*3 + 2]);canvas.drawOval(mSrcR, mPaint);}private void drawFit(Canvas canvas, int index, Matrix.ScaleToFit stf) {canvas.save();setSrcR(index);mMatrix.setRectToRect(mSrcR, mDstR, stf); //在方框中绘制的四种方式,和imageview比较相似 canvas.concat(mMatrix);drawSrcR(canvas, index);canvas.restore();canvas.drawRect(mDstR, mHairPaint);}@Overrideprotected void onDraw(Canvas canvas) {canvas.drawColor(Color.WHITE);canvas.translate(10, 10);canvas.save();for (int i = 0; i < N; i++) {//绘制开始的那四个图形setSrcR(i);drawSrcR(canvas, i);canvas.translate(mSrcR.width() + 15, 0);}canvas.restore();canvas.translate(0, 100);for (int j = 0; j < sFits.length; j++) {canvas.save();for (int i = 0; i < N; i++) {drawFit(canvas, i, sFits[j]);canvas.translate(mDstR.width() + 8, 0); //移动到相应位置}canvas.drawText(sFitLabels[j], 0, HEIGHT*2/3, mLabelPaint);canvas.restore();canvas.translate(0, 80);}}}
}


  相关解决方案