verständnisfrage zur grafik (views, drawables etc)

  • Antworten:5
christian mock
  • Forum-Beiträge: 10

26.08.2010, 21:51:13 via Website

hallo,

ich möcht ein paar linien auf den bildschirm malen. mach mir also einen Path mit den linien, dann ein PathShape daraus und dann aus dem ein ShapeDrawable; letzteres setz ich wiederum als Drawable in eine ImageView -- so hab ich mir das zusammengereimt.

problem dabei: es wird nix gezeichnet. aus den android-tutorials hab ich jetzt eins gefunden, das eine subclass von View macht, dort ein ShapeDrawable anlegt, und in onDraw() dann Drawable.draw() aufruft -- und jetzt frag ich mich: hab ich an der ganzen objektorientierten programmierung was nicht verstanden, oder an deren umsetzung im android-SDK?

ich war bisher davon ausgegangen, daß bei so einer schachtelung von GUI-objekten beim toplevel-objekt die Draw-methode aufgerufen wird und die dann selber dafür sorgt, die bei den child-objekten aufzurufen, und daß ich als programmierer mich darum erst kümmern muß, wenn ich ein eigenes GUI-objekt schreibe, abner nicht, wenn ich nur existierende objekte/klassen zusammenstoppele. geschieht das bei android nicht und ich muß das zu fuß machen, oder hab ich was nicht verstanden?

danke,

cm.

Antworten
c00der
  • Forum-Beiträge: 7

27.08.2010, 10:23:43 via Website

ups. bitte ignorieren, dachte ich wäre in einem anderem topic :*)

— geändert am 27.08.2010, 10:24:36

Antworten
christian mock
  • Forum-Beiträge: 10

27.08.2010, 17:23:51 via Website

Dominic Bartl
du willst also irgend ein polygon in einem imageview anzeigen?

yup, im prinzip. konkret gehts darum, über einen Camera-preview ein fadenkreuz einzublenden; aber auch ohne den preview hab ich das geschilderte problem...

cm.

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

27.08.2010, 21:54:00 via Website

So funtzt ein View bei mir:

1public final class CompassView extends ImageView
2{
3 /**
4 * Start direction of Wind
5 */
6 private volatile CardinalDirection fromDirection = CardinalDirection.N;
7 /**
8 * End direction of Wind
9 */
10 private volatile CardinalDirection toDirection = CardinalDirection.S;
11
12 private int overlayInnerColor = 0;
13
14 private int overlayOuterColor = 0;
15
16 private int overlayPivotX = 0;
17
18 private int overlayPivotY = 0;
19
20 private int overlayRadius = 0;
21
22 private Paint overlayPaint;
23
24 private Shape shape;
25
26 /**
27 *
28 * @param context
29 */
30 public CompassView(final Context context)
31 {
32 super(context);
33 }
34
35 /**
36 *
37 * @param context
38 * @param attrs
39 */
40 public CompassView(final Context context, final AttributeSet attrs)
41 {
42 super(context, attrs);
43 }
44
45 /**
46 *
47 * @param context
48 * @param attrs
49 * @param defStyle
50 */
51 public CompassView(final Context context, final AttributeSet attrs, final int defStyle)
52 {
53 super(context, attrs, defStyle);
54 }
55
56 /*
57 * (non-Javadoc)
58 *
59 * @see android.widget.ImageView#onMeasure(int, int)
60 */
61 @Override
62 protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
63 {
64 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
65 }
66
67 /*
68 * (non-Javadoc)
69 *
70 * @see android.widget.ImageView#onDraw(android.graphics.Canvas)
71 */
72 @Override
73 protected void onDraw(final Canvas canvas)
74 {
75 if (overlayPaint == null)
76 {
77 readResourceValues();
78 overlayPaint = new Paint();
79 overlayPaint.setShader(new RadialGradient(overlayPivotX, overlayPivotY, overlayRadius, overlayInnerColor,
80 overlayOuterColor, TileMode.REPEAT));
81 overlayPaint.setAntiAlias(true);
82 final boolean isFromGreater = fromDirection.getDegree() > toDirection.getDegree();
83 if (!isFromGreater)
84 {
85 final float startPoint = Math.min(fromDirection.getDegree(), toDirection.getDegree());
86 final float maxAngle = Math.max(fromDirection.getDegree(), toDirection.getDegree());
87 final float angle = maxAngle - startPoint;
88 shape = new ArcShape(startPoint - 90, angle);
89 }
90 else
91 {
92 final float startPoint = fromDirection.getDegree();
93 // Nullpunkt ist bei 90 Grad
94 final float angle = 360 - startPoint + toDirection.getDegree();
95 shape = new ArcShape(startPoint - 90, angle);
96 }
97
98 }
99 /**
100 * translate the canvas to the upper left were Arc begins. Seems that
101 * Android does not draw shapes from its midpoint as Swing does.
102 */
103 //
104 canvas.translate((getWidth() - overlayRadius) / 2.0f, (getHeight() - overlayRadius) / 2.0f);
105 shape.resize(overlayRadius, overlayRadius);
106 shape.draw(canvas, overlayPaint);
107 }
108
109...
110...

In der XML musst du den halt entsprechend angeben.

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
christian mock
  • Forum-Beiträge: 10

30.08.2010, 12:24:38 via Website

danke für den code, bei mir schauts inzwischen im prinzip auch so aus.

aber meine eigentliche frage war die: wenn ich zb. bei einer TextView mit setText() den text ändere, dann brauch ich mich nimmer weiter drum zu kümmern, daß dieser text am display erscheint -- bei einer ImageView mit einem Shape drin aber schon. scheint mir inkonstistent...

cm.

Antworten