随笔 - 224  文章 - 41  trackbacks - 0
<2010年5月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

享受编程

常用链接

留言簿(11)

随笔分类(159)

随笔档案(224)

文章分类(2)

文章档案(4)

经典c++博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜

原文地址:http://blog.csdn.net/Veini/archive/2009/09/10/4537365.aspx
本文介绍在Android如何显示2D图像

1)使用XML,代码如下

 view plaincopy to clipboardprint?
<ImageView   
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/android" 
    /> 
<ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/android"
 />

(代码比较简单,不做解析了!!)

2)继承View

根据public void setContentView (View view) ,新建一个继承View的类,并重写onDraw函数。代码如下:

view plaincopy to clipboardprint?
package android.szu.edu.cn;  
 
import android.app.Activity;  
import android.content.Context;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.View;  
import android.view.Window;  
 
public class Show2DImage extends Activity {  
    /** Called when the activity is first created. */ 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        //setContentView(R.layout.main);  
        setContentView(new Panel(this));  
    }  
      
    class Panel extends View{  
 
        public Panel(Context context) {  
            super(context);  
            // TODO Auto-generated constructor stub  
        }  
          
        public void onDraw(Canvas canvas){  
            Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);  
            canvas.drawColor(Color.BLACK);  
            canvas.drawBitmap(_scratch, 10, 10, null);  
        }  
    }  

package android.szu.edu.cn;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

public class Show2DImage extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //setContentView(R.layout.main);
        setContentView(new Panel(this));
    }
   
    class Panel extends View{

  public Panel(Context context) {
   super(context);
   // TODO Auto-generated constructor stub
  }
     
  public void onDraw(Canvas canvas){
   Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
   canvas.drawColor(Color.BLACK);
   canvas.drawBitmap(_scratch, 10, 10, null);
  }
    }
}

3)利用SurfaceView和Thread

view plaincopy to clipboardprint?
package android.szu.edu.cn;  
 
import android.app.Activity;  
import android.content.Context;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.SurfaceHolder;  
import android.view.SurfaceView;  
import android.view.Window;  
 
public class Show2DImage extends Activity {  
    /** Called when the activity is first created. */ 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        // setContentView(R.layout.main);  
        setContentView(new Panel(this));  
    }  
 
    // class Panel extends View{  
    //  
    // public Panel(Context context) {  
    // super(context);  
    // // TODO Auto-generated constructor stub  
    // }  
    //        
    // public void onDraw(Canvas canvas){  
    // Bitmap _scratch = BitmapFactory.decodeResource(getResources(),  
    // R.drawable.icon);  
    // canvas.drawColor(Color.BLACK);  
    // canvas.drawBitmap(_scratch, 10, 10, null);  
    // }  
    // }  
      
    class Panel extends SurfaceView implements SurfaceHolder.Callback {  
 
        private Draw2DThread _thread;  
 
        public Panel(Context context) {  
            super(context);  
            getHolder().addCallback(this);  
            _thread = new Draw2DThread(getHolder(), this);  
        }  
 
        public void onDraw(Canvas canvas) {  
            Bitmap _scratch = BitmapFactory.decodeResource(getResources(),  
                    R.drawable.android);  
            canvas.drawColor(Color.BLACK);  
            canvas.drawBitmap(_scratch, 10, 10, null);  
        }  
 
        @Override  
        public void surfaceChanged(SurfaceHolder holder, int format, int width,  
                int height) {  
            // TODO Auto-generated method stub  
 
        }  
 
        @Override  
        public void surfaceCreated(SurfaceHolder holder) {  
            // TODO Auto-generated method stub  
            _thread.setRunning(true);  
            _thread.start();  
        }  
 
        @Override  
        public void surfaceDestroyed(SurfaceHolder holder) {  
            // TODO Auto-generated method stub  
            boolean retry = true;  
            _thread.setRunning(false);  
            while (retry) {  
                try {  
                    _thread.join();  
                    retry = false;  
                } catch (InterruptedException e) {  
                    // TODO: handle exception  
                }  
            }  
        }  
 
    }  
 
    class Draw2DThread extends Thread {  
        private SurfaceHolder _surfaceHolder;  
        private Panel _panel;  
        private boolean _run;  
 
        public Draw2DThread(SurfaceHolder surfaceHolder, Panel panel) {  
            _surfaceHolder = surfaceHolder;  
            _panel = panel;  
        }  
 
        public void setRunning(boolean run) {  
            _run = run;  
        }  
 
        public void run() {  
            Canvas c;  
            while (_run) {  
                c = null;  
                try {  
                    c = _surfaceHolder.lockCanvas(null);  
                    synchronized (_surfaceHolder) {  
                        _panel.onDraw(c);  
                    }  
                } finally {  
                    if (c != null) {  
                        _surfaceHolder.unlockCanvasAndPost(c);  
                    }  
                }  
            }  
 
        }  
    }  

package android.szu.edu.cn;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class Show2DImage extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  // setContentView(R.layout.main);
  setContentView(new Panel(this));
 }

 // class Panel extends View{
 //
 // public Panel(Context context) {
 // super(context);
 // // TODO Auto-generated constructor stub
 // }
 //     
 // public void onDraw(Canvas canvas){
 // Bitmap _scratch = BitmapFactory.decodeResource(getResources(),
 // R.drawable.icon);
 // canvas.drawColor(Color.BLACK);
 // canvas.drawBitmap(_scratch, 10, 10, null);
 // }
 // }
 
 class Panel extends SurfaceView implements SurfaceHolder.Callback {

  private Draw2DThread _thread;

  public Panel(Context context) {
   super(context);
   getHolder().addCallback(this);
   _thread = new Draw2DThread(getHolder(), this);
  }

  public void onDraw(Canvas canvas) {
   Bitmap _scratch = BitmapFactory.decodeResource(getResources(),
     R.drawable.android);
   canvas.drawColor(Color.BLACK);
   canvas.drawBitmap(_scratch, 10, 10, null);
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
   // TODO Auto-generated method stub

  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
   // TODO Auto-generated method stub
   _thread.setRunning(true);
   _thread.start();
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
   // TODO Auto-generated method stub
   boolean retry = true;
   _thread.setRunning(false);
   while (retry) {
    try {
     _thread.join();
     retry = false;
    } catch (InterruptedException e) {
     // TODO: handle exception
    }
   }
  }

 }

 class Draw2DThread extends Thread {
  private SurfaceHolder _surfaceHolder;
  private Panel _panel;
  private boolean _run;

  public Draw2DThread(SurfaceHolder surfaceHolder, Panel panel) {
   _surfaceHolder = surfaceHolder;
   _panel = panel;
  }

  public void setRunning(boolean run) {
   _run = run;
  }

  public void run() {
   Canvas c;
   while (_run) {
    c = null;
    try {
     c = _surfaceHolder.lockCanvas(null);
     synchronized (_surfaceHolder) {
      _panel.onDraw(c);
     }
    } finally {
     if (c != null) {
      _surfaceHolder.unlockCanvasAndPost(c);
     }
    }
   }

  }
 }
}

第三种方法,可以快速地描绘界面,使用SurfaceView可以让其它非UI线程直接对画面进行操作。

posted on 2010-07-22 23:02 漂漂 阅读(896) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理