» »

android črta

android črta

g333kk ::

Hotel sem izrisati android ustvari sem nov projekt vendar mi je aplikacija, ko sem jo prižgal, se mi je ugasnila sama. Ne vem kaj je narobe. Ali bi moral kaj v xml-ju spremeniti če hočem narisati črto ?
Koda...
Drawing.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
 
public class Drawing extends View {
    Paint paint = new Paint();
 
    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLUE);//here you can set any color you wish for your line
    }
 
    @Override
    public void onDraw(Canvas canvas) {
            canvas.drawLine(0, 0, 35, 35, paint); //first 0, is starting x, second 0 is starting y(this means line will start be drawn from top-left corner of the device....and will end at 20 pixels on x and 20 on y)
            canvas.drawLine(35, 0, 0, 35, paint); //this is a second line that will cross the first line
    }
 
}

To use this class you will implement the Main.java class:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
 
public class Main extends Activity {
    Drawing drawView;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        drawing= new Drawing(this);
        drawing.setBackgroundColor(Color.WHITE);
        setContentView(drawing);
 
    }
}
  • spremenilo: g333kk ()

mm1992 ::

Kaj pa ti napiše v "LogCat"?

Ali ti to sploh prav prevede? Ker meni se zdi da je napaka v konstrukturju "Drawing" razreda...

Zgodovina sprememb…

  • spremenil: mm1992 ()

g333kk ::

Hja zdej mi pa noče importat projekta ;( ..Groza..

g333kk ::

[2012-08-31 19:45:29 - pii] Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
[2012-08-31 19:45:29 - pii] Please check logcat output for more details.
[2012-08-31 19:45:29 - pii] Launch canceled!

Končno dobil sori k ste me čakal :((

Kodo sem spremenil...pa po moje je zaradi gumbeka k sem ga dodal..
package com.example.pii;


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.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, 5, paint);
            // Log.d(TAG, "Painting: "+point);
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        Log.d(TAG, "point: " + point);
        return true;
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}


package com.example.pii;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class Draw extends Activity {
    DrawView drawView;
    Button knof;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set full screen view
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        drawView = new DrawView(this);
        setContentView(drawView);
        drawView.requestFocus();
        knof=(Button)findViewById(R.id.button1);
        knof.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
        
    }
}

Zgodovina sprememb…

  • spremenilo: g333kk ()

g333kk ::

