Android Drawable及其相關(guān)類的使用
一個(gè)讓人賞心悅目的界面對(duì)軟件來說非常重要,因此圖形圖像資源也顯得非常重要。本講就要談一談Android中處理圖形圖像的最重要的一個(gè)類Drawable。Drawable就是一個(gè)可以畫的對(duì)象的抽象(有點(diǎn)別扭,你湊合看吧),下面是它的繼承關(guān)系,可以看到BitmapDrawable,AnimationDrawable等對(duì)象都是它的子類。
最簡單的使用Drawable資源的方法是,把圖片放入Android工程的res\drawable目錄下,編程環(huán)境會(huì)自動(dòng)在R類里為此資源創(chuàng)建一個(gè)引用。你可以使用此引用訪問該資源對(duì)象。譬如對(duì)應(yīng)用程序的圖標(biāo),在Java代碼中可以用R.drawable.icon引用到它,在XML中可以用@drawable/icon引用到它。
那么如果圖片資源不在項(xiàng)目中而是在SDCard中時(shí)如何使用呢,我們看一下下面的例子學(xué)習(xí)一下Drawable的使用,并且順便學(xué)習(xí)一下Bitmap和BitmapFactory的使用。
1、創(chuàng)建項(xiàng)目 Lesson23_Drawable,主Acitivity的名字是 MainDrawable.java,拷貝a.jpg和b.jpg兩個(gè)文件到sdcard中
2、res\main.xml的內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="20sp" android:text="Drawable的使用-設(shè)置壁紙"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看圖片A" android:id="@+id/Button01"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看圖片B" android:id="@+id/Button02"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="設(shè)置圖片A為壁紙" android:id="@+id/Button03"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="設(shè)置圖片B為壁紙" android:id="@+id/Button04"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="恢復(fù)默認(rèn)壁紙" android:id="@+id/Button05"> </button> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ImageView01"> </imageview> </textview></linearlayout>
3、MainDrawable.java的內(nèi)容如下:
package android.basic.lesson23; import java.io.IOException; import android.app.Activity; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainDrawable extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //定義UI組件 Button b1 = (Button) findViewById(R.id.Button01); Button b2 = (Button) findViewById(R.id.Button02); Button b3 = (Button) findViewById(R.id.Button03); Button b4 = (Button) findViewById(R.id.Button04); Button b5 = (Button) findViewById(R.id.Button05); final ImageView iv= (ImageView)findViewById(R.id.ImageView01); //定義按鈕點(diǎn)擊監(jiān)聽器 OnClickListener ocl = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.Button01: //給ImageView設(shè)置圖片,從存儲(chǔ)卡中獲取圖片為Drawable,然后把Drawable設(shè)置為ImageView的背景 iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/a.jpg")); break; case R.id.Button02: iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/b.jpg")); break; case R.id.Button03: try { //Activity的父類ContextWrapper有這個(gè)setWallpaper方法,當(dāng)然使用此方法需要有android.permission.SET_WALLPAPER權(quán)限 setWallpaper(BitmapFactory.decodeFile("/sdcard/a.jpg")); } catch (IOException e1) { e1.printStackTrace(); } break; case R.id.Button04: try { setWallpaper(BitmapFactory.decodeFile("/sdcard/b.jpg")); } catch (IOException e1) { e1.printStackTrace(); } break; case R.id.Button05: try { //Activity的父類ContextWrapper有這個(gè)clearWallpaper方法,作用是恢復(fù)默認(rèn)壁紙,當(dāng)然使用此方法需要有android.permission.SET_WALLPAPER權(quán)限 clearWallpaper(); } catch (IOException e) { e.printStackTrace(); } break; } } }; //給按鈕們綁定點(diǎn)擊監(jiān)聽器 b1.setOnClickListener(ocl); b2.setOnClickListener(ocl); b3.setOnClickListener(ocl); b4.setOnClickListener(ocl); b5.setOnClickListener(ocl); } }
4、AndroidManifest.xml的內(nèi)容如下(設(shè)置權(quán)限)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson23" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:label="@string/app_name" android:name=".MainDrawable"> <intent -filter=""> <action android:name="android.intent.action.MAIN"> <category android:name="android.intent.category.LAUNCHER"> </category></action></intent> </activity> </application> <uses -sdk="" android:minsdkversion="8"> <uses -permission="" android:name="android.permission.SET_WALLPAPER"></uses> </uses></manifest>
5、運(yùn)行程序,查看結(jié)果:
點(diǎn)擊“查看圖片A”按鈕,ImageView載入圖片A并顯示出來
點(diǎn)擊”設(shè)置圖片B為壁紙”按鈕,可以看到圖片B已經(jīng)成為桌面壁紙。 好了本講就到這里。
以上就是對(duì)Android Drawable 的資料整理,后續(xù)繼續(xù)補(bǔ)充,謝謝大家的支持!
- android中圖形圖像處理之drawable用法分析
- 詳解Android中Drawable方法
- 關(guān)于Android中drawable必知的一些規(guī)則
- Android自定義Drawable實(shí)現(xiàn)圓角效果
- Android開發(fā)基于Drawable實(shí)現(xiàn)圓角矩形的方法
- Android自定義Drawable實(shí)現(xiàn)圓形和圓角
- Android Drawable和Bitmap的轉(zhuǎn)換實(shí)例詳解
- Android中drawable使用Shape資源
- Android開發(fā)使用Drawable繪制圓角與圓形圖案功能示例
- Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例
相關(guān)文章
android實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Android端實(shí)現(xiàn)單點(diǎn)登錄的方法詳解
所謂單點(diǎn)登錄就是指的同一個(gè)賬戶(id)不能在一個(gè)以上的設(shè)備上登錄對(duì)應(yīng)的用戶系統(tǒng)(排除web端和移動(dòng)端可以同時(shí)登錄的情況),例如:用戶m在A設(shè)備登錄并保持登錄狀態(tài),然后又在B設(shè)備登錄,此時(shí)A應(yīng)該要強(qiáng)制下線,m無法在A設(shè)備上繼續(xù)執(zhí)行用戶相關(guān)的操作,下面來一起看看吧。2016-11-11Android sd卡讀取數(shù)據(jù)庫實(shí)例代碼
這篇文章主要介紹了Android sd卡讀取數(shù)據(jù)庫實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Android startActivityForResult的基本用法詳解
這篇文章主要介紹了Android startActivityForResult的基本用法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android編程獲取網(wǎng)絡(luò)連接方式及判斷手機(jī)卡所屬運(yùn)營商的方法
這篇文章主要介紹了Android編程獲取網(wǎng)絡(luò)連接方式及判斷手機(jī)卡所屬運(yùn)營商的方法,涉及Android針對(duì)網(wǎng)絡(luò)的判斷及本機(jī)信息的獲取技巧,需要的朋友可以參考下2016-01-01Android獲取手機(jī)系統(tǒng)版本等信息的方法
這篇文章主要介紹了Android獲取手機(jī)系統(tǒng)版本等信息的方法,涉及Android獲取手機(jī)版本中各種常見信息的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04如何獲取Android設(shè)備掛載的所有存儲(chǔ)器
這篇文章主要為大家詳細(xì)介紹了如何獲取Android設(shè)備掛載的所有存儲(chǔ)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Android Studio實(shí)現(xiàn)簡易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)簡易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07