Android 用Canvas畫textview、bitmap、矩形(裁剪)、橢圓、線、點、弧

來源:互聯網
上載者:User

標籤:

初始化對象

private Paint mPaint;//畫筆private int count;//點擊次數private Rect rect;//矩形public CounstomView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);//初始化畫筆mPaint = new Paint();rect = new Rect();setOnClickListener(this);}

1.畫textview。並且設定可點擊。//渲染文本,Canvas類除了上面的還可以描繪文字,參數一是String類型的文本,參數二x軸,參數三y軸,參數四是Paint對象。

String text = null;@SuppressLint("DrawAllocation") @Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//設定paintmPaint.setStyle(Style.FILL);//實心//mPaint.setColor(Color.BLUE);//黃色//mPaint.setAlpha(50);//透明度15canvas.drawColor(Color.YELLOW);canvas.drawRect(0, 0, getHeight(), getWidth(), mPaint);//畫矩形mPaint.setColor(Color.RED);mPaint.setTextSize(25.0f); text = String.valueOf(count);mPaint.getTextBounds(text, 0, text.length(), rect);//將內容的長和寬。添加到rect矩形中float width = rect.width();//擷取寬float height = rect.height();canvas.drawText(text, getWidth() / 2  - width / 2, getHeight() / 2 + height/2, mPaint);//設定文本位置//}@Overridepublic void onClick(View arg0) {count++;invalidate();//重繪}

2.畫簡單的bitmap。參數一就是我們常規的Bitmap對象,參數二是來源區域(這裡是bitmap),參數三是目的地區域(應該在canvas的位置和大小),參數四是Paint畫刷對象,因為用到了縮放和展開的可能,當原始Rect不等於目標Rect時效能將會有大幅損失。

protected void onDraw(Canvas canvas) {super.onDraw(canvas);//偏左和偏上的位置Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.login_backgroud);canvas.drawBitmap(bitmap,0,0, mPaint);}


3.利用canvas.drawBitmap(bitmap, src, dst, paint)畫裁剪過的bitmap//參數一就是我們常規的Bitmap對象,參數二是來源區域(這裡是bitmap),參數三是目的地區域(應該在canvas的位置和大小),參數四是Paint畫刷對象,因為用到了縮放和展開的可能,當原始Rect不等於目標Rect時效能將會有大幅損失。

protected void onDraw(Canvas canvas) {super.onDraw(canvas);// 偏左和偏上的位置Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.login_backgroud);Paint paint = new Paint();canvas.save();// left----距離螢幕左側距離。。。// top------距離頂部距離。。。// right-----矩形的寬度// buttom-----矩形的高度Rect rect = new Rect(10, 10, 500, 500);canvas.clipRect(rect); // 設定裁剪地區canvas.drawColor(Color.BLUE);// 裁剪地區的rect變為藍色Rect rec = new Rect(20, 20, 300, 300);canvas.drawBitmap(bitmap, rect, rec, paint);canvas.restore();}




4:利用canvas.drawOval畫橢圓。畫橢圓,參數一是掃描地區,參數二為paint對象;

protected void onDraw(Canvas canvas) {super.onDraw(canvas);// 偏左和偏上的位置Paint paint = new Paint();canvas.save();// left----距離螢幕左側距離。。。A// top------距離頂部距離。。。B// right-----橢圓的寬度.....C// buttom-----橢圓的高度......DRectF dst = new RectF(10,10,300, 400);//2a=100-30,2b=310-260paint.setColor(Color.YELLOW);canvas.drawColor(Color.RED);canvas.drawOval(dst, paint);canvas.restore();}


5.利用canvas畫點。//畫點,參數一水平x軸,參數二垂直y軸,第三個參數為Paint對象。

@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();canvas.save();canvas.drawColor(Color.RED);//有背景先畫背景canvas.restore();canvas.save();paint.setColor(Color.BLACK);paint.setTextSize(20.0f);canvas.drawText("畫點:", 10, 90, paint);//文本canvas.restore();canvas.save();paint.setColor(Color.GREEN);paint.setStrokeWidth(20.0f);//設定點的大小canvas.drawPoint(120, 90, paint);//參數一水平x軸,參數二垂直y軸,第三個參數為Paint對象。//canvas.drawPoints(new float[]{60,400,65,400,70,40}, paint);//畫多個點canvas.restore();canvas.save();paint.setColor(Color.BLACK);paint.setStyle(Style.FILL);canvas.drawText("這樣可以當圓點用麼:", 10, 130, paint);paint.setAntiAlias(true);//去除消除鋸齒paint.setColor(Color.YELLOW);canvas.drawCircle(120, 130, 10, paint);//參數一水平x軸,參數二垂直y軸,參數三圓半徑,參數四Paint對象canvas.restore();}



6:畫線。drawLine(float startX, float startY, float stopX, float stopY, Paintpaint)//參數一起始點的x軸位置,參數二起始點的y軸位置,參數三終點的x軸水平位置,參數四y軸垂直位置,最後一個參數為Paint 畫刷對象。

protected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();canvas.drawColor(Color.YELLOW);paint.setAntiAlias(true);//消除鋸齒paint.setDither(true);//抖動,讓圖形看起來沒有毛邊paint.setColor(Color.RED);paint.setStrokeWidth(3);//設定線的粗細canvas.drawLine(20, 50, 200, 100, paint);//參數一起始點的x軸位置,參數二起始點的y軸位置,參數三終點的x軸水平位置,參數四y軸垂直位置,最後一個參數為Paint 畫刷對象。}



7:畫弧線:

參數一是RectF對象,一個矩形地區橢圓形的界限用於定義在形狀、大小、電弧,參數二是起始角(度)在電弧的開始,

參數三掃掠角度(度)開始順時針測量的,參數四是如果這是真的話,包括橢圓中心的電弧,並關閉它,如果它是假這將是一個弧線,參數五是Paint對象;


protected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();canvas.drawColor(Color.BLUE);//畫弧paint.setStyle(Style.STROKE);//設定空心paint.setColor(Color.RED);//設定顏色paint.setStrokeWidth(3);//設定粗細RectF oval = new RectF();oval.set(50, 50, 200, 200);//oval.contains(100, 55, 205, 205);//canvas.drawArc(oval, 180, 180, true, paint);canvas.drawArc(oval, 180, 180, false, paint);}
當為true時
canvas.drawArc(oval, 180, 180, true, paint);

當為false時

canvas.drawArc(oval, 180, 180, false, paint);


Android 用Canvas畫textview、bitmap、矩形(裁剪)、橢圓、線、點、弧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.