1)從android的資源文件夾layout中加載xml布局文件,並把布局文件映射為Bitmap
main.xml文件如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="256px"
- android:layout_height="256px">
- <TextViewandroid:layout_width="wrap_content"android:id="@+id/city"
- android:layout_height="wrap_content"android:textSize="20px"
- android:textColor="#ffffff"android:shadowColor="#0000AA"
- android:shadowDx="0"android:shadowDy="-2"android:shadowRadius="0.1"
- android:layout_gravity="right"android:layout_marginRight="5px"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="256px" android:layout_height="256px"> <TextView android:layout_width="wrap_content" android:id="@+id/city" android:layout_height="wrap_content" android:textSize="20px" android:textColor="#ffffff" android:shadowColor="#0000AA" android:shadowDx="0" android:shadowDy="-2" android:shadowRadius="0.1" android:layout_gravity="right" android:layout_marginRight="5px" /> </LinearLayout>
java代碼中的處理,方法一:
- //加載xml布局文件
- LayoutInflater factory = LayoutInflater.from(context);
- View view = factory.inflate(R.layout.main, null);
- //獲得布局文件中的TextView
- TextView city = (TextView) view.findViewById(R.id.city);
- //設置city的文本信息
- city.setText("xml中的textview");
- //啟用繪圖緩存
- view.setDrawingCacheEnabled(true);
- //調用下面這個方法非常重要,如果沒有調用這個方法,得到的bitmap為null
- view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
- //這個方法也非常重要,設置布局的尺寸和位置
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- //獲得繪圖緩存中的Bitmap
- view.buildDrawingCache();
- Bitmap bitmap = view.getDrawingCache();
//加載xml布局文件
LayoutInflater factory = LayoutInflater.from(context);
View view = factory.inflate(R.layout.main, null);
//獲得布局文件中的TextView
TextView city = (TextView) view.findViewById(R.id.city);
//設置city的文本信息
city.setText("xml中的textview");
//啟用繪圖緩存
view.setDrawingCacheEnabled(true);
//調用下面這個方法非常重要,如果沒有調用這個方法,得到的bitmap為null
view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
//這個方法也非常重要,設置布局的尺寸和位置
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//獲得繪圖緩存中的Bitmap
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
java代碼中的處理,方法二:
- //加載xml布局文件
- LayoutInflater factory = LayoutInflater.from(context);
- View view = factory.inflate(R.layout.main, null);
- //獲得布局文件中的TextView
- TextView city = (TextView) view.findViewById(R.id.city);
- //設置city的文本信息
- city.setText("xml中的textview");
- //調用下面這個方法非常重要,如果沒有調用這個方法,得到的bitmap為null
- view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
- //這個方法也非常重要,設置布局的尺寸和位置
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- //生成bitmap
- Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
- Bitmap.Config.RGB_565);
- //利用bitmap生成畫布
- Canvas canvas = new Canvas(bitmap);
- //把view中的內容繪制在畫布上
- view.draw(canvas);
//加載xml布局文件
LayoutInflater factory = LayoutInflater.from(context);
View view = factory.inflate(R.layout.main, null);
//獲得布局文件中的TextView
TextView city = (TextView) view.findViewById(R.id.city);
//設置city的文本信息
city.setText("xml中的textview");
//調用下面這個方法非常重要,如果沒有調用這個方法,得到的bitmap為null
view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
//這個方法也非常重要,設置布局的尺寸和位置
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//生成bitmap
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.RGB_565);
//利用bitmap生成畫布
Canvas canvas = new Canvas(bitmap);
//把view中的內容繪制在畫布上
view.draw(canvas);
2)Bitmap轉換為byte[]數組
方法一:
- privatebyte[] Bitmap_To_Bytes(Bitmap bitmap){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
- return baos.toByteArray();
- }
private byte[] Bitmap_To_Bytes(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
方法二:
- publicstaticbyte[] readStream(InputStream inStream) throws Exception {
- byte[] buffer = newbyte[1024];
- int len = -1;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- while ((len = inStream.read(buffer)) != -1) {
- baos .write(buffer, 0, len);
- }
- byte[] data = baos .toByteArray();
- baos .close();
- inStream.close();
- return data;
- }
public static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1) {
baos .write(buffer, 0, len);
}
byte[] data = baos .toByteArray();
baos .close();
inStream.close();
return data;
}
3)設置在應用中支持32位的圖像:
在onCreate()函數中加入以下兩行:
- getWindow().setFormat(PixelFormat.RGBX_8888);
- BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
getWindow().setFormat(PixelFormat.RGBX_8888);
BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
以使之 支持32bit的圖像。
4)把drawable文件夾下的文件轉成Bitmap
- Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(),
- R.drawable.down);
