آموزش برنامه نویسی اندروید

saalek110

Well-Known Member
opengl
یک برنامه در اوایل تاپیک از سایت زیر:
A simple example of using an Android Renderer to illustrate OpenGL ES boilerplate.
با کد زیر:
JavaScript:
/* GraphicGlDemoActivity.java
* Author: Yong Bakos
* Since: 11/26/2012
* Thanks to:
* Cube: http://intransitione.com/blog/create-a-spinning-cube-with-opengl-es-and-android/
* OpenGL Boilerplate: http://www.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/
*/
package com.humanoriented.sudoku;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class GraphicGlDemoActivity extends Activity {
public class DemoRenderer implements Renderer {
private Cube cube = new Cube();
private float rotation;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
// Depth buffer setup.
gl.glClearDepthf(1.0f);
// Enables depth testing.
gl.glEnable(GL10.GL_DEPTH_TEST);
// The type of depth testing to do.
gl.glDepthFunc(GL10.GL_LEQUAL);
// Really nice perspective calculations.
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// Sets the current view port to the new size.
gl.glViewport(0, 0, width, height);
// Select the projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
// Reset the projection matrix
gl.glLoadIdentity();
// Calculate the aspect ratio of the window
GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
// Select the modelview matrix
gl.glMatrixMode(GL10.GL_MODELVIEW);
// Reset the modelview matrix
gl.glLoadIdentity();
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -10.0f);
gl.glRotatef(rotation, 1.0f, 1.0f, 1.0f);
cube.draw(gl);
gl.glLoadIdentity();
rotation -= 0.15f;
}
}
class Cube {
private FloatBuffer mVertexBuffer;
private FloatBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
private float vertices[] = {
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f
};
private float colors[] = {
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f
};
private byte indices[] = {
0, 4, 5, 0, 5, 1,
1, 5, 6, 1, 6, 2,
2, 6, 7, 2, 7, 3,
3, 7, 4, 3, 4, 0,
4, 7, 6, 4, 6, 5,
3, 0, 1, 3, 1, 2
};
public Cube() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mVertexBuffer = byteBuf.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mColorBuffer = byteBuf.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE,
mIndexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new DemoRenderer());
setContentView(view);
}
}
پست شد که بهتره در 3 فایل تقسیم بشود. دو کلاس داخل کد هست که ببرید به فایلهای جاوای جدید.
مکعب چرخان:
a1.jpg
در oncreate کلاس اصلی :
JavaScript:
    @Override
    protected void onCreate(Bundle state) {   //Bundle savedInstanceState
       // super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);
        super.onCreate(state);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        GLSurfaceView view = new GLSurfaceView(this);
        view.setRenderer(new DemoRenderer());
        setContentView(view);
    }
همان طور که می بینید پنجره جدید تنظیم شده.
در کلاس DemoRenderer که از Renderer مشتق شده...
JavaScript:
public class DemoRenderer implements Renderer {
    private Cube cube = new Cube();
    private float rotation;
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    // Depth buffer setup.
    gl.glClearDepthf(1.0f);
    // Enables depth testing.
    gl.glEnable(GL10.GL_DEPTH_TEST);
    // The type of depth testing to do.
    gl.glDepthFunc(GL10.GL_LEQUAL);
    // Really nice perspective calculations.
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    }
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
    // Sets the current view port to the new size.
    gl.glViewport(0, 0, width, height);
    // Select the projection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);
    // Reset the projection matrix
    gl.glLoadIdentity();
    // Calculate the aspect ratio of the window
    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
    // Select the modelview matrix
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    // Reset the modelview matrix
    gl.glLoadIdentity();
    }
    @Override
    public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glTranslatef(0.0f, 0.0f, -10.0f);
    gl.glRotatef(rotation, 1.0f, 1.0f, 1.0f);
    cube.draw(gl);
    gl.glLoadIdentity();
    rotation -= 0.15f;
    }
    }//class DemoRenderer
ابتدا یک آبجکت از کلاس مکعب ساخته شده:
JavaScript:
private Cube cube = new Cube();
و بعد یک متغیر float برای چرخش مکعب ایجاد شده.
JavaScript:
private float rotation;
و بعد 3 تابع داریم.
onSurfaceCreated و onSurfaceChanged و onDrawFrame

در مورد اولی:
منبع می گوید:
In onSurfaceCreated, you initialize your program and your initial configurations.
This method is called once for each Surface’s view’s cycle.
But the Surface can be destroyed and this method will be called when the next one is created.
متد اولی یعنی onSurfaceCreated طبق گفته سایت بالا فقط یک بار اجرا میشه. برخلاف سومی یعنی onDrawFrame که گفته very often اجرا میشه. یعنی خیلی زیاد.
در سومین متد یعنی onDrawFrame می بینید متد Draw مکعب فراخوانی شده.
JavaScript:
@Override
    public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glTranslatef(0.0f, 0.0f, -10.0f);
    gl.glRotatef(rotation, 1.0f, 1.0f, 1.0f);
    cube.draw(gl);
    gl.glLoadIdentity();
    rotation -= 0.15f;
    }
