android相關知識簡介

DownloadManager是Android爲開發者提供的一個後臺應用組件,它經過Http層進行文件的下載任務.
1:使用
首先要在AndroidManifest.xml中申請訪問DownloadManager的權限html

添加一個下載任務:
ContentValues values = new ContentValues();
values.put(Downloads.URI, url);//指定下載地址
values.put(Downloads.COOKIE_DATA, cookie);//若是下載Server須要cookie,設置cookie
values.put(Downloads.VISIBILITY,Downloads.VISIBILITY_HIDDEN);//設置下載提示是否在屏幕頂部顯示
values.put(Downloads.NOTIFICATION_PACKAGE, getPackageName());//設置下載完成以後回調的包名
values.put(Downloads.NOTIFICATION_CLASS, DownloadCompleteReceiver.class.getName());//設置下載完成以後負責接收的Receiver,這個類要繼承BroadcastReceiver
values.put(Downloads.DESTINATION,save_path);//設置下載到的路徑,這個須要在Receiver裏自行處理
values.put(Downloads.TITLE,title);//設置下載任務的名稱
this.getContentResolver().insert(Downloads.CONTENT_URI, values);//將其插入到DownloadManager的數據庫中,數據庫會觸發修改事件,啓動下載任務
java

2:如何爲DownloadManager設置代理,好比Wap
values.put(Downloads.PROXY_HOST,」10.0.0.172″);
values.put(Downloads.PROXY_PORT,」80″);android

3:如何在下載過程當中監聽下載任務
能夠經過監聽數據庫來實現
DownloadsChangeObserver mDownloadObserver=new DownloadsChangeObserver(Downloads.CONTENT_URI);
private class DownloadsChangeObserver extends ContentObserver {
public DownloadsChangeObserver(Uri uri) {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
//查詢須要監聽的字段
//好比要監聽實時下載進度,查看當前下載狀態:是否已經斷開,或者下載失敗等等
StringBuilder wherequery = new StringBuilder(Downloads.TITLE);
wherequery.append(「=」);
wherequery.append(「‘」);
wherequery.append(mTitle);
wherequery.append(「‘」);數據庫

mDownloadCursor =mContext.getContentResolver().query(Downloads.CONTENT_URI, new String[] {Downloads.TITLE, Downloads.STATUS, Downloads.CURRENT_BYTES,}, wherequery.toString(), null,orderBy);
int mSizeColunmId=mDownloadCursor.getColumnIndexOrThrow(Downloads.CURRENT_BYTES);
mDownloadCursor.moveToFirst();
int size=mDownloadCursor.getInt(mSizeColunmId);


4:如何刪除下載記錄
private void deleteHistory(String title)//刪除掉指定名稱的下載記錄
{
StringBuilder whereDelete = new StringBuilder(Downloads.TITLE);
whereDelete.append(「=」);
whereDelete.append(「‘」);
whereDelete.append(str);
whereDelete.append(「‘」);
this.getContentResolver().delete(Downloads.CONTENT_URI,whereDelete.toString(), null);
}canvas

在Android中,申請WakeLock可讓你的進程持續執行即便手機進入睡眠模式,比較實用的是好比後臺有網絡功能,能夠保證操做持續進行.
方法: 在操做以前加入
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);cookie

wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
wakeLock.acquire();網絡

別忘了在操做完畢以後釋放掉
if (wakeLock != null) {app

wakeLock.release();
wakeLock = null;
}ide

最近在作項目的時候,遇到這樣的問題,我須要可以把整個application都殺死,找了半天都沒有找到,後來問同事才知道答案。代碼以下:
view plaincopy to clipboardprint?函數

ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mActivityManager .restartPackage(context.getPackageName());

在此作個記錄,以備忘記!

Android 獲取系統中全部安裝的APK的信

private
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
public class AppsAdapter extends BaseAdapter {
public AppsAdapter() {
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(Grid1.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(50, 50));
} else {
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return i;
}

public final int getCount() {
return mApps.size();
}
public final Object getItem(int position) {
return mApps.get(position);
}
public final long getItemId(int position) {
return position;
}
List mApps;

安裝完Android SDK後,默認會在C盤建立一個.Android的文件夾用來存放AVD.此時若是想修改AVD的路徑,能夠設置一個環境變量ANDROID_SDK_HOME=要設置的路徑(好比:F:\Java\Android),從新啓動Eclipse,修改爲功!

android在處理一寫圖片資源的時候,會進行一些類型的轉換,如今有空整理一下:
一、Drawable → Bitmap
Java代碼

public static Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
二、從資源中獲取Bitmap
Java代碼

Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
三、Bitmap → byte[]
Java代碼

private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}

四、 byte[] → Bitmap

Java代碼

private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}

經過分析Launcher的生成快捷方式的過程,找出了使用Intent發送請求,Launcher經過本身註冊的InstallShortCutReceiver和UnInstallShortCutReceiver實現了快捷方式圖標的生成與移除過程。本文主要分析外部apk如何使用Intent請求生成快捷方式和移除快捷方式圖標的問題。

生成快捷方式代碼:

Java代碼

private static final String ACTION_INSTALL_SHORTCUT =
「com.android.launcher.action.INSTALL_SHORTCUT」;

/**
* 是否能夠有多個快捷方式的副本
*/
static final String EXTRA_SHORTCUT_DUPLICATE = 「duplicate」;

Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
Intent intent2 = new Intent(Intent.ACTION_MAIN);
intent2.addCategory(Intent.CATEGORY_LAUNCHER);

intent2.setComponent(new ComponentName(this.getPackageName(),
「.Main」));

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.icon));
sendBroadcast(shortcutIntent);

注:Intent intent2 = new Intent(Intent.ACTION_MAIN); 這個也能夠換成的構造參數也能夠是Intent.ACTION_CREATE_SHORTCUT,也能夠生成快捷方式圖標,可是這樣不標準,在刪除的時候若是不和這個對於相同則沒法刪除。因此仍是用Intent.ACTION_MAIN。

那麼刪除快捷方式的代碼是:
Java代碼

private static final String ACTION_UNINSTALL_SHORTCUT =
「com.android.launcher.action.UNINSTALL_SHORTCUT」;

Intent intent = new Intent(ACTION_UNINSTALL_SHORTCUT );
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
ComponentName comp = new ComponentName(info.activityInfo.packageName,
info.activityInfo.name);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()
.setComponent(comp).setAction(「android.intent.action.MAIN」));
sendBroadcast(intent);

經常使用的Android自定義主題

android:theme="@android :style/Theme.Dialog "    將一個Activity顯示爲能話框模式
android:theme="@android :style/Theme.NoTitleBar "  不顯示應用程序標題欄
android:theme="@android :style/Theme.NoTitleBar.Fullscreen "  不顯示應用程序標題欄,並全屏
android:theme="Theme.Light "  背景爲白色
android:theme=" Theme.Light.NoTitleBar"  白色背景並沒有標題欄 
android:theme=" Theme.Light.NoTitleBar.Fullscreen"  白色背景,無標題欄,全屏
android:theme=" Theme.Black"  背景黑色
android:theme=" Theme.Black.NoTitleBar"  黑色背景並沒有標題欄
android:theme=" Theme.Black.NoTitleBar.Fullscreen"    黑色背景,無標題欄,全屏
android:theme="Theme.Wallpaper"  用系統桌面爲應用程序背景
android:theme="Theme.Wallpaper.NoTitleBar"  用系統桌面爲應用程序背景,且無標題欄
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"  用系統桌面爲應用程序背景,無標題欄,全屏
android:theme="Translucent"
android:theme="Theme.Translucent.NoTitleBar"
android:theme="Theme.Translucent.NoTitleBar.Fullscreen"
android:theme="Theme.Panel"
android:theme="Theme.Light.Panel"


使用Intent調用系統的Camera程序的裁剪功能實現圖片修剪。

  Intent intent = new Intent("com.android.camera.action.CROP");   
   intent.setClassName("com.android.camera", "com.android.camera.CropImage");  

  不過可能會出現沒法找到Activity的android.content.ActivityNotFoundException異常,這是因爲Android內部的gallery和camera都有處理,能夠嘗試另外一種URI,com.android.gallery的com.android.camera.CropImage,在setClassName時,具體的代碼爲

