Home

Music

Video

Kanny

Android Studio Tutorial wallpaper (Simple Code)






MainActivity.Java

  1. package com.duniyarhausa.duniyarhausawallpaper;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6.  
  7. public class MainActivity extends Activity {
  8.  
  9. protected void onCreate(Bundle paramBundle) {
  10. super.onCreate(paramBundle);
  11. setLiveWP();
  12. }
  13.  
  14. protected void setLiveWP() {
  15. startActivity(new Intent(
  16. "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER"));
  17. finish();
  18. }
  19.  
  20. }




MyWalpaper.Java

  1. package com.duniyarhausa.duniyarhausawallpaper;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import android.content.SharedPreferences;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Canvas;
  10. import android.graphics.Color;
  11. import android.graphics.Matrix;
  12. import android.graphics.Paint;
  13. import android.graphics.Rect;
  14. import android.os.Handler;
  15. import android.service.wallpaper.WallpaperService;
  16. import android.view.MotionEvent;
  17. import android.view.SurfaceHolder;
  18.  
  19. public class MyWallpaper extends WallpaperService {
  20. Bitmap bg;
  21. int height;
  22. int width;
  23. private final Handler mHandler = new Handler();
  24. private boolean mVisible;
  25. BitmapFactory.Options options;
  26. Random randomGenerator;
  27. public float scale;
  28. float scaleSpeed;
  29.  
  30. public void onCreate() {
  31. super.onCreate();
  32. this.scale = getResources().getDisplayMetrics().density;
  33. this.randomGenerator = new Random();
  34. this.options = new BitmapFactory.Options();
  35. this.options.inPreferredConfig = Bitmap.Config.ARGB_4444;
  36. this.options.inPurgeable = true;
  37. this.options.inInputShareable = true;
  38. }
  39.  
  40. @Override
  41. public WallpaperService.Engine onCreateEngine() {
  42. return new CubeEngine();
  43. }
  44.  
  45. public void onDestroy() {
  46. super.onDestroy();
  47. }
  48.  
  49. class CubeEngine extends WallpaperService.Engine {
  50. Canvas c;
  51. ArrayList<Elements> elements = new ArrayList<Elements>();
  52. public float eventX;
  53. public float eventY;
  54. SurfaceHolder holder;
  55. Matrix m;
  56. private final Runnable mDrawer;
  57. private final Paint mPaint = new Paint();
  58. private final Paint mPaintBG = new Paint();
  59. private float mTouchX = -1.0F;
  60. private float mTouchY = -1.0F;
  61. boolean tapFlag = false;
  62.  
  63. CubeEngine() {
  64. this.mDrawer = new Runnable() {
  65. public void run() {
  66. MyWallpaper.CubeEngine.this.drawFrame();
  67. }
  68. };
  69. this.m = new Matrix();
  70. this.mPaint.setAntiAlias(true);
  71. this.mPaint.setFilterBitmap(true);
  72. this.mPaint.setDither(true);
  73. this.mPaintBG.setAntiAlias(true);
  74. this.mPaintBG.setFilterBitmap(true);
  75. this.mPaintBG.setDither(true);
  76. MyWallpaper.this.scaleSpeed = 0.02f;
  77. MyWallpaper.this.bg = BitmapFactory.decodeResource(
  78. MyWallpaper.this.getResources(), R.drawable.background);
  79. // Initialize all elements
  80. int[] drawables = {R.drawable.e1, R.drawable.e2, R.drawable.e3, R.drawable.e4, R.drawable.e5, R.drawable.e6, R.drawable.e7, R.drawable.e8, R.drawable.e9, R.drawable.e1, R.drawable.e2, R.drawable.e8, R.drawable.e9, R.drawable.e3, R.drawable.e4};
  81. for (int i = 0; i < drawables.length; ++i) {
  82. Elements e = new Elements(
  83. BitmapFactory.decodeResource(
  84. MyWallpaper.this.getResources(), drawables[i],
  85. MyWallpaper.this.options),
  86. (int) (randomGenerator.nextInt(400) * MyWallpaper.this.scale),
  87. (int) (randomGenerator.nextInt(600) * MyWallpaper.this.scale),
  88. randomGenerator.nextInt(100) / 100.0f, randomGenerator.nextInt(360), randomGenerator.nextInt(255));
  89. this.elements.add(e);
  90. }
  91.  
  92. drawFrame();
  93. }
  94.  
  95. void drawAll(Canvas canvas) {
  96. canvas.save();
  97. canvas.translate(0, 0);
  98. canvas.drawBitmap(MyWallpaper.this.bg, new Rect(0, 0,
  99. MyWallpaper.this.bg.getWidth(),
  100. MyWallpaper.this.bg.getHeight()),
  101. new Rect(0, 0, canvas.getWidth(), canvas.getHeight()),
  102. this.mPaintBG);
  103. for (int i = 0; i < this.elements.size(); i++) {
  104. Elements e = this.elements.get(i);
  105. Matrix matrix = new Matrix();
  106. matrix.postScale(e.scale, e.scale);
  107. matrix.postRotate(e.rotate);
  108. matrix.postTranslate(e.x, e.y);
  109. this.mPaint.setAlpha(e.alpha);
  110. canvas.drawBitmap(e.b, matrix, this.mPaint);
  111. }
  112.  
  113. canvas.restore();
  114. }
  115.  
  116. void drawFrame() {
  117. final SurfaceHolder holder = getSurfaceHolder();
  118.  
  119. try {
  120. c = holder.lockCanvas();
  121. if (c != null) {
  122. drawAll(c);
  123. drawTouchPoint(c);
  124. }
  125.  
  126. mHandler.removeCallbacks(mDrawer);
  127. if (mVisible) {
  128. update(c);
  129. mHandler.postDelayed(mDrawer, 40L);
  130. }
  131. } finally {
  132. // Release the canvas
  133. if (c != null) {
  134. holder.unlockCanvasAndPost(c);
  135. }
  136. }
  137. }
  138.  
  139. void drawTouchPoint(Canvas canvas) {
  140. if ((this.mTouchX >= 0.0F) && (this.mTouchY >= 0.0F) && (!this.tapFlag)) {
  141. this.tapFlag = true;
  142. int i = MyWallpaper.this.randomGenerator.nextInt(-1
  143. + this.elements.size());
  144. if (i < this.elements.size()) {
  145. Elements e = this.elements.get(i);
  146. e.x = (int) this.mTouchX;
  147. e.y = (int) this.mTouchY;
  148. e.alpha = 255;
  149. e.scale = 1.0F;
  150. e.rotate = 0;
  151. e.alphaTag = false;
  152. e.scaleTag = false;
  153. }
  154. }
  155. }
  156.  
  157. public void onCreate(SurfaceHolder holder) {
  158. super.onCreate(holder);
  159. setTouchEventsEnabled(true);
  160. }
  161.  
  162. public void onDestroy() {
  163. super.onDestroy();
  164. MyWallpaper.this.mHandler.removeCallbacks(this.mDrawer);
  165. for (int i = 0; i < this.elements.size(); i++) {
  166. Elements e = this.elements.get(i);
  167. if (e.b != null) {
  168. e.b.recycle();
  169. e.b = null;
  170. }
  171. }
  172. }
  173.  
  174. public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
  175. if (!MyWallpaper.this.mVisible) {
  176. MyWallpaper.this.mVisible = true;
  177. drawFrame();
  178. }
  179. }
  180.  
  181. public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  182. super.onSurfaceChanged(holder, format, width, height);
  183. MyWallpaper.this.height = height;
  184. MyWallpaper.this.width = width;
  185. getSurfaceHolder();
  186. this.c = null;
  187. }
  188.  
  189. public void onSurfaceCreated(SurfaceHolder holder) {
  190. super.onSurfaceCreated(holder);
  191. }
  192.  
  193. public void onSurfaceDestroyed(SurfaceHolder holder) {
  194. super.onSurfaceDestroyed(holder);
  195. MyWallpaper.this.mVisible = false;
  196. MyWallpaper.this.mHandler.removeCallbacks(this.mDrawer);
  197. }
  198.  
  199. public void onTouchEvent(MotionEvent motionevent) {
  200. if (motionevent.getAction() == 0) {
  201. mTouchX = motionevent.getX();
  202. mTouchY = motionevent.getY();
  203. if (!mVisible) {
  204. mVisible = true;
  205. drawFrame();
  206. }
  207. } else {
  208. tapFlag = false;
  209. mTouchX = -1F;
  210. mTouchY = -1F;
  211. }
  212. super.onTouchEvent(motionevent);
  213. }
  214.  
  215. public void onVisibilityChanged(boolean visible) {
  216. MyWallpaper.this.mVisible = visible;
  217. if (visible) {
  218. drawFrame();
  219. } else {
  220. MyWallpaper.this.mHandler.removeCallbacks(this.mDrawer);
  221. }
  222. }
  223.  
  224. public void update(Canvas canvas) {
  225. for (int i = 0; i < elements.size(); ++i) {
  226. Elements e = elements.get(i);
  227.  
  228. if (!e.scaleTag) {
  229. e.scale = e.scale + scaleSpeed;
  230. } else {
  231. e.scale = e.scale - scaleSpeed;
  232. }
  233.  
  234. if (e.scale >= 0.99) {
  235. e.scaleTag = true;
  236. } else if (e.scale <= 0.01) {
  237. e.rotate = randomGenerator.nextInt(360);
  238. e.scaleTag = false;
  239. e.x = randomGenerator.nextInt(width);
  240. e.y = randomGenerator.nextInt(height);
  241. }
  242.  
  243. if (!e.alphaTag) {
  244. e.alpha = e.alpha + 1;
  245. } else {
  246. e.alpha = e.alpha - 1;
  247. }
  248.  
  249. if (e.alpha >= 250) {
  250. e.alphaTag = true;
  251. } else if (e.alpha <= 50) {
  252. e.alphaTag = false;
  253. }
  254. }
  255. }
  256.  
  257. }
  258.  
  259. }




Elements.Java


  1. package com.duniyarhausa.duniyarhausawallpaper;
  2.  
  3. import android.graphics.Bitmap;
  4.  
  5. public class Elements {
  6. int alpha;
  7. boolean alphaTag;
  8. Bitmap b;
  9. int rotate;
  10. boolean rotateTag;
  11. float scale;
  12. boolean scaleTag;
  13. int x;
  14. int y;
  15.  
  16. public Elements(Bitmap bitmap, int x, int y, float scale, int rotate, int alpha) {
  17. this.b = bitmap;
  18. this.x = x;
  19. this.y = y;
  20. this.scale = scale;
  21. this.rotate = rotate;
  22. this.alpha = alpha;
  23. this.alphaTag = false;
  24. this.scaleTag = false;
  25. }
  26. }




Styles.xml


  1. <resources>
  2.  
  3. <!--
  4. Base application theme, dependent on API level. This theme is replaced
  5. by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
  6. -->
  7. <style name="AppBaseTheme" parent="android:Theme.Light">
  8. <!--
  9. Theme customizations available in newer API levels can go in
  10. res/values-vXX/styles.xml, while customizations related to
  11. backward-compatibility can go here.
  12. -->
  13. </style>
  14.  
  15. <!-- Application theme. -->
  16. <style name="AppTheme" parent="AppBaseTheme">
  17. <!-- All customizations that are NOT specific to a particular API-level can go here. -->
  18. </style>
  19.  
  20. </resources>


