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


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
آموزش ساخت چراغ قوه کامل + سورس

آموزش ساخت چراغ قوه کامل + سورس

#1
با سلام و عرض ادب خدمت کاربران گرامی
در این مطلب قصد داریم تا یک چراغ قوه تر تمیز و استاندارد رو طراحی کنیم که با کمترین کد و در یک اکتیویتی تمام کدها رو بنویسیم .

قابلیت ها:
- فوق العاده سبک و کم حجم 
- دارای ویجت اختصاصی
- کد نویسی بهینه و استاندارد
- هیچ گونه تبلیغاتی در نرم افزار وجود ندارد
- دسترسی آسان
- کاربرد ساده و بدون معتلی
- پشتیبانی از نگارش اندروید 2 به بالا 

 خوب بریم سراغ آموزش ساخت چراغ قوه - چندتا عکس از برنامه پیوست شد .
فایل main.xml :

کد پی‌اچ‌پی:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
xmlns:tools="http://schemas.android.com/tools"
    
android:id="@+id/main"
    
android:layout_width="match_parent"
    
android:layout_height="match_parent"
    
android:background="@drawable/bg_off"
    
tools:context="co.tooba.flashlight.Main" >

        <
Button
            android
:id="@+id/btnOn"
            
style="?android:attr/buttonStyleSmall"
            
android:layout_width="wrap_content"
            
android:layout_height="wrap_content"
            
android:layout_alignParentBottom="true"
            
android:layout_alignParentLeft="true"
            
android:layout_marginBottom="15dp"
            
android:layout_marginLeft="15dp"
            
android:width="80dp"
            
android:onClick="flashLightOn"
            
android:text="روشن" />
        
       <
Button
            android
:id="@+id/btnOff"
            
style="?android:attr/buttonStyleSmall"
            
android:layout_width="wrap_content"
            
android:layout_height="wrap_content"
            
android:layout_alignParentBottom="true"
            
android:layout_alignParentLeft="true"
            
android:layout_marginBottom="15dp"
            
android:layout_marginLeft="15dp"
            
android:width="80dp"
            
android:onClick="flashLightOff"
            
android:text="خاموش" />  

</
RelativeLayout

فایل Main.java :

کد پی‌اچ‌پی:
package co.tooba.flashlight;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class 
Main extends  Activity {

    public static 
Camera cam null;
    private static 
long back_pressed;
    private 
Button btnOn btnOff;
    
RelativeLayout layout;
    
    protected 
void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        
setContentView(R.layout.main);
        
        
btnOn = (Button)findViewById(R.id.btnOn);
        
btnOff = (Button)findViewById(R.id.btnOff);
        
btnOff.setVisibility(View.INVISIBLE);
        
layout =(RelativeLayoutfindViewById(R.id.main);

    }


    public 
void flashLightOn(View view) {
        
        try {
            if (
getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                
cam Camera.open();
                
Parameters p cam.getParameters();
                
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                
cam.setParameters(p);
                
cam.startPreview();
                
                
btnOn.setVisibility(View.INVISIBLE);
                
btnOff.setVisibility(View.VISIBLE);
                
layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_on));

            }
        } catch (
Exception e) {
            
e.printStackTrace();
            
Toast.makeText(getBaseContext(), "گوشی شما این قابلیت را پشتیبانی نمی کند"Toast.LENGTH_SHORT).show();
        }
    }

    public 
void flashLightOff(View view) {
        try {
            if (
getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                
cam.stopPreview();
                
cam.release();
                
cam null;
                
                
btnOff.setVisibility(View.INVISIBLE);
                
btnOn.setVisibility(View.VISIBLE);
                
layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_off));
                
            }
        } catch (
Exception e) {
            
e.printStackTrace();
            
Toast.makeText(getBaseContext(), "گوشی شما این قابلیت را پشتیبانی نمی کند"Toast.LENGTH_SHORT).show();
        }
    }

    public 
void onBackPressed(){
        if (
back_pressed 2000 System.currentTimeMillis()){
            
super.onBackPressed();
        }
        else{
            
Toast.makeText(getBaseContext(), "جهت خروج دوباره کلیک کنید."Toast.LENGTH_SHORT).show();
            
back_pressed System.currentTimeMillis();
        }
    }


فایل AndroidManifest.xml :

کد پی‌اچ‌پی:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="co.tooba.flashlight"
    android:versionCode="1"
    android:versionName="1.0" >

     <uses-permission android:name="android.permission.CAMERA" />
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <activity
            android:name=".Main"
            android:label="@string/app_name" 
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        
         <receiver android:name=".AppWidget" android:label="@string/app_name">
              <intent-filter>
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
              </intent-filter>
              <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
        </receiver>
        
         <receiver android:name="FlashlightWidgetReceiver">
                <intent-filter>
                       <action android:name="COM_FLASHLIGHT"></action>
                </intent-filter>
         </receiver>
                
    </application>

</manifest> 

فانکشن flashLightOn برای روشن کردن فلش دوربین دیوایس می باشد . در صورتی که گوشی مورد نظر این قابلیت را نداشته باشد ،پیامی نمایش داده میشود .
فانکشن flashLightOff برای خاموش کردن فلش دوربین می باشد .
در هر setBackgroundDrawable جهت تغییر عکس پس زمینه اکتیویتی استفاده شده است .
دستور setVisibility(View.VISIBLE) برای مخفی کردن Button و دستور setVisibility(View.INVISIBLE) برای نمایان کردن Button می باشد .کدهای به کار برده خیلی ساده و قابل فهم می باشند . در صورت وجود مشکل میتوانید سوالات خودتان را  پست کنید .
 
دانلود سورس پروژه


فایل‌های پیوست

عکس(ها)
                   

دانلود Flashlight.zip

نام فایل Flashlight.zip
نوع فایل .zip
دفعات دانلود 309
اندازه 2.44 MB
ارسال کننده فایل Ali Majidi
پاسخ
 سپاس شده توسط admin ، ramtin2080 ، Erfannj ، شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


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