Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Cách vẽ đồ thị 2D trong android?

    Mình mới học android , có 1 bài vẽ đồ thị trên java . Nhưng khi mình chuyển sang android thì ko biết phải lấy gì để hiển thị form vẽ đồ thị đó. Cụ thế là mình có tạo 1 class DrawForm :

    Mã:
    package com.example.androidapplication4; import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Graphics;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;  public class DrawForm extends JFrame{        private int max_x = 20;    private int ax1,ax2,by1,by2 =0;    float[] value = new float[3];    public DrawForm(float [] par,String title){        super (title);        value = par;        setLayout(new FlowLayout(FlowLayout.CENTER));        setSize(600,600);        setResizable(false);        setLocation(400, 90);        this.addWindowListener(new WindowAdapter(){            @Override            public void windowClosing(WindowEvent e){                System.exit(0);            }        });    }    @Override    public void paint(Graphics g){        g.drawLine(0,300,900,300);        g.drawLine(300, 0,300, 900);        int x1[]={300,295,305};        int y1[]={22,32,32};        g.fillPolygon(x1, y1, 3);        int x2[]={598,588,588};        int y2[]={300,295,305};        g.fillPolygon(x2, y2, 3);         g.setColor(Color.blue);        g.setFont(new Font("Tahoma",Font.BOLD,15));        g.drawString("y",280,45);        g.setColor(Color.blue);        g.setFont(new Font("Tahoma",Font.BOLD,15));        g.drawString("x",575,290);        g.setColor(Color.blue);        g.setFont(new Font("Tahoma",Font.BOLD,15));        g.drawString("0",280,320);        g.drawLine(ax1, by1, ax2, by2);         float xMax = 600;        float yMax = 600;        float x0 = xMax / 2;         float y0 = yMax / 2;        float x  =  -max_x;        float k  =  3;        float dx  =  (float) 0.01;         float fx1,fx2;        fx1  =  calc(x);        ax1  =  (int)(x0 + x*k);        by1  =  (int)(y0 - fx1*k);         while(x <= max_x){            fx2 =  calc(x);            ax2 =  (int) (x0 + (x*k));            by2 =  (int) (y0 - fx2*k);            g.drawLine(ax1, by1, ax2, by2);            ax1  =  ax2;            by1  =  by2;            x  +=  dx;        }    }    public float calc(float x){            return(value[0]*x*x+value[1]*x+value[2]);    }}
    Sau đó mình tạo 1 giao diện trong file xml để nhập 3 số a,b,c:
    Mã:

    Mã:
    <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=".MainActivity" >     <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginTop="36dp"        android:text="@string/a" />     <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="40dp"        android:text="@string/b" />     <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView2"        android:layout_below="@+id/textView2"        android:layout_marginTop="50dp"        android:text="@string/c" />     <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView1"        android:layout_alignBottom="@+id/textView1"        android:layout_alignParentRight="true"        android:ems="10" >         <requestFocus />    </EditText>     <EditText        android:id="@+id/editText2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView2"        android:layout_alignBottom="@+id/textView2"        android:layout_alignRight="@+id/editText1"        android:ems="10" />     <EditText        android:id="@+id/editText3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/textView3"        android:layout_alignRight="@+id/editText2"        android:ems="10" />     <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="@string/Draw" /> </RelativeLayout>
    Vấn đề nằm trong hàm main , trước đây trong java sau khi mình gọi hàm repaint() thì nó sẽ vẽ 1 form mới hiển thị đồ thị. Trong android mình đoán là phải làm 1 file xml thứ 2 để hiển thị, nhưng mình ko biết phải làm chính xác thế nào , ai biết chỉ dùm mình
    file MainActivity :
    Mã:

    Mã:
    package com.example.androidapplication4; import android.view.View; import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.Button;import android.widget.EditText; public class MainActivity extends Activity {      float [] value=new float[3];     public DrawForm form;     EditText a,b,c;     Button draw1 ;            @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        a =(EditText)findViewById(R.id.editText1);        b =(EditText)findViewById(R.id.editText2);        c =(EditText)findViewById(R.id.editText3);        draw1=(Button)findViewById(R.id.button1);    }     @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        draw1.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                // TODO Auto-generated method stub                value[1]=Float.parseFloat(a.getText().toString());                value[2]=Float.parseFloat(b.getText().toString());                value[3]=Float.parseFloat(c.getText().toString());                form=new DrawForm(value,"dongchuoi");                form.setVisible(true);               // repaint();                            }        });        return true;    } }

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn có thể sử dụng SurfaceView và vẽ trong onDraw
    Hoặc, tạo ra một view kế thừa từ GLSurfaceView rồi tạo một Render để xử lý vẽ đồ thị sẽ tốt hơn

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trong Android không dùng thư viện awt và swing.
    Để vẽ form, thay vì bạn tạo class extends JFrame thì bạn có thể tạo một class extends View và viết lại hàm onDrawn() giống như hàm paint().
    Khi nào muốn vẽ lại hình, bạn không gọi repaint() mà gọi view.invalidate().

    Để chuyển cửa sổ bạn có thể sử dụng Dialog.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •