您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在android中繪制一個幾何圖形,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
代碼:
public class MyView extends View { public MyView(Context context, AttributeSet set) { super(context, set); } @Override // 重寫該方法,進行繪圖 protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 把整張畫布繪制成白色 canvas.drawColor(Color.WHITE); Paint paint = new Paint();//創建一個畫筆對象 // 去鋸齒 paint.setAntiAlias(true); paint.setColor(Color.BLUE);//設置畫筆顏色為藍色 paint.setStyle(Paint.Style.STROKE);//設置樣式 paint.setStrokeWidth(3);//設置樣式的寬度 // 繪制圓形 canvas.drawCircle(40, 40, 30, paint); // 繪制正方形 canvas.drawRect(10, 80, 70, 140, paint); // 繪制矩形 canvas.drawRect(10, 150, 70, 190, paint); RectF re1 = new RectF(10, 200, 70, 230); // 繪制圓角矩形 canvas.drawRoundRect(re1, 15, 15, paint); RectF re11 = new RectF(10, 240, 70, 270); // 繪制橢圓 canvas.drawOval(re11, paint); // 定義一個Path對象,封閉成一個三角形。 Path path2 = new Path(); path2.moveTo(10, 340);//將路徑點設置到10,340位置 path2.lineTo(70, 340);//將起始路徑點連接都70,340位置 path2.lineTo(40, 290);//將第二連接點連接到40,290位置 path2.close();//關閉路徑的繪制 // 根據Path進行繪制,繪制三角形 canvas.drawPath(path2, paint); // 定義一個Path對象,封閉成一個五角形。 Path path3 = new Path(); path3.moveTo(26, 360); path3.lineTo(54, 360); path3.lineTo(70, 392); path3.lineTo(40, 420); path3.lineTo(10, 392); path3.close(); // 根據Path進行繪制,繪制五角形 canvas.drawPath(path3, paint); // ----------設置填充風格后繪制---------- paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); canvas.drawCircle(120, 40, 30, paint); //繪制正方形 canvas.drawRect(90, 80, 150, 140, paint); //繪制矩形 canvas.drawRect(90, 150, 150, 190, paint); RectF re2 = new RectF(90, 200, 150, 230); //繪制圓角矩形 canvas.drawRoundRect(re2, 15, 15, paint); RectF re21 = new RectF(90, 240, 150, 270); // 繪制橢圓 canvas.drawOval(re21, paint); Path path4 = new Path(); path4.moveTo(90, 340); path4.lineTo(150, 340); path4.lineTo(120, 290); path4.close(); //繪制三角形 canvas.drawPath(path4, paint); Path path5 = new Path(); path5.moveTo(106, 360); path5.lineTo(134, 360); path5.lineTo(150, 392); path5.lineTo(120, 420); path5.lineTo(90, 392); path5.close(); //繪制五角形 canvas.drawPath(path5, paint); // ----------設置漸變器后繪制---------- // 為Paint設置漸變器 Shader mShader = new LinearGradient(0, 0, 40, 60 , new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW } , null , Shader.TileMode.REPEAT); paint.setShader(mShader); //設置陰影 paint.setShadowLayer(45 , 10 , 10 , Color.GRAY); // 繪制圓形 canvas.drawCircle(200, 40, 30, paint); // 繪制正方形 canvas.drawRect(170, 80, 230, 140, paint); // 繪制矩形 canvas.drawRect(170, 150, 230, 190, paint); RectF re3 = new RectF(170, 200, 230, 230); // 繪制圓角矩形 canvas.drawRoundRect(re3, 15, 15, paint); RectF re31 = new RectF(170, 240, 230, 270); // 繪制橢圓 canvas.drawOval(re31, paint); Path path6 = new Path(); path6.moveTo(170, 340); path6.lineTo(230, 340); path6.lineTo(200, 290); path6.close(); // 根據Path進行繪制,繪制三角形 canvas.drawPath(path6, paint); Path path7 = new Path(); path7.moveTo(186, 360); path7.lineTo(214, 360); path7.lineTo(230, 392); path7.lineTo(200, 420); path7.lineTo(170, 392); path7.close(); // 根據Path進行繪制,繪制五角形 canvas.drawPath(path7, paint); // ----------設置字符大小后繪制---------- paint.setTextSize(24); paint.setShader(null); // 繪制7個字符串 canvas.drawText(getResources().getString(R.string.circle), 240, 50, paint); canvas.drawText(getResources().getString(R.string.square), 240, 120, paint); canvas.drawText(getResources().getString(R.string.rect), 240, 175, paint); canvas.drawText(getResources().getString(R.string.round_rect), 230, 220, paint); canvas.drawText(getResources().getString(R.string.oval), 240, 260, paint); canvas.drawText(getResources().getString(R.string.triangle), 240, 325, paint); canvas.drawText(getResources().getString(R.string.pentagon), 240, 390, paint); } }
上述內容就是怎么在android中繪制一個幾何圖形,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。