Tuesday 27 June 2017

Name Live Wallpaper Example Android

Android Live Wallpaper:
This tutorial describes about how to implement the live wallpaper for android.
Steps to develop live wallpaper:
Step 1:
·         Create an XML folder in resource(res) folder.
Right click on res->new->directory-> enter xml and click ok.
·         Create an xml resource file for wallpaper as follows wallpaper.xml
write your description and icon

<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
   
android:description="@string/wallpaper_service_description"
   
android:settingsActivity="blog.thiru.livewallpaper.WallpaperSettingsActivity"
   
android:thumbnail="@drawable/thumb" />

Here description attribute is the wallpaper description will be shown in list of wallpapers and thumbnail is the icon that displays in wallpaper list. If you want Settings Activity create a settings activity by extending AppCompatActivity. If you don’t want to change the settings of the live wallpapers, just remove the android:settingsActivity attribute.

Step 2:
Create Wallpaper settings activity.
There are 3 ways to create the wallpaper settings activity

·         Create an activity extending the AppcompatActivity
·         Create a class and extends PreferenceFragment.
·         Create a class and extend to PreferenceActivity.
Here I used first approach.

Step 3:   WallpaperService Class

Create a Service class which extends WallpaperService class, which is base class for all live wallpapers in the systems. Need to implement onCreateEngine() method and return an object o type android.service.wallpaper.WallpaperService.Engine. The Engine class has few Life cycle methods such as,
  • onCreate();
  • onSurfaceCreated();
  • onVisibilityChanged();
  • onOffsetChanged();
  • onTouchEvent();
  • onCommand();