08-31 17:54:26.504: D/AndroidRuntime(1162): Shutting down VM
08-31 17:54:26.504: W/dalvikvm(1162): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-31 17:54:26.594: E/AndroidRuntime(1162): FATAL EXCEPTION: main
08-31 17:54:26.594: E/AndroidRuntime(1162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pii/com.example.pii.Draw}: java.lang.NullPointerException
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.os.Looper.loop(Looper.java:123)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-31 17:54:26.594: E/AndroidRuntime(1162): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 17:54:26.594: E/AndroidRuntime(1162): at java.lang.reflect.Method.invoke(Method.java:521)
08-31 17:54:26.594: E/AndroidRuntime(1162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-31 17:54:26.594: E/AndroidRuntime(1162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-31 17:54:26.594: E/AndroidRuntime(1162): at dalvik.system.NativeStart.main(Native Method)
08-31 17:54:26.594: E/AndroidRuntime(1162): Caused by: java.lang.NullPointerException
08-31 17:54:26.594: E/AndroidRuntime(1162): at com.example.pii.Draw.onCreate(Draw.java:27)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-31 17:54:26.594: E/AndroidRuntime(1162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-31 17:54:26.594: E/AndroidRuntime(1162): ... 11 more

g333kk ::

Sicer sem resil problem ampak imam pa vprašanje, kako naredim da bom lahko risal pike sem :
setContentView(R.layout.activity_main); 


in ne
setContentView(drawview);
tukaj rišem..

Marat ::

Sicer nisem še delal tega, ampak mislim da boš moral inflatat view v katerega boš risal. Poglej si LayoutInflater in malo poguglaj.

illion ::

Če uporabljaš custom view v xml-ju, moraš v tem view-u implemenitrat pravilen konstruktor (najbolje da kar vse). Če pogledaš ostale View-e (subclasse) vidiš, da imajo vsi konstruktor, ki dobi not še parameter attrs, tako da dodaj še ta konstrukotr:

public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
 
        this.setOnTouchListener(this);
 
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

public DrawView(Context context, AttributeSet attrs) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
 
        this.setOnTouchListener(this);
 
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

Dejcc ::

@illion: če se ne motrm morš dodat še tretji konstruktor:
public DrawView(Context context, AttributeSet attrs, int defStyle)  {}


Nato pa lahko svoj DrawView vključš v xml layout tako:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
    <com.package.DrawView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

<elementi ki jih hočeš še dodat />
</FrameLayout>

To pomen, da boš imel tvoj view za ozadje, čez pa lahko dodaš poljubne elemente.
nato pa v onCreate:
drawView = (DrawView) findViewById(R.id...)
in lahko dostopaš do metod.

Upam da pomaga :)

Zgodovina sprememb…

  • spremenil: Dejcc ()

g333kk ::

Ekola takole sem spremenil pa mi se crasha aplikacija..
   drawView = new DrawView(this, null, 0);
    setContentView(drawView);
    drawView.requestFocus();


   public DrawView(Context context, AttributeSet attrs, int defStyle) {

Dejcc ::

Ta tretji konstruktor sm dopisu za primer, če bi tvoj View vključu v layout v xml datoteki.
(ker te je zanimal kako risat pike -> setContentView(R.layout.activity_main); )

g333kk ::

sej sem ga..takole:
sem prekopiral sam zgleda je ena napaka tam :

<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:background="@color/crna"
   >
    <com.package.DrawView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/shrani" />

</RelativeLayout>

Zgodovina sprememb…

  • spremenilo: g333kk ()

g333kk ::

Ali pa če tole kaj spremenim:

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

Dejcc ::

com.package.DrawView - to spremen glede na to kje maš ti DrawView shranjen v projektu, pa dodaj še id, tko k maš za knof

Pa nism zihr če lahko to nardiš z RelativeLayout, najbrž bo treba zamenjat s FrameLayout. Sprobej pa povej.

Edit: narobe kličeš super(), poklicat ga morš s parametri:
super(context, attrs, defStyle);

Zgodovina sprememb…

  • spremenil: Dejcc ()

g333kk ::

Jah še kar napaka, pa sem spremenil:
(com.example.pii-tuakaj je shranjen drawview.java)


<FrameLayout 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:background="@color/crna" xmlns:android1="http://schemas.android.com/apk/res/android">

    <com.example.pii.DrawView
        android:id="@+id/DrawView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="@string/shrani" />

</FrameLayout>

Marat ::

A si ti že kdaj prej delal v Androidu, al s tem šele začenjaš? Ker mislim da moraš najprej pokapirat osnove... Poglej si tole knjigo: Hello, Android

g333kk ::

Eto take so napake:


09-01 10:08:01.568: W/dalvikvm(583): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-01 10:08:01.658: E/AndroidRuntime(583): FATAL EXCEPTION: main
09-01 10:08:01.658: E/AndroidRuntime(583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pii/com.example.pii.Draw}: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.pii.DrawView
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.os.Looper.loop(Looper.java:123)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 10:08:01.658: E/AndroidRuntime(583): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 10:08:01.658: E/AndroidRuntime(583): at java.lang.reflect.Method.invoke(Method.java:521)
09-01 10:08:01.658: E/AndroidRuntime(583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-01 10:08:01.658: E/AndroidRuntime(583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-01 10:08:01.658: E/AndroidRuntime(583): at dalvik.system.NativeStart.main(Native Method)
09-01 10:08:01.658: E/AndroidRuntime(583): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.pii.DrawView
09-01 10:08:01.658: E/AndroidRuntime(583): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-01 10:08:01.658: E/AndroidRuntime(583): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.Activity.setContentView(Activity.java:1647)
09-01 10:08:01.658: E/AndroidRuntime(583): at com.example.pii.Draw.onCreate(Draw.java:24)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 10:08:01.658: E/AndroidRuntime(583): ... 11 more
09-01 10:08:01.658: E/AndroidRuntime(583): Caused by: java.lang.NoSuchMethodException: DrawView(Context,AttributeSet)
09-01 10:08:01.658: E/AndroidRuntime(583): at java.lang.Class.getMatchingConstructor(Class.java:660)
09-01 10:08:01.658: E/AndroidRuntime(583): at java.lang.Class.getConstructor(Class.java:477)
09-01 10:08:01.658: E/AndroidRuntime(583): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
09-01 10:08:01.658: E/AndroidRuntime(583): ... 21 more

Dejcc ::

Bo pa treba logcat pogledat za napako. Možn da ne pusti met dvakrat
xmlns:android1="http://schemas.android.com/apk/res/android"


Edit: nekje sred ti piše: Caused by: java.lang.NoSuchMethodException: DrawView(Context,AttributeSet)

A maš vse 3 konstruktorje?

Zgodovina sprememb…

  • spremenil: Dejcc ()

g333kk ::

Mart sem že delal gumbe dodajal menjal viewe itd, sedaj sem se odločil da pritiskam z prstom in narišem nekaj :P ..

g333kk ::

Ja sicer sem vse na nul nastavu,ker ne vem zakj smo dodali spremenljivke,.. :

drawView = new DrawView(this, null, 0);
   setContentView(R.layout.main_action);
   drawView.requestFocus();



public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, 5, paint);
            // Log.d(TAG, "Painting: "+point);
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        Log.d(TAG, "point: " + point);
        return true;
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

g333kk ::

Ja sicer sem vse na nul nastavu,ker ne vem zakj smo dodali spremenljivke,.. :

drawView = new DrawView(this, null, 0);
   setContentView(R.layout.main_action);
   drawView.requestFocus();



public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, 5, paint);
            // Log.d(TAG, "Painting: "+point);
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        Log.d(TAG, "point: " + point);
        return true;
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

Dejcc ::

Ah dej no, a nč ne bereš :)
Rabš 3 konstruktorje. Ti maš samo enga. Dodej:
public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
 
        this.setOnTouchListener(this);
 
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }
 
public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

Sicer pa bo najbolš da pogledaš kaj ti je predlagu Marat.

g333kk ::

Najlepša hvala, za pomoč.. :)))
Ajoj, to je to ker wrninge ne upoštevam :P..
Zakaj pa 3 konstruktorje rabmo.(konstruktor je itak samo zato da pokličemo začetne vrednosti tko je blo v c++ )

illion ::

Sem sprobal to kodo in mi deluje brez problema, si res dodal vse 3 konstruktorje? ker v kodi zgoraj imaš samo enega, moraš pa dodat vse!

public class DrawView extends View implements OnTouchListener {
	private static final String TAG = "DrawView";

	List<Point> points = new ArrayList<Point>();
	Paint paint = new Paint();

	public DrawView(Context context) {
		this(context, null, 0);
	}

	public DrawView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DrawView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setFocusable(true);
		setFocusableInTouchMode(true);

		this.setOnTouchListener(this);

		paint.setColor(Color.RED);
		paint.setAntiAlias(true);
	}

	@Override
	public void onDraw(Canvas canvas) {
		for (Point point : points) {
			canvas.drawCircle(point.x, point.y, 5, paint);
			// Log.d(TAG, "Painting: "+point);
		}
	}

	public boolean onTouch(View view, MotionEvent event) {
		// if(event.getAction() != MotionEvent.ACTION_DOWN)
		// return super.onTouchEvent(event);
		Point point = new Point();
		point.x = event.getX();
		point.y = event.getY();
		points.add(point);
		invalidate();
		Log.d(TAG, "point: " + point);
		return true;
	}
}

class Point {
	float x, y;

	@Override
	public String toString() {
		return x + ", " + y;
	}
}



<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <com.example.pii.DrawView
        android:id="@+id/DrawView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="shrani" />

</FrameLayout>



tud kle deluje setContentView na oba načina:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
//        DrawView drawing= new DrawView(this);
//        setContentView(drawing);
        
        setContentView(R.layout.activity_main);
        
    }
    
}

g333kk ::

