Android封裝自定義庫(jar)

在開發過程中經常需要將代碼封裝後交付使用,在Android中也是如此

下面是封裝的步驟

創建一個沒有Activity的Android的工程

1、將res中的資源全部刪除

2、刪除AndroidManifest.xml中資源的引用

  類似這樣的代碼android:icon="@drawable/icon" android:label="@string/app_name"

  其實AndroidManifest在我們發佈jar的時候並不發佈,之所以這樣做不過是爲了編譯時好看而已。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="MyAndroid.Widget"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6. </manifest> 
3、建立自己的Java控件

  1. package MyAndroid.Widget;
  2. import java.io.InputStream;
  3. import java.net.URL;
  4. import android.content.Context;
  5. import android.content.res.AssetManager;
  6. import android.util.AttributeSet;
  7. import android.view.View;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Canvas;
  11. import android.graphics.Color;
  12. import android.graphics.Paint;
  13. import android.graphics.Rect;
  14. public class MyView extends View {
  15. public MyView(Context context) {
  16. super(context);
  17. // TODO Auto-generated constructor stub
  18. }
  19. public MyView(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. // TODO Auto-generated constructor stub
  22. }
  23. public MyView(Context context, AttributeSet attrs, int defStyle) {
  24. super(context, attrs, defStyle);
  25. // TODO Auto-generated constructor stub
  26. }
  27. @Override
  28. protected void onDraw(Canvas canvas) {
  29. // TODO Auto-generated method stub
  30. super.onDraw(canvas);
  31. canvas.drawColor(Color.GRAY);
  32. Paint paint=new Paint();
  33. paint.setAntiAlias(true);
  34. paint.setColor(Color.RED);
  35. canvas.drawRect(new Rect(10,10,20,20), paint);
  36. Bitmap bgImg = getImageFromAssetFile("png-040.png"); 
  37. canvas.drawBitmap(bgImg, 20, 20, null);
  38. }
  39. private Bitmap getImageFromAssetFile(String fileName){  
  40.     Bitmap image = null;  
  41.     try{  
  42.         AssetManager am = getContext().getAssets();  
  43.         InputStream is = am.open(fileName);  
  44.         image = BitmapFactory.decodeStream(is);  
  45.         is.close();  
  46.     }catch(Exception e){  
  47.           
  48.     }  
  49.     return image;  
  50. } 
  51. }
注意 如果要在jar中封裝自己的資源,要把資源放入assets目錄中

我的demo包中我打入了一個png圖片,資源的名字不能和工程中的資源名字重複。

4、 在 Package Explorer Panel的工程名上 點擊右鍵,選擇 Export

  Java-> JAR file 點下一步

 去掉.classpath, .project, AndroidManifest.xlm, default.properties上的選擇

 輸入 Jar的輸出路徑 點Finish

這樣我們的libary 就創建完成了


使用方法

在打開工程的Properties窗口

選擇Java Build Path中的Libraries頁

點擊 Add External JARs 選擇我們剛剛生成的jar文件

這樣我們就可以使用了編譯好的jar庫了

 

 

  1. <MyAndroid.Widget.MyView
  2.     android:layout_width="fill_parent"
  3.     android:layout_height="fill_parent"
  4.     android:id="@+id/myview"
  5. >
  6. </MyAndroid.Widget.MyView>
  7.  
  8.     MyView myview=(MyView) findViewById(R.id.myview)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章