Erinevus lehekülje "ITI0011:praktikum 10 T8" redaktsioonide vahel
Mine navigeerimisribale
Mine otsikasti
(→Üldine) |
|||
| 6. rida: | 6. rida: | ||
Koodinäide: http://pastebin.com/sG6CPrnt | Koodinäide: http://pastebin.com/sG6CPrnt | ||
| + | |||
| + | == Koodinäide == | ||
| + | |||
| + | <source lang="xml"> | ||
| + | |||
| + | |||
| + | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| + | xmlns:tools="http://schemas.android.com/tools" | ||
| + | android:layout_width="match_parent" | ||
| + | android:layout_height="match_parent" | ||
| + | android:paddingBottom="@dimen/activity_vertical_margin" | ||
| + | android:paddingLeft="@dimen/activity_horizontal_margin" | ||
| + | android:paddingRight="@dimen/activity_horizontal_margin" | ||
| + | android:paddingTop="@dimen/activity_vertical_margin" | ||
| + | tools:context="com.example.androiddrawing.MainActivity" > | ||
| + | |||
| + | <com.example.androiddrawing.CanvasView | ||
| + | android:layout_width="match_parent" | ||
| + | android:layout_height="match_parent" /> | ||
| + | |||
| + | </RelativeLayout> | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | </source> | ||
| + | |||
| + | <source lang="java"> | ||
| + | |||
| + | |||
| + | package com.example.androiddrawing; | ||
| + | |||
| + | import java.util.ArrayList; | ||
| + | import java.util.List; | ||
| + | |||
| + | import android.content.Context; | ||
| + | import android.graphics.Canvas; | ||
| + | import android.graphics.Color; | ||
| + | import android.graphics.Paint; | ||
| + | import android.graphics.Path; | ||
| + | import android.util.AttributeSet; | ||
| + | import android.util.Log; | ||
| + | import android.view.MotionEvent; | ||
| + | import android.view.View; | ||
| + | |||
| + | public class CanvasView extends View { | ||
| + | |||
| + | private Paint paint = new Paint(); | ||
| + | private Path line = new Path(); | ||
| + | private static List<Path> lines = new ArrayList<Path>(); | ||
| + | private float clickX; | ||
| + | private float clickY; | ||
| + | |||
| + | public CanvasView(Context context, AttributeSet attrs) { | ||
| + | super(context, attrs); | ||
| + | |||
| + | Log.d("TERE", "Laadis"); | ||
| + | |||
| + | paint.setAntiAlias(true); | ||
| + | paint.setStrokeWidth(5); | ||
| + | paint.setColor(Color.RED); | ||
| + | paint.setStyle(Paint.Style.STROKE); | ||
| + | paint.setStrokeJoin(Paint.Join.ROUND); | ||
| + | |||
| + | line.moveTo(50, 50); | ||
| + | line.lineTo(200, 200); | ||
| + | lines.add(line); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | protected void onDraw(Canvas canvas) { | ||
| + | for (Path p: lines) | ||
| + | canvas.drawPath(p, paint); | ||
| + | canvas.drawPath(line, paint); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public boolean onTouchEvent(MotionEvent event) { | ||
| + | float x = event.getX(); | ||
| + | float y = event.getY(); | ||
| + | |||
| + | if (event.getAction() == MotionEvent.ACTION_DOWN) { | ||
| + | clickX = x; | ||
| + | clickY = y; | ||
| + | } else if (event.getAction() == MotionEvent.ACTION_UP) { | ||
| + | lines.add(line); | ||
| + | } | ||
| + | |||
| + | line = new Path(); | ||
| + | line.moveTo(clickX, clickY); | ||
| + | line.lineTo(x, y); | ||
| + | |||
| + | invalidate(); | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | } | ||
| + | </source> | ||
Redaktsioon: 4. november 2014, kell 07:41
Tagasi ITI0011 lehele.
Üldine
Praktikum: 4.11.2014 kell 8:00
Koodinäide: http://pastebin.com/sG6CPrnt
Koodinäide
<source lang="xml">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.androiddrawing.MainActivity" > <com.example.androiddrawing.CanvasView android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
</source>
<source lang="java">
package com.example.androiddrawing;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class CanvasView extends View {
private Paint paint = new Paint();
private Path line = new Path();
private static List<Path> lines = new ArrayList<Path>();
private float clickX;
private float clickY;
public CanvasView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d("TERE", "Laadis");
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
line.moveTo(50, 50);
line.lineTo(200, 200);
lines.add(line);
}
@Override
protected void onDraw(Canvas canvas) {
for (Path p: lines)
canvas.drawPath(p, paint);
canvas.drawPath(line, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
clickX = x;
clickY = y;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lines.add(line);
}
line = new Path();
line.moveTo(clickX, clickY);
line.lineTo(x, y);
invalidate();
return true;
}
}
</source>