Jap problem so bili res samo 3 konstruktorji mi lahko en pove, zakaj se uprablja tale this in zakaj smo ptorebovali 3 konstruktorje, ker v c++ kaj takega nisem zasledil nikoli :P ..

illion ::

mal prepozno sm postal :D

3 konstruktorje rabiš, če uporabljaš DrawView v xml-ju, v tem primeru android sam naredi novo instanco DrawViewa in uporabi tisti konstruktor, ki ustreza argumentom v xmlju, ponavadi tistega z dvemi ali tremi argumenti, zato rabiš vse 3.

g333kk ::

oki hvala kaj pa glede this to je pa zelo popularna zadeva povsod this :P ..

illion ::

this je referenca classa samega nase, v konstuktorju se ga ponavadi uporabi, da kličeš drug konstruktor z drugimi parametri, da ti ni treba v vsakem podvajati kode. Drugače pa upam da veš za kaj vse se uporablja this in super, če ne, si preberi, ker to je osnova jave :)

Zgodovina sprememb…

  • spremenil: illion ()

g333kk ::

Ja neki takega(če sem iskren čisto ne razumem vendar sem si primer zapomnil :)..):
- super se nanaša na metodo nadrazreda
o super.izpisiVse() pokliče metodo razreda Student
- this se nanaša na metodo podrazreda
o this.izpisiVse() pokliče metodo razreda IzredniStudent
o referenco this običajno izpustimo

Em zakaj je boljš dp uprabljat kot pa px ..

Zgodovina sprememb…

  • spremenilo: g333kk ()

g333kk ::



Ekola takole sem sedaj naredil, pa me zanima, kako se da shranjevati slike..

illion ::

čist odvisno od tega, kaksno funkcionalnost bi rad. Na začetku, glede na to, da nimaš zapletenega sistema za risanje, lahko recimo exportaš cel List točk v nek file. Lahko uporabiš XML file (poglej SharedPreferences, izbereš nek fajl na sdkartici in tja zapišeš točke, barvo točk, ozadja itd..), potem pa naloadaš ta file ko uporabnik hoče editirat sliko.
Kasnej lahko dodaš še export v sliko, kle imaš nekaj primerov:
http://stackoverflow.com/questions/2174...

ok slo-tech ma nek problem s tem urljem očitno haha, kr sliko hoče narest iz njega :D
tole sm mislu:
http://stackoverflow.com/questions/2174875/android-canvas-to-jpg

Zgodovina sprememb…

  • spremenil: illion ()

g333kk ::

Sicer ali naj naredim tako da se sporotno shranjuje ali pa vse na koncu naenkkrat, kot sem gledal kodo bo moj knof za okras ker sem ga deklariral pri oncreate vendar imam class za risanje na novi strani..

Dejcc ::

Če hočeš shrant objekt, kjer maš vse te točke z barvami zapisane, lahko guglaš za gson (Shranš objekt v json s pomočjo sharedpreferences)
poglej si tole

g333kk ::

a potem mi ni potrebno shranjevati, tako kot mi je illion priporočal.

g333kk ::

Sedaj mi je uspelo, spravit zadevo v delno delovanje, Shrani mi 1. piko ki jo narišem(po novem se riše zelo počasi),..Vendar bi rad da mi shrani vse, ki jih narišem ne le prvo..

  public void onDraw(Canvas c) {
    	
      //  Paint paint = new Paint();
       // paint.setColor(Color.RED);  
    	  for (Point point : points) {
        c.drawCircle(point.x, point.y, 30, paint);

        try {
            getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));
        } catch (Exception e) {
            Log.e("Error--------->", e.toString());
        }
        super.onDraw(c);}
    }

Zgodovina sprememb…

  • spremenilo: g333kk ()

illion ::

Je počasno valda, ker za vsako piko rišeš v file! nared drugo funckijo recimo saveImage in tam notr shran sliko, poleg tega ni nujno da imajo vsi androidi kartico mountano na /mnt/sdcard, zato uporab Environment.getExternalStorageDirectory();