final Intent intent = new Intent("com.android.camera.action.CROP"); 
intent.setClassName("com.android.camera", "com.android.camera.CropImage"); 
intent.setData(Uri.fromFile(mFile)); 
intent.putExtra("outputX", width); 
intent.putExtra("outputY", height); 
intent.putExtra("aspectX", width); 
intent.putExtra("aspectY", height); 
intent.putExtra("scale", true); 
intent.putExtra("noFaceDetection", true); 
intent.putExtra("output", Uri.parse("file:/" + mFile.getAbsolutePath())); 
startActivityForResult(intent, REQUEST_CROP_IMAGE); 


1.思路是經過本身構建KeyEvent對象來改變鍵的功能。

  例如,把全部的按鍵都改爲「返回鍵」的功能,代碼以下:

  //這裏構建KeyEvent對象,其功能爲返回鍵的功能
  //所以咱們按任意鍵都會執行返回鍵功能
  KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);

  //這裏傳入的參數就是咱們本身構建的KeyEvent對象key
  super.onKeyDown(key.getKeyCode(), key); 

至此OK

  控件事件是經過(setOnClickListener)設置其控件的監聽器來監聽並重寫某些函數來處理。

  按鍵按下事件:經過重寫onKeyDown方法

  按鍵重複點擊:經過重寫onKeyMultiple方法
  按鍵彈起事件:經過重寫onKeyUp方法
  觸筆點擊事件:經過實現onTouchEvent方法

-------------------------------------------------------------------------------------------------------------------------

獲取android IP

參考前人研究成果,非我的創做

1.使用WIFI

首先設置用戶權限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

其次,代碼以下

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //獲取wifi服務
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        //判斷wifi是否開啓
        if (!wifiManager.isWifiEnabled()) { 
        wifiManager.setWifiEnabled(true);   
        } 
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();      
        int ipAddress = wifiInfo.getIpAddress();  
        String ip = intToIp(ipAddress);  
        EditText et = (EditText)findViewById(R.id.EditText01);
        et.setText(ip);
    }    
    private String intToIp(int i) {      
        
          return (i & 0xFF ) + "." +      
        ((i >> 8 ) & 0xFF) + "." +      
        ((i >> 16 ) & 0xFF) + "." +      
        ( i >> 24 & 0xFF) ;
     }  

2.使用GPRS

首先,設置用戶上網權限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

其次,代碼以下

public String getLocalIpAddress() 
    { 
        try 
        { 
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) 
            { 
               NetworkInterface intf = en.nextElement(); 
               for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) 
               { 
                   InetAddress inetAddress = enumIpAddr.nextElement(); 
                   if (!inetAddress.isLoopbackAddress()) 
                   { 
                       return inetAddress.getHostAddress().toString(); 
                   } 
               } 
           } 
        } 
        catch (SocketException ex) 
        { 
            Log.e("WifiPreference IpAddress", ex.toString()); 
        } 
        return null; 
    }

----------------------------------------------------------------------------------

不知道其餘應用的action main狀況下啓動他

Android 開發有時須要在一個應用中啓動另外一個應用,好比Launcher加載全部的已安裝的程序的列表,當點擊圖標時能夠啓動另外一個應用。
通常咱們知道了另外一個應用的包名和MainActivity的名字以後即可以直接經過以下代碼來啓動:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);            
ComponentName cn = new ComponentName(packageName, className);            
intent.setComponent(cn);
startActivity(intent);

可是更多的時候,咱們通常都不知道應用程序的啓動Activity的類名,而只知道包名,咱們能夠經過ResolveInfo類來取得啓動Acitivty的類名。

下面是實現代碼:
private void openApp(String packageName) {
PackageInfo pi = getPackageManager().getPackageInfo(packageName, 0);

Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);

List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);

ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

ComponentName cn = new ComponentName(packageName, className);

intent.setComponent(cn);
startActivity(intent);
}
}


android佈局屬性詳解
  各類Layout用到的一些重要的屬性

  第一類:屬性值爲true或false

  android:layout_centerHrizontal 水平居中

  android:layout_centerVertical 垂直居中

  android:layout_centerInparent 相對於父元素徹底居中

  android:layout_alignParentBottom 貼緊父元素的下邊緣

  android:layout_alignParentLeft 貼緊父元素的左邊緣

  android:layout_alignParentRight 貼緊父元素的右邊緣

  android:layout_alignParentTop 貼緊父元素的上邊緣

  android:layout_alignWithParentIfMissing 若是對應的兄弟元素找不到的話就以父元素作參照物

  第二類:屬性值必須爲id的引用名「@id/id-name」

  android:layout_below 在某元素的下方

  android:layout_above 在某元素的的上方

  android:layout_toLeftOf 在某元素的左邊

  android:layout_toRightOf 在某元素的右邊

  android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊

  android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊

  android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊

  android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊

  第三類:屬性值爲具體的像素值,如30dip,40px

  android:layout_marginBottom 離某元素底邊緣的距離

  android:layout_marginLeft 離某元素左邊緣的距離

  android:layout_marginRight 離某元素右邊緣的距離

  android:layout_marginTop 離某元素上邊緣的距離

  EditText的android:hint

  設置EditText爲空時輸入框內的提示信息。

  android:gravity

  android:gravity屬性是對該view 內容的限定.好比一個button 上面的text. 你能夠設置該text 在view的靠左,靠右等位置.以button爲例,android:gravity=」right」則button上面的文字靠右

  android:layout_gravity

  android:layout_gravity是用來設置該view相對與起父view 的位置.好比一個button 在linearlayout裏,你想把該button放在靠左、靠右等位置就能夠經過該屬性設置.以button爲例,android:layout_gravity=」right」則button靠右

  android:layout_alignParentRight

  使當前控件的右端和父控件的右端對齊。這裏屬性值只能爲true或false,默認false。

  android:scaleType:

  android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。ImageView.ScaleType / android:scaleType值的意義區別:

  CENTER /center 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示

  CENTER_CROP / centerCrop 按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬)

  CENTER_INSIDE / centerInside 將圖片的內容完整居中顯示,經過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬

  FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示

  FIT_END / fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置

  FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置

  FIT_XY / fitXY 把圖片 不按比例擴大/縮小到View的大小顯示

  MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。

Android的電源管理部分,在縱向上分爲四層:

第一部分:Application

主要是利用Android Frameworks提供的API進行應用級的開發,須要注意的是相關權限的申明:

< uses-permission android:name="android.permission.WAKE_LOCK" /> 

< uses-permission android:name="android.permission.DEVICE_POWER" />

 

第二部分:Frameworks

1.       / frameworks/base/core/java/android/os/PowerManager.java

v  PowerManager:提供對設備的電源進行管理

?  常見鎖類型

Flag Value

CPU

Screen

Keyboard

PARTIAL_WAKE_LOCK

On

Off

Off

SCREEN_DIM_WAKE_LOCK

On

Dim

Off

SCREEN_BRIGHT_WAKE_LOCK

On

Bright

Off

FULL_WAKE_LOCK

On

Bright

Bright

?  附加鎖類型:僅僅是對Screen有影響

Flag Value

Description

ACQUIRE_CAUSES_WAKEUP

This flag will force the screen and/or keyboard to turn on immediately.

ON_AFTER_RELEASE

If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer

v  WakeLock:電源管理對象鎖

v  Usage:

?  獲取PowerManager實例PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE);   

?  獲取PowerManager.WakeLock實例mWakeLock=pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TEST");

?  Device On

mWakeLock.acquire();

?  Device Off

mWakeLock.release();

2.       / frameworks/base/core/java/android/os/Power.java

提供一些電源管理的函數,如reboot()。其是JNI的上層接口,其往下調用android_os_power.cpp

3.       / frameworks/base/services/java/com/android/server/PowerManagerService.java

電源管理服務,AIDL接口IPowerManager的實現者。

第三部分:JNI

/frameworks/base/core/jni/android_os_power.cpp

第四部分:Native

/hardware/libhardware/power/power.c

Linux kernel交互

第五部分:Linux驅動層

/drivers/android/power.c

第六部分:使用電源管理注意事項

1.       可在onCreate時設置該界面的電源管理,在onDestroy時取消設置

2.       可在onResume時設置該界面的電源管理,在onPause時取消設置

3.       注意設置是以Activity爲單位,不是以應用爲單位

4.       注意在AndroidManifest.xml中聲明該應用有設置電源管理的權限

5.       注意加鎖解鎖要成對出現

6.       注意多個用途最好用多個鎖,不要一鎖多用,以避免出錯

