-1
A
回答
2
看到這個代碼第一:
public class CustomView extends View {
public static final int NO_OF_VERTICAL_CIRCLES = 5;
public static final int NO_OF_HORIZONTAL_CIRCLES = 4;
public static final float RADIUS = 60f;
private Paint mPaintImage;
private RectF[] rectangles = new RectF[NO_OF_HORIZONTAL_CIRCLES
* NO_OF_VERTICAL_CIRCLES];
// Just creating a view for drawing
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// This is paint for your drawing
mPaintImage = new Paint();
mPaintImage.setAntiAlias(true);
mPaintImage.setFilterBitmap(true);
mPaintImage.setDither(true);
// color for circle
mPaintImage.setColor(Color.RED);
// fill style
mPaintImage.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// Account for padding
float xpad = (float) (getPaddingLeft() + getPaddingRight());
float ypad = (float) (getPaddingTop() + getPaddingBottom());
// getting area for drawing you can also get it using getWidth() and getHeight()
float ww = (float) w - xpad;
float hh = (float) h - ypad;
Creating Rectangle for drawing your circles
for (short i = 0; i < NO_OF_HORIZONTAL_CIRCLES; i++) {
for (short j = 0; j < NO_OF_VERTICAL_CIRCLES; j++) {
// calculating horizontal centers
float horizontalCenter = RADIUS + i * (ww - RADIUS * 2)
/(NO_OF_HORIZONTAL_CIRCLES - 1);
// calculating vertical centers
float verticalCenter = RADIUS + j * (hh - RADIUS * 2)
/(NO_OF_VERTICAL_CIRCLES - 1);
rectangles[j + NO_OF_VERTICAL_CIRCLES * i] = new RectF(
horizontalCenter - RADIUS, verticalCenter - RADIUS,
horizontalCenter + RADIUS, verticalCenter + RADIUS);
}
}
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
// Here drawing circles
for (RectF rect : rectangles) {
canvas.drawCircle(rect.centerX(), rect.centerY(), RADIUS,
mPaintImage);
}
super.onDraw(canvas);
}
這裏,我所做的電網只是劃分篩網,然後找出中心,並相應地繪製圓。
以類似的方式,您可以繪製位圖,矩形。
您還可以在空位圖上繪圖。
我希望這會有所幫助。
PS(截屏)::
+0
感謝您的幫助。它的工作。 – Jaydeep 2014-10-17 05:11:51
2
既然你是新的Android和這裏學習畫布是延伸的觀點一個簡單的類代碼 - 你可以覆蓋帆布的onDraw畫在畫布上
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Paint p;
public MyView(Context context, AttributeSet aSet) {
super(context, aSet);
//it's best not to create any new objects in the onDraw
//initialize as class variables
p = new Paint();
}
@Override
public void onDraw(Canvas canvas) {
//paint specs
p.setColor(Color.BLACK);
p.setStyle(Paint.Style.FILL);
p.setDither(true);
p.setAntiAlias(true);
p.setStrokeJoin(Paint.Join.ROUND);
p.setStrokeCap(Paint.Cap.ROUND);
// space between dots
int delta = 50;
// dot radius
int r = 10;
// 1st row
canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4, r, p);
// 2nd row
canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4+delta, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4+delta, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4+delta, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4+delta, r, p);
// 3rd row
canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4+delta*2, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4+delta*2, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4+delta*2, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4+delta*2, r, p);
// 4th row
canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4+delta*3, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4+delta*3, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4+delta*3, r, p);
canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4+delta*3, r, p);
}
}
+0
感謝您的幫助。它的工作。 – Jaydeep 2014-10-17 05:12:25
相關問題
- 1. 畫布和繪畫圖片
- 2. 畫布繪製圖片錯誤
- 3. 如何使用畫布繪製此img?
- 4. 如何隨意使用畫布繪製?
- 5. 如何使用畫布在位圖中繪製筆畫?
- 6. 如何使用CSS畫布繪製幻燈片框架?
- 7. 如何在畫布上繪製畫布
- 8. wp7使用畫布繪製圖像?
- 9. HTML5在畫布上使用for循環繪製圖片?
- 10. 使用畫布繪製Rects
- 11. 使用畫布繪圖
- 12. 使用HTML5畫布繪圖
- 13. WPF畫布圖形繪製
- 14. HTML5畫布繪製圖像
- 15. 如何變暗畫布或畫布上繪製的圖像?
- 16. 如何使用畫布繪製圖像精靈?
- 17. 如何使用html5畫布標籤導出繪製的圖像?
- 18. 如何使用html5畫布繪製多級餅圖?
- 19. 如何使用畫布以梯形形狀繪製圖像
- 20. 如何使用畫布在圖像上繪製疊加層?
- 21. 如何用動畫使用javascript繪製多個畫布元素?
- 22. 如何用Javascript繪製畫布
- 23. 安排畫布上繪製的繪圖
- 24. 如何繪製不使用畫布的圓形動畫?
- 25. 如何動畫在Android中使用代碼繪製的畫布?
- 26. 如何使用畫布動畫繪製曲線?
- 27. 繪圖畫布
- 28. 繪圖畫布
- 29. 用HTML5畫布繪製草圖效果
- 30. 用JQuery在畫布上繪製圖像
到目前爲止你有什麼?代碼和輸出 – Yazan 2014-10-16 13:10:12
得到了答案!!!!!!!!!! – Jaydeep 2014-10-17 07:36:02