public void saveImage(Canvas c) {
       File file= Environment.getExternalStorageDirectory() + "/arun.jpg";
       

      try {
          getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(dir));
      } catch (Exception e) {
          Log.e("Error--------->", e.toString());
      }
  }


potem pa na save button click kliči saveImage();

g333kk ::

Veste kaj me zanima je možno da rišeš brez teh presledkov da bi risal polno črto, alpa je možno da bi risal črte alpa ka kvadrate, kroge itd, poljubne velikosti, pa kak predlog pride prav.Sicer se opravčijem ker sprašujem ampak zanimivo mi je tole programiranje sedaj sem dodal, da lahko izbirap prek spinnerja izbira barva potem brightness spreminjanje in povečavanje bunkic in zmanjšanje, sedaj mi bunkice niso dovolj kakovostno risanje bi rad izboljšal tako kot sem že omenil :p, kaka ideja predvsem od risanja ..

Zgodovina sprememb…

  • spremenilo: g333kk ()

Dejcc ::

Mal se potrud, pa poglej dokumentacijo.
Glede polne črte: med prejšnjo točko in novo črto nariš daljico.
Glede kvadratov itd maš vse tuki: klik

illion ::

pri navadni javi je blo treba implementirat double buffering če si hotel smooth risanje, pri androidu že canvas objekt skrbi za to, tko da vse kar rabiš, je pravilno risanje črte med točkami. Za samo risanje črte bi spet moral implemenitrat algoritm, ponavadi tegale: Bresenham's line algorithm @ Wikipedia , samo ti ni treba, ker imaš class Path, kamor filaš točke in potem ta Path enostavno narišeš na canvas prek drawPath() metode. Spodaj je tvoja koda, spremenjena za delo s Pathi, dodan je seznam Pathov, en Path se začne s pritiskom na zaslon in konča, ko dvigneš prst (takrat se doda na seznam). Class Point ni več potreben kot vidiš, je pa treba pri Paintu nastavit stroke naPaint.Style.STROKE. Za ostala raisanja (krogi/kvadrati itd..) poglej po googlu kakšni classi še obstajajo, lahko si tud sam zmisliš kaj, poleg tega lahko sprobavaš različne tiše Stroke-ov ipd..

public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";
    
    Path path;
    
    Vector<Path> paths = new Vector<Path>();
    
    Paint paint = new Paint();
 
    
    public DrawView(Context context) {
        this(context, null, 0);
    }
 
    public DrawView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
        setFocusableInTouchMode(true);
 
        this.setOnTouchListener(this);
 
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }
 
    @Override
    public void onDraw(Canvas canvas) {
    	if(path != null)
    		canvas.drawPath(path, paint);
    	for(Path p : paths)
    		canvas.drawPath(p, paint);
    }
 
    public boolean onTouch(View view, MotionEvent event) {
    	switch(event.getAction()) {
    	case MotionEvent.ACTION_DOWN :
    		path = new Path();
    		path.moveTo(event.getX(), event.getY());
    		break;
    	case MotionEvent.ACTION_UP :
    		paths.add(path);
    		path = null;
    		break;
    	case MotionEvent.ACTION_MOVE :
    		path.lineTo(event.getX(), event.getY());
    		break;
    	}
        
        invalidate();
        return true;
    }
}

g333kk ::

Hvala sicer sem pa jaz zelo napredoval imam pa spet problem, tako kot je ze standard pri programiranju :PPP
Odločil sem se da ne bom več risal po p objektu in da bom risal R.layout.prvi:Vendar me vedno razbije apliakcija:
Nalimal sem le izvlečke iz kode, če kdo potrebuje kaj mi naj pove :P