7.       注意對運行在後臺和異常時對鎖的處理

8.       注意在網絡鏈接或傳輸時最好加鎖,以避免傳輸被中斷

http://www.rosoo.net/a/201012/10599.html

9.       注意加鎖以保證程序邏輯

http://173.234.53.177/?paged=4

---------------------------------------------------

 經過程序獲取android系統手機的鈴聲和音量。一樣,設置鈴聲和音量的方法也很簡單!
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//通話音量
      int max = am.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
      int current = am.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
      Log.d(」VIOCE_CALL」, 「max : 」 + max + 」 current : 」 + current);
//系統音量
      max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );
      current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );
      Log.d(」SYSTEM」, 「max : 」 + max + 」 current : 」 + current);
//鈴聲音量
      max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
      current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
      Log.d(」RING」, 「max : 」 + max + 」 current : 」 + current);
//音樂音量
      max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
      current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
      Log.d(」MUSIC」, 「max : 」 + max + 」 current : 」 + current);
//提示聲音音量
      max = am.getStreamMaxVolume( AudioManager.STREAM_ALARM );
      current = sm.getStreamVolume( AudioManager.STREAM_ALARM );
      Log.d(」ALARM」, 「max : 」 + max + 」 current : 」 + current);
設置音量的方法也很簡單,AudioManager提供了方法:
public void setStreamVolume(int streamType, int index, int flags)
其中 streamType 有內置的常量,能夠在AudioManager裏面查到相關的定義。 經過程序獲取android系統手機的鈴聲和音量。一樣,設置鈴聲和音量的方法也很簡單!
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
android中定義的dimension單位有如下這些:
px(Pixels ,像素):對應屏幕上的實際像素點。
in(Inches ,英寸):屏幕物理長度單位。
mm(Millimeters ,毫米):屏幕物理長度單位。
pt(Points ,磅):屏幕物理長度單位,1/72英寸。
dp(與密度無關的像素):邏輯長度單位,在 160 dpi 屏幕上,1dp=1px=1/160英寸。隨着密度變化,對應的像素數量也變化,但並無直接的變化比例。
dip:與dp相同,多用於Google示例中。
sp(與密度和字體縮放度無關的像素):與dp相似,可是能夠根據用戶的字體大小首選項進行縮放。


android在處理一寫圖片資源的時候,會進行一些類型的轉換,如今有空整理一下:
一、Drawable → Bitmap
Java代碼

public static Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
二、從資源中獲取Bitmap
Java代碼

Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
三、Bitmap → byte[]
Java代碼

private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}

四、 byte[] → Bitmap

Java代碼

private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}

android:theme="@android :style/Theme.Dialog"   將一個Activity顯示爲對話框模式
android:theme="@android :style/Theme.NoTitleBar"  不顯示應用程序標題欄
android:theme="@android :style/Theme.NoTitleBar.Fullscreen"  不顯示應用程序標題欄,並全屏
android:theme="@android :style/Theme.Light"  背景爲白色
android:theme="@android :style/Theme.Light.NoTitleBar"  白色背景並沒有標題欄 
android:theme="@android :style/Theme.Light.NoTitleBar.Fullscreen"  白色背景,無標題欄,全屏
android:theme="@android :style/Theme.Black"  背景黑色
android:theme="@android :style/Theme.Black.NoTitleBar"  黑色背景並沒有標題欄
android:theme="@android :style/Theme.Black.NoTitleBar.Fullscreen"    黑色背景,無標題欄,全屏
android:theme="@android :style/Theme.Wallpaper"  用系統桌面爲應用程序背景
android:theme="@android :style/Theme.Wallpaper.NoTitleBar"  用系統桌面爲應用程序背景,且無標題欄
android:theme="@android :style/Theme.Wallpaper.NoTitleBar.Fullscreen"  用系統桌面爲應用程序背景,無標題欄,全屏
android:theme="@android :style/Translucent" 半透明效果
android:theme="@android :style/Theme.Translucent.NoTitleBar"  半透明並沒有標題欄
android:theme="@android :style/Theme.Translucent.NoTitleBar.Fullscreen"  半透明效果,無標題欄,全屏
android:theme="@android :style/Theme.Panel"
android:theme="@android :style/Theme.Light.Panel"
相關文章
相關標籤/搜索