انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
عدم درک چگونگی فراخوانی بعضی از روش ها (methods) در کد برنامه اندروید

عدم درک چگونگی فراخوانی بعضی از روش ها (methods) در کد برنامه اندروید

#2
سلام.
من کد MainActivity.java را ذکر می کنم :

کد پی‌اچ‌پی:
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(202015paint);  
  
            
// draw green circle with anti aliasing turned on  
            
paint.setAntiAlias(true);  
            
paint.setColor(Color.GREEN);  
            
canvas.drawCircle(602015paint);  
  
            
// draw red rectangle with anti aliasing turned off  
            
paint.setAntiAlias(false);  
            
paint.setColor(Color.RED);  
            
canvas.drawRect(100520030paint);  
                           
            
// draw the rotated text  
            
canvas.rotate(-45);  
                      
            
paint.setStyle(Paint.Style.FILL);  
            
canvas.drawText("Graphics Rotation"40180paint);  
              
            
//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.mainmenu);  
        return 
true;  
    }  


خوب ابتدا باید ببینیم که در روش onCreate که به محض شروع Activity ، اجرا می شود، چه کدهایی نوشته شده :

کد پی‌اچ‌پی:
@Override  
public void onCreate(Bundle savedInstanceState) {  
    
super.onCreate(savedInstanceState);  
    
demoview = new DemoView(this);  
    
setContentView(demoview);  


خط زیر از کد بالا را ببینید :

کد پی‌اچ‌پی:
demoview = new DemoView(this); 

در آن، از کلاس (Class) با نام DemoView ، یک شیء (object) با نام demoview ساخته ایم (با دیدن کلمه new ، متوجه ساخت شیء از روی کلاس می شویم). کدهای مربوط به تعریف کلاس DemoView در خود کدهای این Activity وجود دارد :

کد پی‌اچ‌پی:
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(202015paint);  

        
// draw green circle with anti aliasing turned on  
        
paint.setAntiAlias(true);  
        
paint.setColor(Color.GREEN);  
        
canvas.drawCircle(602015paint);  

        
// draw red rectangle with anti aliasing turned off  
        
paint.setAntiAlias(false);  
        
paint.setColor(Color.RED);  
        
canvas.drawRect(100520030paint);  
                       
        
// draw the rotated text  
        
canvas.rotate(-45);  
                  
        
paint.setStyle(Paint.Style.FILL);  
        
canvas.drawText("Graphics Rotation"40180paint);  
          
        
//undo the rotate  
        
canvas.restore();  
    }  


که در آن، روش (method) با نام onDraw تعریف شده که با ساخت یک شیء از این کلاس، اجرا خواهد شد :

کد پی‌اچ‌پی:
@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(202015paint);  

    
// draw green circle with anti aliasing turned on  
    
paint.setAntiAlias(true);  
    
paint.setColor(Color.GREEN);  
    
canvas.drawCircle(602015paint);  

    
// draw red rectangle with anti aliasing turned off  
    
paint.setAntiAlias(false);  
    
paint.setColor(Color.RED);  
    
canvas.drawRect(100520030paint);  
                   
    
// draw the rotated text  
    
canvas.rotate(-45);  
              
    
paint.setStyle(Paint.Style.FILL);  
    
canvas.drawText("Graphics Rotation"40180paint);  
      
    
//undo the rotate  
    
canvas.restore();  



bookbook 
لطفا برای درج کد، از دکمه مخصوص درج کد در ادیتور انجمن استفاده کنید.
در مورد برنامه نویسی، مدیران تنها راهنمایی می کنند و نوشتن برنامه نهایی، به عهده کاربران می باشد (اینجا محلی برای یادگیری است، نه سفارش کدنویسی).
کاربران باید ابتدا خود به خطایابی برنامه بپردازند، نه اینکه به محض دیدن خطا، کدها را در انجمن، copy و paste کرده و از مدیران انتظار بررسی داشته باشند.
پاسخ
 سپاس شده توسط داش بهروز ، yousefesi


پیام‌های این موضوع
RE: عدم درک چگونگی فراخوانی بعضی از روش ها (methods) در کد برنامه اندروید - توسط admin - ۱۳۹۳/۱۲/۲۰, ۰۲:۴۰ ق.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان