Android ViewGroup如果下面有很多子View,繪製的時候,需要開啓其子View的繪製緩存功能,從而提高繪製效率。具體的代碼如下:
- public void setChildrenDrawingCacheEnabled(boolean enabled) {
- final int count = getChildCount();
- for (int i = 0; i < count; i++) {
- final View view = getChildAt(i);
- view.setDrawingCacheEnabled(true);
-
- // Update the drawing caches
- view.buildDrawingCache(true);
- }
- }
最後結束的時候,需要通過以下代碼來清空繪製緩存。
- void clearChildrenCache() {
- final int count = getChildCount();
- for (int i = 0; i < count; i++) {
- final CellLayout layout = (CellLayout) getChildAt(i);
- layout.setChildrenDrawnWithCacheEnabled(false);
- }
- }
Android按指定大小讀取圖片
在Android開發中,我們經常遇到Android讀取圖片大小超過屏幕顯示的圖(一般只要顯示一定規格的預覽圖即可),在圖片特別多或者圖片顯示很頻繁的時候要特別注意這個問題,下面介紹個按指定大小讀取圖像的方法。
實現原理:首先獲取圖片文件的圖像高和寬,如果小於指定比例,則直接讀取;如果超過比例則按指定比例壓縮讀取。
捕獲OutOfMemoryError時注意點:後面返回的是null,不要馬上從別的地方再讀圖片,包括R文件中的,不然依然會拋出這個異常,一般在初始化的時候緩存默認圖片,然後顯示緩存中的圖片。
- /** 獲取圖像的寬高**/
- public static int[] getImageWH(String path) {
- int[] wh = {-1, -1};
- if (path == null) {
- return wh;
- }
- File file = new File(path);
- if (file.exists() && !file.isDirectory()) {
- try {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- InputStream is = new FileInputStream(path);
- BitmapFactory.decodeStream(is, null, options);
- wh[0] = options.outWidth;
- wh[1] = options.outHeight;
- }
- catch (Exception e) {
- Log.w(TAG, "getImageWH Exception.", e);
- }
- }
- return wh;
- }
-
- public static Bitmap createBitmapByScale(String path, int scale) {
- Bitmap bm = null;
- try {
- //獲取寬高
- int[] wh = getImageWH(path);
- if (wh[0] == -1 || wh[1] == -1) {
- return null;
- }
-
- //讀取圖片
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = Math.max(wh[0]/scale, wh[1]/scale);
- InputStream is = new FileInputStream(path);
- bm = BitmapFactory.decodeStream(is, null, options);
- }
- catch (Exception e) {
- Log.w(TAG, "createBitmapByScale Exception.", e);
- }
- catch (OutOfMemoryError e) {
- Log.w(TAG, "createBitmapByScale OutOfMemoryError.", e);
- //TODO: out of memory deal..
- }
- return bm;
- }
Android按鈕顏色設置1.工程目錄
a.在res目錄-新建drawble文件夾放入自定義圖片
2.main.xml
- <?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:text="@string/hello"
- />
- <ImageButton
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#000000"
- android:src="@drawable/sy"
- />
- </LinearLayout>
- package com.YANSE;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ImageButton;
- public class YANSE extends Activity {
- private ImageButton Image =null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Image=(ImageButton)findViewById(R.id.button);
- }
- }