public class Wallpaper extends WallpaperService {
    private static final String TAG = "Wallpaper";
    int positionX, postionY;
   
@Override     public void onCreate() {         super.onCreate();     }     @Override     public void onDestroy() {         super.onDestroy();     }     @Override     public Engine onCreateEngine() {         return new WallpaperEngine();     }
    class WallpaperEngine extends Engine {
      private final Handler handler = new Handler();
      private final Runnable drawRunner = new Runnable() {
         @Override           public void run() {
            draw();
           }
          };
      WallpaperEngine() {
       nameImage = Utils.bitmap = textBitmap;
       bgImage = BitmapFactory.decodeResource(getResources(),R.drawable.background);
       postionX=-130; 
       postionY=200; 
       }
   public void onCreate(SurfaceHolder surfaceHolder){
        super.onCreate(surfaceHolder);
    }
    @Override   
    public void onVisibilityChanged(boolean mVisible)
     {
        this.mVisible = mVisible;
        if (mVisible){
            handler.post(drawRunner);
        }
        else
            handler.removeCallbacks(drawRunner);
     }
    @Override    public void onSurfaceDestroyed(SurfaceHolder holder){
        super.onSurfaceDestroyed(holder);
        this.mVisible = false;
        handler.removeCallbacks(drawRunner);
    }
    public void onOffsetsChanged(float xOffset,float yOffset,float xStep,float yStep,int xPixels,int yPixels){
        draw();
    }
    void draw()
    {
        final SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
        try{
            c = holder.lockCanvas();
            c.drawColor(Color.BLACK);
            if (c != null){
                c.drawBitmap(bgImage, 0, 0, null);
                c.drawBitmap(nameImage, postionX,postionY, null);
                c.drawBitmap(nameImage, postionY,postionX, null);
                int width=c.getWidth();
                if( postionX>width+100){
                   postionX=-130;
                }
               postionX = postionX+1;
               postionY = postionY+1;
            }
        }
        finally{  
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
        handler.removeCallbacks(drawRunner);
        if (mVisible){
            handler.postDelayed(drawRunner, 10); // delay 10 mileseconds        }
    }
}

Step 4:
Here is the Main Activity:
public class HomeActivity extends AppCompatActivity{
    private AlertDialog.Builder alertDialog;
    private View view;
    private EditText fontText;
    private TextView text1;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        text1 = (TextView) findViewById(R.id.txtToBitmap);
    }
    private void initDialog() {
        alertDialog = new AlertDialog.Builder(this);
        view = getLayoutInflater().inflate(R.layout.font_dialog, null);
        fontText = (EditText) view.findViewById(R.id.fontText);
        removeView();
        alertDialog.setView(view);
        alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {
                String font = fontText.getText().toString();
                if(!font.equals("")) {
                    text1.setText(font);
                    text1.setTypeface(Typeface.createFromAsset(
                            text1.getContext().getAssets(),
                            "fonts/chunq_dipped.ttf"));
                    text1.post(new Runnable() {
                        @Override                        public void run() {
                            Bitmap textBitmap = getBitmapFromView(text1);
                            Utils.bitmap = textBitmap;
                        }
                    });
                    Intent intent = new Intent(
                            WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
                    intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                            new ComponentName(HomeActivity.this, MyWallpaperService.class));
                    startActivity(intent);
                }
                dialog.dismiss();
            }
        });
    }
    private void removeView() {
        if (view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
    }
    public Bitmap getBitmapFromView(TextView view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        bgDrawable.draw(canvas);
        view.draw(canvas);
        return returnedBitmap;
    }
}
activity_home.xml:
<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:background="@drawable/home_bg"    
tools:context="com.ddrinfosystems.nameart.HomeActivity">
<TextView    
android:id="@+id/txtToBitmap"   
android:layout_width="wrap_content"    
android:layout_height="wrap_content"    
android:textSize="32sp"    
android:textColor="#FFFF00"    
android:visibility="invisible"
    android:layout_marginLeft="10dp"    
android:layout_marginStart="10dp"    
android:layout_marginBottom="70dp" />
</RelativeLayout>

font_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    
xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical"    
android:layout_width="match_parent"    
android:layout_height="match_parent">
    <EditText        
android:id="@+id/fontText"        
android:layout_width="match_parent"       
android:layout_height="wrap_content"       
android:padding="16dp"        
android:hint="Enter your name"        android:layout_margin="16dp">
    </EditText>
</LinearLayout>

Utils class:
public class Utils {
    static Bitmap bitmap;
}
Step 5:
Setting up Manifest.xml
Live Wallpaper is a service, need to register the service in your manifest file. The Manifest file seems like..
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="blog.thiru.livewallpaper"
    android:versionCode="1"
    android:versionName="1.0" >
   <uses-sdk
       android:minSdkVersion="15"
       android:targetSdkVersion="25" />
    <uses-feature android:name="android.software.live_wallpaper" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/wallpaper_application_title"
        android:theme="@style/AppTheme" >
        <service
            android:name=".Wallpaper"
            android:label="@string/wallpaper_service_title"
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/wallpaper" />
        </service>
        <activity
            android:name=".WallpaperSettingsActivity"
            android:exported="true"
            android:label="@string/pref_screen_title" >
        </activity>
        <activity
            android:name=".WallpaperActivity"
            android:label="@string/wallpaper_application_title" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Here it is just sample. You can customize the draw() method for your interest. 

Thursday 1 June 2017

Top Companies and their founders
1. Google— Larry Page & Sergey Brin 2. Facebook— Mark Zuckerberg 3. AAlkatel A3 Jeff Bezos

3. Yahoo— David Filo & Jerry Yang 4. Twitter— Jack Dorsey & Dick Costolo Sparx Canvas Shoes
5. Internet— Tim Berners-Lee 6. LinkedIn— Reid Hoffman, Allen Blue&Koonstantin Guericke 7. Email— Shiva Ayyadurai Click here for more details
8. Gtalk— Richard Wah-kan 9. Whatsapp— Brian Acton and Jan Koum 10. Hotmail— Sabeer Bhatia 11. Orkut— Buyukkokten 12. Wikipedia— Jimmy Wales 13. You tube— Steve Chen, Chad Hurley &JawedKarim 14. Rediffmail— Ajit Balakrishnan 15. Nimbuzz— Martin Smink & Evert Jaap Lugt 16. Myspace— Chris Dewolfe & Tom Anderson

17. Ibibo— Ashish Kashyap 18. OLX— Alec Oxenford & Fabrice Grinda 19. Skype— Niklas Zennstrom, JanusFriis & Reid Hoffman 20. Opera— Jon Stephenson von Tetzchner & Geir lvarsoy 21. Mozilla Firefox— Dave Hyatt & Blake Ross 22. Blogger— Evan Willams