android 基础知识六

来源:互联网 发布:物业公司软件 编辑:程序博客网 时间:2024/06/05 19:31
Android ViewGroup提高绘制性能
Android ViewGroup如果下面有很多子View,绘制的时候,需要开启其子View的绘制缓存功能,从而提高绘制效率。具体的代码如下:


  1. public void setChildrenDrawingCacheEnabled(boolean enabled) {
  2.     final int count = getChildCount();
  3.     for (int i = 0; i < count; i++) {
  4.         final View view = getChildAt(i);
  5.         view.setDrawingCacheEnabled(true);
  6.          
  7.         // Update the drawing caches
  8.         view.buildDrawingCache(true);
  9.     }
  10. }
复制代码
另一方面也可以通过setDrawingCacheQuality(low)将缓存质量降低,减少内存。
最后结束的时候,需要通过以下代码来清空绘制缓存。


  1. void clearChildrenCache() {
  2.     final int count = getChildCount();
  3.     for (int i = 0; i < count; i++) {
  4.         final CellLayout layout = (CellLayout) getChildAt(i);
  5.         layout.setChildrenDrawnWithCacheEnabled(false);
  6.     }
  7. }
复制代码
---------------------------------------------------------------------------
Android按指定大小读取图片
在Android开发中,我们经常遇到Android读取图片大小超过屏幕显示的图(一般只要显示一定规格的预览图即可),在图片特别多或者图片显示很频繁的时候要特别注意这个问题,下面介绍个按指定大小读取图像的方法。
实现原理:首先获取图片文件的图像高和宽,如果小于指定比例,则直接读取;如果超过比例则按指定比例压缩读取。
捕获OutOfMemoryError时注意点:后面返回的是null,不要马上从别的地方再读图片,包括R文件中的,不然依然会抛出这个异常,一般在初始化的时候缓存默认图片,然后显示缓存中的图片。


  1. /** 获取图像的宽高**/
  2. public static int[] getImageWH(String path) {
  3.     int[] wh = {-1, -1};
  4.     if (path == null) {
  5.         return wh;
  6.     }
  7.     File file = new File(path);
  8.     if (file.exists() && !file.isDirectory()) {
  9.         try {
  10.             BitmapFactory.Options options = new BitmapFactory.Options();
  11.             options.inJustDecodeBounds = true;
  12.             InputStream is = new FileInputStream(path);
  13.             BitmapFactory.decodeStream(is, null, options);
  14.             wh[0] = options.outWidth;
  15.             wh[1] = options.outHeight;
  16.         }
  17.         catch (Exception e) {
  18.             Log.w(TAG, "getImageWH Exception.", e);
  19.         }
  20.     }
  21.     return wh;
  22. }
  23.    
  24. public static Bitmap createBitmapByScale(String path, int scale) {
  25.     Bitmap bm = null;
  26.     try {
  27.         //获取宽高
  28.         int[] wh = getImageWH(path);
  29.         if (wh[0] == -1 || wh[1] == -1) {
  30.             return null;
  31.         }
  32.   
  33.         //读取图片
  34.         BitmapFactory.Options options = new BitmapFactory.Options();
  35.         options.inSampleSize = Math.max(wh[0]/scale, wh[1]/scale);
  36.         InputStream is = new FileInputStream(path);
  37.             bm = BitmapFactory.decodeStream(is, null, options);
  38.     }
  39.     catch (Exception e) {
  40.         Log.w(TAG, "createBitmapByScale Exception.", e);
  41.     }
  42.     catch (OutOfMemoryError e) {
  43.         Log.w(TAG, "createBitmapByScale OutOfMemoryError.", e);
  44.         //TODO: out of memory deal..
  45.     }
  46.     return bm;
  47. }

复制代码
--------------------------------------------------------------------------
Android按钮颜色设置1.工程目录
a.在res目录-新建drawble文件夹放入自定义图片

20110926225648.jpg (12.25 KB, 下载次数: 0)

下载附件  保存到相册

2012-10-16 14:01 上传


2.main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="wrap_content"
  10.     android:text="@string/hello"
  11.     />
  12.     <ImageButton
  13.    android:id="@+id/button"
  14.     android:layout_width="wrap_content"
  15.     android:layout_height="wrap_content"
  16.    android:background="#000000"
  17.    android:src="@drawable/sy"
  18.    />
  19. </LinearLayout>
复制代码
3.类代码

  1. package com.YANSE;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.ImageButton;

  5. public class YANSE extends Activity {

  6.     private ImageButton Image =null;

  7.    @Override

  8.    public void onCreate(Bundle savedInstanceState) {

  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.main);

  11.         Image=(ImageButton)findViewById(R.id.button);

  12.     }

  13. }
原创粉丝点击