09-06 18:20:23.613: D/dalvikvm(328): GC freed 588 objects / 49456 bytes in 598ms
09-06 18:20:23.653: D/AndroidRuntime(328): Shutting down VM
09-06 18:20:23.663: W/dalvikvm(328): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-06 18:20:23.663: E/AndroidRuntime(328): Uncaught handler: thread main exiting due to uncaught exception
09-06 18:20:23.683: E/AndroidRuntime(328): java.lang.RuntimeException: Unable to start activity ComponentInfo{somitsolutions.training.android.androidpaint/somitsolutions.training.android.androidpaint.AndroidPaint}: android.view.InflateException: Binary XML file line #7: Error inflating class somitsolutions.training.android.androidpaint.AndroidPaint
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.os.Looper.loop(Looper.java:123)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-06 18:20:23.683: E/AndroidRuntime(328): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 18:20:23.683: E/AndroidRuntime(328): at java.lang.reflect.Method.invoke(Method.java:521)
09-06 18:20:23.683: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-06 18:20:23.683: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-06 18:20:23.683: E/AndroidRuntime(328): at dalvik.system.NativeStart.main(Native Method)
09-06 18:20:23.683: E/AndroidRuntime(328): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class somitsolutions.training.android.androidpaint.AndroidPaint
09-06 18:20:23.683: E/AndroidRuntime(328): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-06 18:20:23.683: E/AndroidRuntime(328): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.Activity.setContentView(Activity.java:1622)
09-06 18:20:23.683: E/AndroidRuntime(328): at somitsolutions.training.android.androidpaint.AndroidPaint.onStart(AndroidPaint.java:179)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.Activity.performStart(Activity.java:3723)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
09-06 18:20:23.683: E/AndroidRuntime(328): ... 11 more
09-06 18:20:23.683: E/AndroidRuntime(328): Caused by: java.lang.NoSuchMethodException: AndroidPaint(Context,AttributeSet)
09-06 18:20:23.683: E/AndroidRuntime(328): at java.lang.Class.getMatchingConstructor(Class.java:674)
09-06 18:20:23.683: E/AndroidRuntime(328): at java.lang.Class.getConstructor(Class.java:486)
09-06 18:20:23.683: E/AndroidRuntime(328): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
09-06 18:20:23.683: E/AndroidRuntime(328): ... 22 more
09-06 18:20:23.723: I/dalvikvm(328): threadid=7: reacting to signal 3
09-06 18:20:23.723: E/dalvikvm(328): Unable to open stack trace file '/data/anr/traces.txt': Permission denied


koda
package somitsolutions.training.android.androidpaint;
public class AndroidPaint extends Activity {
    public void onStart(){
    	super.onStart();
    	//p.surfaceCreated(p.getHolder());
    	p = new Panel(this);
      //  setContentView(p/*new Panel(this)*/);
        setContentView(R.layout.prva);
       p=(Panel)findViewById(R.id.DrawView);
        		// mView =(TesterView) findViewById (R.id.DrawView);
        
    	//setContentView(p); <-- deluje odlično 
    
         }*/
        /* int n = graphicobjects.size();
         if (getLastNonConfigurationInstance() != null)
         {
         	//GraphicObject = (Shape)getLastNonConfigurationInstance();
         	//graphicobjects.set(number_of_graphicObjects-1, (Shape)getLastNonConfigurationInstance());
         	//graphicobjects.addAll((Shape)getLastNonConfigurationInstance());
        	 Object[] temp = getLastNonConfigurationInstance();
         	for(int i = 0; i<n; i++){
         		graphicobjects.set(i,(Shape)getLastNonConfigurationInstance());
         	}
         }*/
    }
}
    
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
}


xml koda
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <somitsolutions.training.android.androidpaint.AndroidPaint
             android:id="@+id/DrawView"
             android:layout_width="fill_parent"
            android:layout_height="fill_parent"
             />
       
  

</RelativeLayout>

matej94 ::

Emm maš ti tole v istem dokumentu ..

Zgodovina sprememb…

  • spremenilo: matej94 ()

g333kk ::

emm ja če misliš clasa panel in androidpaint sta skupaj v 1 androidpain.java.

Zgodovina sprememb…

  • spremenilo: g333kk ()


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[Android] Splošno

Oddelek: Programiranje
351934 (1064) piki12
»

Programiranje problem android

Oddelek: Programiranje
51088 (853) g333kk
»

[Android] Aplikacija se zruši

Oddelek: Programiranje
6739 (576) g333kk
»

[android] vstavljanje slike

Oddelek: Programiranje
71138 (1035) messi
»

[android] crash

Oddelek: Programiranje
61258 (1067) messi

Več podobnih tem