Strings.xml


  1. <resources>
  2.  
  3. <string name="app_name">DuniyarHausa Wallpaper</string>
  4. <string name="wallpaper_description">Duniyar Hausa Wallpaper</string>
  5. <string name="wallpaper_name">DuniyarHausa Wallpaper</string>
  6.  
  7. </resources>


Wallpaper_data.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:description="@string/wallpaper_description"
  4. android:thumbnail="@drawable/ic_launcher" />


AndroidManifest.xml


  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.duniyarhausa.duniyarhausawallpaper"
  3. android:versionCode="1"
  4. android:versionName="1.0" >
  5.  
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="21" />
  9.  
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name="MainActivity"
  17. android:label="@string/app_name"
  18. android:screenOrientation="portrait" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24.  
  25. <service
  26. android:name="MyWallpaper"
  27. android:configChanges="keyboardHidden|orientation"
  28. android:icon="@drawable/ic_launcher"
  29. android:label="@string/wallpaper_name"
  30. android:permission="android.permission.BIND_WALLPAPER"
  31. android:screenOrientation="portrait" >
  32. <intent-filter>
  33. <action android:name="android.service.wallpaper.WallpaperService" />
  34. </intent-filter>
  35.  
  36. <meta-data
  37. android:name="android.service.wallpaper"
  38. android:resource="@xml/wallpaper_data" />
  39. </service>
  40. </application>
  41.  
  42. </manifest>

Share this



Fb tt wp ig
Powered by DuniyarHausa and Developed By DuniyarHausa Web team