JavaScript:
cube.draw(gl);
تابع glTranslatef که قبل رسم مکعب آمده برای تغییر مکان شکل به جای مناسب است.
و glRotatef برای چرخاندن شکل است.
در خط آخر متد onDrawFrame داریم:
JavaScript:
rotation -= 0.15f;
و در تابع چرخش داشتیم:
JavaScript:
    gl.glRotatef(rotation, 1.0f, 1.0f, 1.0f);
مقدار متغیر چرخش مدام در حال تغییر است چون متد onDrawFrame مدام اجرا می شود.
پس مکعب مدام در حال چرخش خواهد بود.
 
آخرین ویرایش:

saalek110

Well-Known Member
دوستانی که opengl جزو برنامه اشان نیست این پستها را ندیده بگیرند و به بقیه پست های تاپیک توجه کنند. وابستگی وجود ندارد و مشکلی پیش نمی آید.
 

پیوست ها

  • a1.gif
    a1.gif
    39.8 کیلوبایت · بازدیدها: 0
  • a2.gif
    a2.gif
    44.8 کیلوبایت · بازدیدها: 0
  • a3.gif
    a3.gif
    32.2 کیلوبایت · بازدیدها: 0
  • a4.gif
    a4.gif
    48.2 کیلوبایت · بازدیدها: 0
  • a5.gif
    a5.gif
    52.8 کیلوبایت · بازدیدها: 0

saalek110

Well-Known Member
Android Simple Graphics Example - javatpoint

sg.jpg

PHP:
package com.example.simplegraphics; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.View; 
 
public class MainActivity extends Activity { 
 
    DemoView demoview; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        demoview = new DemoView(this); 
        setContentView(demoview); 
    } 
 
    private class DemoView extends View{ 
        public DemoView(Context context){ 
            super(context); 
        } 
 
        @Override protected void onDraw(Canvas canvas) { 
            super.onDraw(canvas); 
 
            // custom drawing code here 
            Paint paint = new Paint(); 
            paint.setStyle(Paint.Style.FILL); 
 
            // make the entire canvas white 
            paint.setColor(Color.WHITE); 
            canvas.drawPaint(paint); 
             
            // draw blue circle with anti aliasing turned off 
            paint.setAntiAlias(false); 
            paint.setColor(Color.BLUE); 
            canvas.drawCircle(20, 20, 15, paint); 
 
            // draw green circle with anti aliasing turned on 
            paint.setAntiAlias(true); 
            paint.setColor(Color.GREEN); 
            canvas.drawCircle(60, 20, 15, paint); 
 
            // draw red rectangle with anti aliasing turned off 
            paint.setAntiAlias(false); 
            paint.setColor(Color.RED); 
            canvas.drawRect(100, 5, 200, 30, paint); 
                          
            // draw the rotated text 
            canvas.rotate(-45); 
                     
            paint.setStyle(Paint.Style.FILL); 
            canvas.drawText("Graphics Rotation", 40, 180, paint); 
             
            //undo the rotate 
            canvas.restore(); 
        } 
    } 
    @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); 
        return true; 
    } 
}
 

saalek110

Well-Known Member
اردیبهشت 1400: پستها را مرور کردم و بعضی قسمتها شرح کمکی گذاشتم.


در پستهای قبلی در اتصال به سایت ناموفق بودیم. بنابراین به استفاده از کتابخانه ها روی می آوریم. مثل Volley
 
آخرین ویرایش:

saalek110

Well-Known Member
نصب والی




  • Add volley.jar to the libs folder. Don't add it to the build path.
  • In Eclipse go to Project and make sure Build Automatically is selected.
  • Now in Project, click on Clean:
    Project -> select Clean -> select the project.
  • As Volley doesn't have any resources like styles, layouts etc., its not necessary to reference it as a library project. The above should work, provided all steps are followed.
  • If this doesn't work ...
  • Make sure the library has not been added to the build path. If it is, then remove it. Now, try:
  • Closing and re-opening the project.
  • Restarting the IDE (last resort).
 
آخرین ویرایش:

saalek110

Well-Known Member
من وقتی می خواستم از کدهای والی استفاده کنم خطای زمان اجرا می داد. علتش را پیگیری نکردم.
 

hanie9988

Member
۲. **زبان:** زبان رسمی آمریکا انگلیسی است.

۳. **سیستم حکومتی:** آمریکا یک جمهوری فدرال با سیستم حکومتی دموکراتیک دارد.

۴. **تاریخ:** آمریکا دارای تاریخ متنوع و پررنگی است که شامل دوران کلونی‌ها، استقلال از انگلیس، جنگ داخلی، و دوران پس از جنگ جهانی دوم می‌شود.

۵. **اقتصاد:** اقتصاد آمریکا یکی از بزرگترین اقتصادهای جهان است و به عنوان یک قدرت اقتصادی و نظامی برجسته شناخته می‌شود.

۶. **فرهنگ و هنر:** مهاجرت به آمریکا اخبار مهاجرت
 

جدیدترین ارسال ها

بالا