ITI0011:praktikum 10 N8

Allikas: Kursused
Redaktsioon seisuga 6. november 2014, kell 07:10 kasutajalt Ago (arutelu | kaastöö) (Uus lehekülg: 'Tagasi ITI0011 lehele. == Üldine == Praktikum: 6.11.2014 kell 8:00 Koodinäide: http://pastebin.com/EmPZq3AS == Koodinäide == '''activity_main.xml''': <source lang="xm...')
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 6.11.2014 kell 8:00

Koodinäide: http://pastebin.com/EmPZq3AS

Koodinäide

activity_main.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.drawing.MainActivity" >

   <com.example.drawing.DrawingView
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</RelativeLayout> </source>

DrawingView.java: <source lang="java"> package com.example.drawing;

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 DrawingView 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 DrawingView(Context context, AttributeSet attrs) {
               super(context, attrs);
              
               Log.d("DrawingView", "Laadis ära");
              
               paint.setColor(Color.BLUE);
               paint.setStrokeWidth(5);
               paint.setAntiAlias(true);
               paint.setStyle(Paint.Style.STROKE);
               paint.setStrokeJoin(Paint.Join.ROUND);
              
               line.moveTo(150, 50);
               line.lineTo(50, 150);
       }
      
       @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("DrawingView", "Finger on 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>