ITI0011:praktikum 10 N14

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 6.11.2014 kell 14:00

Koodinäide: http://pastebin.com/28QMAMWF

Koodinäide

main_activity.xml:

<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.paint.MainActivity" >

   <com.example.paint.CanvasView
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</RelativeLayout> </source>

MainActivity.java:

<source lang="java"> package com.example.paint;

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 List<Path> lines = new ArrayList<Path>();
       private float startX;
       private float startY;
      
       public CanvasView(Context context, AttributeSet attrs) {
               super(context, attrs);
               Log.d("Canvas", "Laadis edukalt!");
              
               paint.setColor(Color.BLACK);
               paint.setStrokeWidth(5);
               paint.setStyle(Paint.Style.STROKE);
               paint.setStrokeJoin(Paint.Join.ROUND);
               paint.setAntiAlias(true);
              
               line.moveTo(250, 200);
               line.lineTo(450, 300);
       }
      
       @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();
               Log.d("Canvas", "touch at x:" + x + " y:" + y);
               if (event.getAction() == MotionEvent.ACTION_DOWN) {
                       startX = x;
                       startY = y;
               } else if (event.getAction() == MotionEvent.ACTION_UP) {
                       lines.add(line);
               }
               line = new Path();
               line.moveTo(startX, startY);
               line.lineTo(x, y);
              
               invalidate();
               return true;
       }

} </source>